Sign in to follow this  
c_olin

Luabind and non garbage collected collections

Recommended Posts

The majority of my game's logic code is written in Lua including the entire entity system. I've been storing my entities in Lua tables and it has been working fine. However, I need to hold very large lists of entities (hundreds of thousands). Whenever I have an extremely large table of Lua objects the garbage collector starts to interfere with performance.

There should be a way around this because the entities themselves should only be deleted when explicitly removed from the list or the list itself is destroyed. I tried to achieve this like the following:

[code]

class LuaList {

public:

~LuaList () {
deleteList(&elements);
}

void insert(luabind::object* object) { elements.push_back(object); }

std::list<luabind::object*>& getElements() { return elements; }

private:

std::list<luabind::object*> elements;

};


class_<LuaList >("LuaList ")
.def(constructor<>())
.def("insert", &LuaList ::insert, adopt(_2))
.property("elements", &LuaList ::getElements, return_stl_iterator),
[/code]

... but when I execute the insert function within Lua it says that the arguments do not match. Has anyone else done something like this? Any help is appreciated.

Thanks


Share this post


Link to post
Share on other sites
Are all your entities of a "type" or just lua tables? Not sure why it doesn't work. One thing you could try is making a c++ base class and inheriting from that:

[code]class Entity
{
Entity() {}
virtual Entity();
};

class EntityWrapper: public luabind::wrap_base
{
};

void LuaList ::insert(Entity* object) { elements.push_back(object); }[/code]

Are you using classes in lua?
[code]luaList = LuaList();

function EntityA:__init()
Entity.__init(self)
end

e = EntityA();
luaList(e);[/code]

You could tell luabind to use shared_ptr, you wouldn't have to worry about using the adopt policy then perhaps.

Share this post


Link to post
Share on other sites
[quote name='Nanoha' timestamp='1306917889' post='4818207']
Are all your entities of a "type" or just lua tables? Not sure why it doesn't work. One thing you could try is making a c++ base class and inheriting from that:

[code]class Entity
{
Entity() {}
virtual Entity();
};

class EntityWrapper: public luabind::wrap_base
{
};

void LuaList ::insert(Entity* object) { elements.push_back(object); }[/code]

Are you using classes in lua?
[code]luaList = LuaList();

function EntityA:__init()
Entity.__init(self)
end

e = EntityA();
luaList(e);[/code]

You could tell luabind to use shared_ptr, you wouldn't have to worry about using the adopt policy then perhaps.
[/quote]

Thanks for the reply. The issue is that my entity class is defined in Lua. My goal is to have 100% of the logic decoupled from the C++ engine. Maybe I could have a class type in C++ (say NonGarbageCollected) and expose a collection class containing those types and have the Entity class in Lua inherit from it.

Share this post


Link to post
Share on other sites
[quote name='c_olin' timestamp='1306941746' post='4818299']
Thanks for the reply. The issue is that my entity class is defined in Lua. My goal is to have 100% of the logic decoupled from the C++ engine. Maybe I could have a class type in C++ (say NonGarbageCollected) and expose a collection class containing those types and have the Entity class in Lua inherit from it.
[/quote]

I reliase I missed out a line of code at the begining,

it should have been:

[code]luaList = LuaList();

class 'EntityA' (Entity) -- Forgot this bit

function EntityA:__init()
Entity.__init(self)
end

e = EntityA();
luaList(e);[/code]

It seem you came up with the same idea anyway though :) (Inheriting from a base class defined in c++).

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