Sign in to follow this  
JoshKlint_34394

Control of Luabind objects

Recommended Posts

JoshKlint_34394    1440
Is it possible to ensure luabind always uses the same Luabind::object for any given C++ object? I am having problems because luabind is creating new luabind::objects and returning those, without the information I attached to the “real” object.

Example:
[code]--“entity” is the luabind::object created for the C++ object
entity.health= entity.health -1

for child in entity.kids do —“kids” is an std::list exposed to LuaBind
child.health = child.health-1 –Can’t access this because luabind creates a new luabind::object and returns it, instead of returning the one I created and pushed the “health” value onto
end[/code]

I would like to have a callback function LuaBind uses to get the actual luabind::object I created and pushed onto the stack when I added values. Right now, any values I add to a luabind::object will get lost if a function returns an object, because a new empty luabind::object will be created.

Share this post


Link to post
Share on other sites
_the_phantom_    11250
Posting the C++ binding code, along with output and what you expect might be helpful as well.

My inital thoughts however are that the objects you are adjusting aren't references to the real objects, so the object values are being copied to lua but never copied back again. I suspect there is a way around this but I can't be sure atm.

Share this post


Link to post
Share on other sites
JoshKlint_34394    1440
Sorry, I should have explained that "health" is not a member of the Entity class. Luabind allows you to add values to the object that don't exist in the C++ class. So you can add "health", "mood", "ammo", and other gamic stuff that doesn't belong in the core engine.

The problem is if I set these script values, and then call a function somewhere else that returns this entity, the returned luabind::object does not have all the script values I added to it, even though it corresponds to the same C++ object.

I always use pointers for Entity objects.

Share this post


Link to post
Share on other sites
JoshKlint_34394    1440
Here's another example. The printed output, with one child in the entity kids, is:
No Name
Joe
No Name

"name" is not a C++ class member.

It seems a new luabind::object is created each time a C++ object is returned from a function?

[code] for child in self.kids do
if child.name==nil then
Print("No Name")
child.name="Joe"
Print(child.name)
else
Print(child.name)
end
end

for child in self.kids do
if child.name==nil then
print("No Name")
else
Print(child.name)
end
end[/code]

Share this post


Link to post
Share on other sites
SimonForsman    7642
[quote name='Josh Klint' timestamp='1305960660' post='4813778']
Here's another example. The printed output, with one child in the entity kids, is:
No Name
Joe
No Name

"name" is not a C++ class member.

It seems a new luabind::object is created each time a C++ object is returned from a function?

[code] for child in self.kids do
if child.name==nil then
Print("No Name")
child.name="Joe"
Print(child.name)
else
Print(child.name)
end
end

for child in self.kids do
if child.name==nil then
print("No Name")
else
Print(child.name)
end
end[/code]
[/quote]


What kind of std::list are you using for kids ? std::list<luabind::object> should work if its expose correctly to lua. If you are using something like std::list<Child> and then have the child class exposed to lua aswell you should get the problem you're having.

Share this post


Link to post
Share on other sites
JoshKlint_34394    1440
I read the luabind documentation on object slicing. I have my classes and binding set up as follows:

[code]class Object
class Object_wrap : public Object, public luabind::wrap_base {};

class Entity : public Object
class Entity_wrap : public Entity, public luabind::wrap_base {};

class Model : public Entity
class Model_wrap : public Entity, public luabind::wrap_base {};

class_<Object,Object_wrap>("Object")
class_<Entity,Entity_wrap,Object>("Entity")
class_<Model,Model_wrap,Entity,Object>("Model")[/code]
Is that all I have to do to make luabind consistently use the same luabind::object? It doesn’t appear to have any effect.

Share this post


Link to post
Share on other sites
Nanoha    2682
I don't know if this helps (I didn't read all the posts) but when doing classes in lua I have to put "self." infront of my class variables and then they are unique to each object. Its working a bit like the "this" pointer in c++. This may be something added by luabind.

[size="2"][code]function AttackState:__init(fsm, memory)
AI.PathMoveState.__init(self, fsm, self)
self.memory = memory;
self.health = 100;
...
end
[/code]
Then in my other methods I can do self.health = self.health - 10; or whatever I need to do.

[/size]

Share this post


Link to post
Share on other sites
JoshKlint_34394    1440
The issue is talked about here:
http://www.rasterbar.com/products/luabind/docs.html#slicing

It's documented, to the minimum extent that you could classify their vague description as "documentation".

I'm pretty shocked that such a basic issue is apparently obscure knowledge. This makes me question whether I should be using LuaBind at all. This just tells me people never really get into any depth with it.

Share this post


Link to post
Share on other sites
JoshKlint_34394    1440
This illustrates the problem nicely, except I am not even trying to add a Lua class onto the C++ class, I just want to preserve the Lua data:
http://old.nabble.com/Slicing---Object-identity-problem-td30319053.html

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this