1
\$\begingroup\$

If I have class

class Game {
    public:
        Game() { level = new Level(); }

    private:
        Level *level;
        lua_State *L;
}

Where the level class has member functions such as "addObject()", how can I use that instance of the level class to bind addObject into the lua state?

The lua file may contain:

addObject("Player", 100, 100)
addObject("Enemy", 200, 100)

So to clarify, the 'Game' class contains a class 'Level' to manage things like the objects + there movement etc... Then I want to call a lua script to load the current level, which will bind the instance of 'Level' in the 'Game' class so it can call addObject() from the lua code.

I believe this is possible via LuaBind however I'm not keen on the boost dependencies, I did come across https://stackoverflow.com/questions/32416388/how-to-register-member-function-to-lua-without-lua-bind-in-c however method 1 seems to reply on 'Game' being a singleton and on method 2 I failed to bind a member method, only a normal function. If all else fails I will revert to LuaBind with it's awful dependencies.

\$\endgroup\$
1
\$\begingroup\$

Use Luna for C++ object binding to Lua. It is essentially a single header file containing helper functions to bind a C++ class to Lua. The previous version also contains instructions as to how to get it working. The usage is very simple, but you have to write boilerplate code, a wrapper class for each of your classes you wish to bind.

\$\endgroup\$
  • \$\begingroup\$ Okay I managed to bind a class, so to create a wrapper would I have to use a singleton like pattern so from the wrapper class I can access the instance of Level? \$\endgroup\$ – matthew9090 Dec 23 '15 at 14:27
0
\$\begingroup\$

Okay, you can do this using user data, simple as pushing a pointer of Level onto the stack using lua_pushlightuserdata. Then getting the user data off the stack when the get_object function is called:

Level *lvl = (Level*)lua_touserdata(L, 1);
lvl->addObject(...);

http://www.gamedev.net/page/resources/_/technical/game-programming/the-lua-tutorial-r2999

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.