jordanvrtanoski/luacpp

Question: How can I call custome methods on a methaObject?

Closed this issue · 3 comments

As the title already says? Is it possible to call in a lua-script custome methods of the CPP-Class deriving from LuaMetaObject?

Or have I go over the library functionality?

Thanks for your help!

Creating a callable method is a little bit more work since Lua is a C library and we need a bridge between the C and C++. LuaMetaObject is an example of a bridge that supports function calls.
The LuaMetaObject is a base class that can be used to extend when creating objects that can be manipulated by the Lua script engine. The class provides a method Execute which is called whenever the Lua script invokes the meta-objects. For example, if the meta-object is stored in the context under the name "foo", the Lua script foo() will invoke the object and the control will be passed to the Execute method. The method will receive the current Lua state and you can extract the arguments passed to the call from the state. The LuaObject can also be stored in a Map which will allow you to create something similar to an object in Lua. See the TestLuaMetaObject for more details on how the class can be used.

Also, the LuaTUserData which is the parent of the LuaMetaObject provides a method AddMetaFunction to register a C function in the meta table, so you can use C++ friend functions (same as LuaMetaObject is doing it for the table callbacks __index, __newindex and __call. Study the mechanism that is used by the friend function u_call to understand how LuaMetaObject is handling the Execute method, and you can replicate this behavior for your methods.

Ok, thanks a lot, I've solved it for the moment by implementing custome c-libraries.

Sorry, a little offtopic, but do you know, if there are differences in performance between luaL_loadfile and luaL_loadstring? I couldn't find anything about it and I'm concerned about memory for large files and if it is a difference at all or lua has to load anyway the complete file into memory.