lua-users home
lua-l archive

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


You kinda suggest the solution in your message subject:

First retrieve "test" (the table), then fetch the function (its member) and
then call it.

Also, since you defined your function as test:fn() [instead of test.fn()]
you need to provide the hidden "self" parameter as the first argument.

I hope this help.

AH.

----- Original Message -----
From: "pion1013" <pion@griffonstudio.net>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Monday, March 31, 2003 9:33 PM
Subject: calling table:function from c/c++


> Hi,
>
> I wrote a short lua program like this:
>
>
> -- test.lua
> test = {}
> function test:fn (num1, num2)
>   return num1 + num2
> end
>
>
> now, I wanted to call this function from c/c++,
>
>
> lua_State *L = lua_open(0);
> lua_baselibopen(L);
> lua_dofile (L, "test.lua");
> lua_getglobal(L, "test:fn");
> lua_pushnumber(L, 100);
> lua_pushnumber(L, 200);
> lua_call(L, 2, 1);
> printf("result : %d\n", (int)lua_tonumber(L, -1));
> lua_close(L);
>
>
> which doesn't work. The program complains about calling nil function.
> My rough guess is that the function "test:fn" belongs to the
> table "test", and not globally known. But, I don't know how to
> correct it.
>
> Can anyone help me, please?
>
>
> Pion
>
>
> p.s. I'm using Lua-4.0.1
>
>
>
>
>