tags:

views:

439

answers:

2

I have for example, a Lua table/object:

bannana

And this Lua table has a function inside it called chew, that takes a parameter

bannana.chew(5)

I have also used SWIG, and have for example a class CPerson:

class CPerson {
    public:
        // ....
        void Eat();
        // ....
};

I can obtain an instance of this object from Lua:

person = engine:getPerson()

What I need to be able to do is the following Lua code:

person = engine:getPerson()
person:Eat(bannana)

Where person:eat would call the chew function in the bannana table, passing a parameter.

Since CPerson is implemented in C++, what changes are needed to implement Eat() assuming the CPerson class already has a Lua state pointer?

Edit1: I do not want to know how to bind C++ classes to Lua, I already have SWIG to do this for me, I want to know how to call Lua functions inside Lua tables, from C++.

Edit2: The CPerson class and bannana table, are both general examples, it can be assumed that the CPerson class already has a LuaState pointer/reference, and that the function signature of the Eat method can be changed by the person answering.

A: 

Maybe "Simpler Cpp Binding" will be helpful.

Boris
this doesnt help me, all this tells me is how to pass literals, how to bind C++ to lua with luna, and how to pass C++ objects around with lua.
Tom J Nowell
What I asked for, is how to call a method on a lua table from C++, I already have SWIG to bind C++ to lua for me
Tom J Nowell
what is the exact signature of the CPerson's Eat method (generated by SWIG, I suppose ?) From what I undersand, what you want is the CPerson class to get the 'bannana' lua table, right ? I think for that you need to get a handler on the corresponding luaState ...
phtrivier
its an example, the person who answers the question is free to meddle with the definition of CPerson::Eat as is necessary, it is safe to assume the LuaState has already been passed to the CPerson object beforehand, and that only one such LuaState is present.
Tom J Nowell
+2  A: 

Ignoring any error checking ...

lua_getglobal(L, "banana"); // or get 'banana' from person:Eat()
lua_getfield(L, -1, "chew");
lua_pushinteger(L, 5);
lua_pcall(L, 1, 0, 0);
uroc