How to call a Lua function which has been defined in a table ?

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

How to call a Lua function which has been defined in a table ?

Nicolas Goles-2
Hello!,

I'm trying to call a Lua Function which has been defined inside a table:

table = {}

function table:test()
    print("Lua Rocks")
end


How can I call that function from C++ ? I'm trying to use luabind::call_function resulting in:

terminate called after throwing an instance of 'luabind::error'
  what():  lua runtime error

I tried :

            luabind::call_function<void>(L "table:test", NULL);

Help is appreciated :)

Thanks.

_Nicolas


--
Nicolas Goles Domic
Founder & Lead Developer
Gando Games


------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: How to call a Lua function which has been defined in a table ?

Tony Kostanjsek
hi Nicolas,

try to pull the function from the table first before calling it:
luabind::object func = luabind::globals["mytable"]["func"];
luabind::call_function<void>(L, func);
In Lua, this would be the dot notation: mytable.func()
If it is a method with implicit "self" parameter then you need to specify it as the first one in the call:
luabind::object myobject = luabind::globals["myobject"];
luabind::object func = luabind::globals["myobject"]["func"];
luabind::call_function<void>(L, func, myobject);
This would be equivalent to myobject:func() in Lua (note the colon). Add more parameters as required.
Don't forget to check for nil values.
Something like this should work.
Best,
Tony 





Von: Nicolas Goles <[hidden email]>
An: [hidden email]
Gesendet: Montag, den 13. Dezember 2010, 19:39:49 Uhr
Betreff: [luabind] How to call a Lua function which has been defined in a table ?

Hello!,

I'm trying to call a Lua Function which has been defined inside a table:

table = {}

function table:test()
    print("Lua Rocks")
end


How can I call that function from C++ ? I'm trying to use luabind::call_function resulting in:

terminate called after throwing an instance of 'luabind::error'
  what():  lua runtime error

I tried :

            luabind::call_function<void>(L "table:test", NULL);

Help is appreciated :)

Thanks.

_Nicolas


--
Nicolas Goles Domic
Founder & Lead Developer
Gando Games



------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user