Jump to content
AnthonyPython

what is the best way to expose C++ for LUA?

Recommended Posts

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 :)

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...