lua-users home
lua-l archive

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


Hello there, all.

I was wondering if anyone can help me with the following :

I'm calling a Lua function from C, and wish to set up some local variables
for that function, pre-calling it.  In my current implementation, I'm
setting the variables in the global table, but for garbage collection
reasons, I'd rather not be leaving references floating around in the global
namespace, i.e. I'd like those references cleared up when the locals go out
of scope.

And all without stopping that function from having access to the variables
stored in the global scope, of course...

My question is how do I go about this?

Currently, I'm doing something along the lines of :

//
//  This global set is the bit that I want to remove in favour of adding a
local
//  to the lua function in question.
//

s32 top = lua_gettop(s);			// Store the stack top.
lua_pushstring(s, mName);
lua_pushstring(2,"gFiringTrigger");	
lua_settable(s, LUA_GLOBALSINDEX};
lua_settop(s,top);					// Restore the stack
top.

//
//  Now call the function.
//

// push the function reference on the stack:
lua_getref (s, funcRef);

// get the function from the global table:
lua_rawget (s, LUA_GLOBALSINDEX);

// test if the function exists:
if(lua_isfunction (s, -1))
{
	// call the function:
	lua_call (s, nArgs, nResults);
	bRetVal = true;
}

Thanks in advance,

Dan
--
Plotting the Downfall of Humanity, since 1974...