lua-users home
lua-l archive

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


you could also save the function as a string instead of using luaref, at
least thats what I do in our game.

your C function called addEvent would need to take a time, in your case 10
secs and a global variable function name that is the function you want to
call on the timer event, in your case "f".

-- So it would look like this:
addEvent( 10, "f" )

in C you would register the C function addEvent as so:

int addEvent( lua_State* state )
{
	// get time - note no error checking :(
	float time = (float)lua_tonumber( state, 1 );
	// get function name you want to call on timer
	const char* lua_func_name = lua_tostring( state, 2 );
	// save func name to be called later on a timer
	
	return 0;
}

// function that gets called on timer
void process_registered_lua_functions( lua_State* state)
{
	// get function name from whereever you stored it
	lua_getglobal( state, stored_lua_func_name );

	// call this function with no args and no returns
	lua_call( state, 0, 0);
}

hope this helps. This is not exactly how I would write it but I hope you get
my point. 

shelby.




-----Original Message-----
From: Eric Ries [mailto:eries@there.com]
Sent: Friday, November 16, 2001 5:44 PM
To: Multiple recipients of list
Subject: RE: Storing function references


See the manual entry for lua_ref()

Later on, you can use lua_getref and then lua_call to do what you want.

Eric

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Kenneth Gangstoe
> Sent: Friday, November 16, 2001 5:25 PM
> To: Multiple recipients of list
> Subject: Storing function references
>
>
> Hi.
>
> I am in need of storing a reference to a Lua function in C, for later use.
>
> In Lua:
>
> function f() write("foo") end
>
> addEvent(10, f)
>
>
> The question becomes how do I at the C side (where addEvent is
> implemented)
> save a reference to 'f' so that I can call 'f' asynchronously 10 seconds
> later ?
>
> - Kenneth
>