Control of Luabind objects

Started by
8 comments, last by Josh Klint 12 years, 10 months ago
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:
--“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


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.

10x Faster Performance for VR: www.ultraengine.com

Advertisement
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.
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.

10x Faster Performance for VR: www.ultraengine.com

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?

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

10x Faster Performance for VR: www.ultraengine.com


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?

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




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.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
The list is like this:

class Entity
{
std::list<Entity*> kids;

...(+some other shit)

};

10x Faster Performance for VR: www.ultraengine.com

I read the luabind documentation on object slicing. I have my classes and binding set up as follows:

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

Is that all I have to do to make luabind consistently use the same luabind::object? It doesn’t appear to have any effect.

10x Faster Performance for VR: www.ultraengine.com

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.

function AttackState:__init(fsm, memory)
AI.PathMoveState.__init(self, fsm, self)
self.memory = memory;
self.health = 100;
...
end

Then in my other methods I can do self.health = self.health - 10; or whatever I need to do.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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.

10x Faster Performance for VR: www.ultraengine.com

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

10x Faster Performance for VR: www.ultraengine.com

This topic is closed to new replies.

Advertisement