lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Gavin Kistner wrote:
[...]
> Does anyone have any suggestions on how to pre-allocate a few userdata
> objects and associate them with a specific function, so that when that
> function is called (from different 'scopes' as the self using Lua's OOP
> syntax sugar) you can access them?

You can use closures for this.

local function f_maker()
	local foo = 1
	local bar = 2
	local baz = 3
	return function(a, b, c)
		foo = foo + a
		bar = bar + b
		baz = baz + c
		return foo + bar + baz
	end
end
f = f_maker()

f is now a global function with three items of static data (in C parlance).

Does that help?

-- 
David Given
dg@cowlark.com