Advertisement

Need help with Lua and ToLua

Started by March 27, 2003 04:10 PM
4 comments, last by Impz0r 21 years, 7 months ago
Hi, I need a litle bit of help to pointing out how to call a method from a class which is imported by ToLua. I''ll try to point out what i mean This is my litle unuseful lua script:

-- create the example class
MyClass cMySupahClass:new();
-- the function i like to call from c++
function MyClass:OnUpdate(DeltaTime)
 -- do some useful stuff right here...
end
MyClass:delete();
 
So i need to know how i could get the name of the new created instance (here MyClass) because i think i need these name to get access to the table where are all the functions stored. Hope i''m not totaly wrong here *g* Ah and yes thats what i''m asked for, how could i call the "MyClass:OnUpdate()" function from c++?! Thanks alot, Mfg Impz0r
Stay Evil & Ugly!
I assume your first statement is meant to be:
MyClass = cMySupahClass:new();
rether than
MyClass cMySupahClass:new(); <- that is an invalid LUA statement.

what you are then doing in the next line is adding another method to the MyClass instance of cMySupahClass. While I guess it is valid in LUA, I doubt that is what you are trying to do really. If that class has a OnUpdate method in your C++ code, then what you are doing is completly wrong, since you are re-writing the method, so that it never actually gets through to the c++ side.

I am not entirely sure what you are trying to do, however if I guessed correctly, I think what you want in your situation is to simlpy define a function in LUA without using tables, and then call that function from C++ with the name hard-coded. If that is not appropriate with what you are trying to do, then please post some more information about what you want to achieve.

[edited by - Tylon on March 27, 2003 8:47:50 PM]

[edited by - Tylon on March 27, 2003 8:52:45 PM]
Advertisement
Ah sorry, I think I see what you are trying to do. Ill leave my post there above, just incase that is the case rather than this.

You can do that in 2 ways, an easier but less efficient, and a more difficult but more efficient way:

lua_dostring(VM, "MyClass:OnUpdate()");
or if the var name isnt fixed then something like
lua_dostring(VM, classname + ":OnUpdate()");

And the difficult way is to access the stack using the lua stack functions, (get global, get table etc) and pushing the arguments on, calling the function etc.

This is more efficient, since there is no need for compilation, however the first method is easier to read i guess.

Edit: Actually, you are better off using the second method if there are return values involved, since you can get it directly, while with the first method, the only way i can think of is to put the return in a global variable by calling it like
lua_dostring(VM, "temp = MyClass:OnUpdate()"); and then accessing the temp variable, however there is quite a bit of overhead involved this way. The second method is definitly the proper way to do it, ie access the stack.

Either way, if you gave the cMySupahClass an OnUpdate method in C++, then take it out! If your going to make it redundant in LUA by over-writing it, then it doesn't have to be in the C++ code. Only leave methods in the C++ side that you want executed on the C++ side.
If you want OnUpdate code to be executed on both the LUA side and the C++ side, then call them different methods, and implement each method so that it calls the equivalent on the other side.


[edited by - Tylon on March 27, 2003 9:05:27 PM]
Hi,

Thanks Tylon for your awnser and ye sorry for my bad example So what i''m trying to achieve is to call an event method which is only declared in a lua script and only there (but i know how the event method is named, because i like to call it from c++ .

Note: Ye i meant "MyClass = cMySupahClass:new();"

Hmm i allready tried something similar to your second way but i cant get it to run. Here''s the code but something went wrong because i get an exception and i cant figure out how...
lua_getglobal(LuaVm, "MyClass");// Push the table memberlua_pushstring(LuaVm, "OnUpdate");// Get the function onto the stacklua_rawget(LuaVm, -2); // this is how i get the exception..grmbl// Push the Self pointerlua_getglobal(LuaVm, "MyClass");// push some argumentlua_pushnumber(LuaVm, fDeltaTime);// call the methodlua_call(LuaVm, 2, 0);// clean some stufflua_pop(LuaVm, 1); 


Another question about lua and ToLua, is it possible to create a class in a lua script without these new() methode?!


Thanks alot

Mfg Impz0r
Stay Evil & Ugly!
quote: Original post by Impz0r
Another question about lua and ToLua, is it possible to create a class in a lua script without these new() methode?!


Yes, download http://www.inf.puc-rio.br/~roberto/luabook1.pdf

There is a chapter there about how to implement classes and objects with inheritance etc purely in LUA.

When you are using ToLUA to export classes to LUA, it is almost the same thing, except the table contains tagged userdata, which is told to call the associated operations in C++. So basically when you call MyClass::new(), control is passed on to C++, where an instance of MyClass is dynamically created, and its address stored back in LUA. When you call a method on that instance, and the appropriate method is called on the object pointed to by that pointer, again in C++.

As for the rest of your question, ill try to have a look at it once I get home .
Hey,

Thanks Tylon, i hope you could give me to the other part of my question an answer


Mfg Impz0r
Stay Evil & Ugly!

This topic is closed to new replies.

Advertisement