Thanks for the quick reply!If I change it to lua_call(VM, 1, 0) the interpreter goes into error and stops executing my script.So if I do:msg("Will show")
GameObject:start()msg("Won't show")...the second message won't show. (note: 'msg' is a function to simply show a message box).Shouldn't I somehow push 'self' onto the stack before doing the call? Is this the same as pushing the metatable onto the stack? (just guessing here :)Thanks again,Hugo-----Original Message-----On Jun 8, 2005, at 8:58 PM, Framework Studios: Hugo wrote:
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Russell Y.Webb
Sent: woensdag 8 juni 2005 12:09
To: Lua list
Subject: Re: Calling a function in a table from C++ without loosing 'self'
I'm trying to call a function called 'init' in a table from C++. Currently this is what I've got:/smaller>/fontfamily>
int cGameObject::start(lua_State *VM)/smaller>/fontfamily>
{/smaller>/fontfamily>
// first get the table/smaller>/fontfamily>
lua_pushstring(VM, m_TableName); // m_TableName = "GameObject" in this example/smaller>/fontfamily>
lua_gettable(VM, LUA_GLOBALSINDEX);/smaller>/fontfamily>
// get the 'init' function from the table/smaller>/fontfamily>
lua_pushstring(VM, "init");/smaller>/fontfamily>
lua_gettable(VM, -2);/smaller>/fontfamily>
// call the function/smaller>/fontfamily>
lua_call(VM, 0, 0);/smaller>/fontfamily>
return 0;/smaller>/fontfamily>
}/smaller>/fontfamily>
Try using
lua_call(VM, 1, 0)
since you have 1 argument to pass (self is the first argument).
Note t:f() is semantically the same as t.f(t)
Hope that helps,
Russ