Luabind and non garbage collected collections

Started by
2 comments, last by Nanoha 12 years, 10 months ago
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:



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


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


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

class Entity
{
Entity() {}
virtual Entity();
};

class EntityWrapper: public luabind::wrap_base
{
};

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


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

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

e = EntityA();
luaList(e);


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

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


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:

class Entity
{
Entity() {}
virtual Entity();
};

class EntityWrapper: public luabind::wrap_base
{
};

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


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

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

e = EntityA();
luaList(e);


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


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.

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.


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

it should have been:

luaList = LuaList();

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

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

e = EntityA();
luaList(e);


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

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

This topic is closed to new replies.

Advertisement