lua-users home
lua-l archive

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


Hi,

the most simple way is

int KeyHandlerRef;

int SetKeyFunc( lua_State* l )
{
  KeyHandlerRef = lua_ref( l, 1 );
}

void CallKeyHandler( lua_State* l, int key )
{
  lua_getref( l, KeyHandlerRef ); //push func on stack
  lua_pushnumber( l, key ); // push key param
  lua_call( l, 1, 0 ); // call
  lua_pop( l, 1 ); // pop func from stack
}

The lua_ref stuff is just there for compatibility (older versions)
but it works well. What it basically does is, store the function
at some index (KeyHandlerRef) inside a table (registry) and read
back that functionvalue on the stack, using lua_getref.

Regards,

  Michael


> -----Ursprüngliche Nachricht-----
> Von: owner-lua-l@tecgraf.puc-rio.br 
> [mailto:owner-lua-l@tecgraf.puc-rio.br] Im Auftrag von 
> jose_marin2 <jose_marin2@yahoo.com.br>
> Gesendet: Montag, 6. Januar 2003 18:08
> An: Multiple recipients of list
> Betreff: How to get Lua functions address?
> 
> 
> Hi!
> 
> I want to do something like this:
> 
> 
> I Lua file:
> 
> ---------------------------------
> 
> function KeyFunc(key)
> .
> .
> .
> end
> 
> SetKeyFunction(KeyFunc)
> 
> ---------------------------------
> 
> 
> SetKeyFunction is a C function. It must to get the address of the 
> function and store it in a variable.
> Then, when the user press a key, I must call this func (KeyFunc) with 
> a parameter (the key pressed).
> 
> I have 2 problems: 
> 1 - how to get and store a Lua function address in a C 
> variable and 2 - how to call it with a parameter from C code.
> 
> Any tip?
> 
> Thanks in advance
> 
> José
>