Luabind to access classes.

Started by
2 comments, last by JesseWerner 13 years ago
Hi, I want to have a class representing a game object and I want to be able to have access to it from both lua and c++.

Right now I parse my scene file and call this code to create objects:

luabind::object myObject = globals(L)[name]["__init"]();
GameObject *gameObject = luabind::object_cast<GameObject*> ( myObject );


This does call the luabind object and return the game object. I have verified this by changing the "name" value in my lua constructor and printing it out in the GameObject update function on the c++ side. The problem is I dont know how to continue to call lua functions on this object. Does anyone have any ideas?


EDIT: So since the lua __init function called above can change members on the game object, I have no idea why this wouldnt work:

luabind::object myObject = globals(L)[name]["__init"]();
myObject["update"]();

Calling update makes it crash. And if i call update in the constructor of the derived lua class, it calls the c++ GameObject update D=
Advertisement
After searching exhaustively for the answer to this, I have found no results. I have decided to ditch luabind and create my own structure for binding classes on top of lua.
If anyone has any suggestions or resources let me know =)

After searching exhaustively for the answer to this, I have found no results. I have decided to ditch luabind and create my own structure for binding classes on top of lua.
If anyone has any suggestions or resources let me know =)


__init requires a self parameter and returns nothing.
Update probably requires a self parameter too.

If you really want to create objects this way, I suggest something like this:

// Will call the global function called "name", which, from lua-side, acts as you'd expect a "obj = new Object( )" to work
luabind::object myObject = luabind::call_function<luabind::object>( name )[luabind::adopt(luabind::_return);
myObject["update"]( );


The function calls might not be exactly right, but should give you an idea.

However, I've had a few crashes lately with luabind not keeping references to objects alive which were created lua-side but stored C++-side. Although this was also using lua-side specialisation of classes, so your case may not be affected.
Is it possible to pass a self parameter to lua from the c++ side?

Also so you must make a global function for each of your lua classes?

Edit: Your post convinced me to play around with is some more. I managed to get it to work with aggregation but I cant get inheritance to work.

In lua I have:


class 'Derived'

function Derived:__init(userdata)
self.gameObject = userdata
userdata:setName("name set from lua")
self.gameObject:printName()
end


That works just fine. I can access my game object through the lua "gameObject" field. But does anyone know why this wouldnt work:

class 'Derived' (GameObject)

function Derived:__init()
self:setName("name set from lua")
self:printName()
end


I read somewhere that you can use super to call the base class constructor. That throws errors. I tried directly calling __init and that throws errors as well.


EDIT: I was wrong, it doesnt work as I previously thought. I can edit the data in the constructor but I cant call any other functions from the lua object even though i set "self.gameObject = userdata". I must be missing something...

This topic is closed to new replies.

Advertisement