lua-users home
lua-l archive

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


Hi,

you define the function using the "method form", by using a : between
the table and function name so there's an additional hidden first
parameter named "self" (like the this pointer in C++).

You have to push your table as first paramter onto the stack or
if you don't need instance related information, just create the
function with a "." like

function myTable.saysomething( a, b )

Regards,

  Michael Flad

--
Fantastic Realms Interactive GmbH - Germany
Phone: +49 (0)7121 / 983 7820
Fax:   +49 (0)7121 / 983 7830


> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of
> luigi@realityslip.com
> Sent: Friday, March 07, 2003 10:38
> To: Multiple recipients of list
> Subject: Accessing table methods via C++
>
>
> 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
>