Reputation: 771
Let's say I've got callback function which is executed when specified player dies.
function OnPlayerDeath(playerid)
end
I want this function to be called inside Lua C module without putting it inside lua script:
static int l_OnPlayerConnect (lua_State * L) {
enum { lc_nformalargs = 1 };
lua_settop(L,1);
// so here I can use playerid argument - 1 arg
return 0;
}
It is somehow possible to receive this callback argument in C?
#define LUA extern "C" __declspec(dllexport) int __cdecl
LUA luaopen_mymodule(lua_State *L)
{
/* function OnPlayerConnect( playerid )
*
* end */
lua_pushcfunction(L,l_OnPlayerConnect);
lua_setfield(L,LUA_GLOBALSINDEX,"OnPlayerConnect"); //there's already OnPlayerConnect I just want to also call it here but I don't know how.
assert(lua_gettop(L) - lc_nextra == 0);
return 1;
}
I don't want to push this function onto the lua stack because this func already exists. I just want it to be already existing Lua function.
Upvotes: 0
Views: 325
Reputation: 5525
You needed to push it on the stack one way or another, if you want to run it in Lua from C API. If it already exists somewhere in the global table, you can push it by calling lua_getglobal. lua_call (lua_pcall) requires the function to be called and its arguments to exist on top of the stack before making the call.
If you feel like it, you can check LuaJIT ffi callback feature, but it's not plain Lua.
Upvotes: 1