lua-users home
lua-l archive

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


Hello,

I'm trying to access call a table's method from C++ by manipulating the
stack.  Seems fairly straightforward after reading the manual, in fact it
works fine but I oddly need to push an extra parameter for the function.
I'm probably not manipulating the stack properly and I was hoping someone
could spot what's wrong.

Here's an example:

[LUA CODE]

myTable = {}
function myTable:saysomething( a, b )
    print( "myTable says: " .. a .. " " ..  b )
end

[C CODE]
// luaVM is an open lua_State
lua_getglobal(luaVM, "myTable");
lua_pushstring(luaVM, "saysomething");
lua_gettable(luaVM, -2);

lua_pushstring(luaVM, "param UNKNOWN!!");
lua_pushstring(luaVM, "param a");
lua_pushstring(luaVM, "param b");
lua_call(luaVM, 3, 0);

[LUA OUTPUT]

myTable says: param a param b

Theoretically I should only push two string parameters and call lua_call
with 2 instead of 3 as nargs.  Yet by doing so I get an error: "attempt to
concat local value 'b' (a nil value)" which seems to indicate that I've only
passed one parameter to the function.  As it is, I have no idea what happens
to the unknown (first parameter) I push on the stack and it doesn't seem to
be affecting execution in any way.  I'm confident it's a mistake with my
table method calling as it works fine with normal global functions.

Thanks for reading :-)

Luigi Rosso
Lead Developer
RealitySlip
http://www.RealitySlip.com