lua-users home
lua-l archive

Calling a function in a table from C++ without loosing 'self'

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


Hi all,
I'm trying to call a function called 'init' in a table from C++. Currently this is what I've got:
int cGameObject::start(lua_State *VM)
{
// first get the table
lua_pushstring(VM, m_TableName); // m_TableName = "GameObject" in this example
lua_gettable(VM, LUA_GLOBALSINDEX);
// get the 'init' function from the table
lua_pushstring(VM, "init");
lua_gettable(VM, -2);
// call the function
lua_call(VM, 0, 0);
return 0;
}
Now this works fine, except when using 'self' in the init function, it fails. 'self' is unknown.
But when I do this: lua_dostring("GameObject:init()") 'self' *is* known in the init() function.
The reason why I would like to do the call myself, besides performance, is I want to pass any parameters passed to the 'start' function on to the 'init' function.
So, what am I doing wrong? Why is 'self' unknown when calling the 'init' function directly?
Thanks,
Hugo

AltStyle によって変換されたページ (->オリジナル) /