AnthonyPython Posted February 26, 2015 Posted February 26, 2015 I found this http://stackoverflow.com/questions/19381301/how-to-expose-c-functions-to-a-lua-script which is helpful, but what is your all's opinion on this subject? Quote OS: Windows 10 Pro CPU: i3-10100 CPU @ 3.60GHz GPU: NVIDIA 2060 Super - 8 GB RAM: 32 GB
Guppy Posted February 26, 2015 Posted February 26, 2015 http://www.leadwerks.com/werkspace/blog/138/entry-1288-pushing-complex-lua-tables-from-c/ In the article I do show how to use the function but it's quite easy; (this is from a now abandoned UI kit for Leadwerks C++/Lua ) luaInterface::luaInterface(LeadwerksUI* _gui):gui(_gui){ //ctor typeMap guiObj={ {"loadCore",new luaMemberFunction<luaInterface,&luaInterface::loadCore>(this)}, {"loadLayout",new luaMemberFunction<luaInterface,&luaInterface::loadLayout>(this)}, {"findWidget",new luaMemberFunction<luaInterface,&luaInterface::findWidget>(this)}, {"findWidgets",new luaMemberFunction<luaInterface,&luaInterface::findWidgets>(this)}, {"removeWidget",new luaMemberFunction<luaInterface,&luaInterface::removeWidget>(this)}, {"showMouse",new luaMemberFunction<luaInterface,&luaInterface::showMouse>(this)}, }; luaTable *table=new luaTable(guiObj); table->push(); Leadwerks::Interpreter::SetGlobal("myGUI"); delete table; } This binds member functions to an lua table giving a faux object in lua Breakdown; new luaMemberFunction<luaInterface,&luaInterface::loadCore>(this) luaInterface - class name &luaInterface::loadCore - member function note the format, it's important this pointer to the actual instance you want the member function to be called on - in this case it's "this" because I wanted it to call it's own member functions but it could be any instance of the given class. I thougth about posting the actual functions I used above but they honestly do not make much sense so I cooked up a new one to show you the ropes; int luaInterface::findWidget(lua_State *L){ //I used a single macro for these lines as you need to write them often, I can post if if you like int argc=Leadwerks::Interpreter::GetStackSize(); if ( argc != 1 ) { std::cerr << "Wrong argument count for findWidget" << std::endl; Leadwerks::Interpreter::PushBool(false); //Put a the bool value false on the stack to let the user know he screwed up return 1; //This lets lua know that we returned 1 argument } std::string parameter1= Leadwerks::Interpreter::ToString(1); //or ToBool, etc depending on what parameter you expect. Leadwerks::Interpreter::PushBool(true); /*Look at the source for Leadwerks to figure out what types you can push - you can even return a complex lua table as outlined above */ return 1; /*once again tell lua that how many parameters we returned if you get this wrong you get crashes or memory leaks*/ } int luaInterface::hideMouse(lua_State *L){ assertArgc(!= 0, "hideMouse") //the first 6 lines of the other function rolled into a nice discrete macro //We dont expect any parameters gui->showMouse(false); //you can call any member function of the object even setting variables for later use return 0; //Nor do we return anything - tell lua the stack is empty } It may seem a little daunting at first but once you get the hang of it it's quite easy 1 Quote System: Linux Mint 17 ( = Ubuntu 14.04 with cinnamon desktop ) Ubuntu 14.04, AMD HD 6850, i5 2500k
Rick Posted February 26, 2015 Posted February 26, 2015 You can also expose stuff via a DLL if you don't want to do it from your game's exe directly. http://www.leadwerks.com/werkspace/topic/11875-more-precise-timer/page__st__20#entry87654 This would make it a little more modular and easier to distribute to others if that's something you are interested in doing. Quote
Josh Posted February 26, 2015 Posted February 26, 2015 This is what I use: http://www.leadwerks.com/werkspace/files/file/216-tolua/ Quote Let's build cool stuff and have fun.
AnthonyPython Posted February 26, 2015 Author Posted February 26, 2015 Thanks a bunch guys Quote OS: Windows 10 Pro CPU: i3-10100 CPU @ 3.60GHz GPU: NVIDIA 2060 Super - 8 GB RAM: 32 GB
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.