Calling functions in tables in 4.0

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Calling functions in tables in 4.0

Ken Paulson
G'day!

If I have something like:

MyStuff={};
MyStuff.Data="whatever";
function MyStuff.MyFunction()
  do stuff
end

I can call MyFunction from within the lua script without a problem as MyStuff.MyFunction(), but when I try it from C I get an error (nil value).

If I change the function to MyFunction (not within a table), then it works fine. I'm sure it's just a dumb newbie mistake, so I'd appreciate if someone can point out what I'm doing wrong.

Stay Casual,

Ken


Reply | Threaded
Open this post in threaded view
|

Re: Calling functions in tables in 4.0

Wim Couwenberg-2
Ken Paulson wrote:

> If I change the function to MyFunction (not within a table), then it
> works fine.  I'm sure it's just a dumb newbie mistake, so I'd
> appreciate if someone can point out what I'm doing wrong.

In C the procedure is the same as in a Lua script:
-  extract the function from the table
-  call the function

/* get (global) table */
lua_pushstring(L, "MyStuff");
lua_getglobal(L);

/* get function */
lua_pushstring(L, "MyFunction");
lua_gettable(L, -2);

/* drop table */
lua_remove(L, -2);

/* call function (0 args, 0 results) */
lua_call(L, 0, 0);

Bye,
Wim