Jump to content
  • Advertisement
Sign in to follow this  
c_olin

Luabind and non garbage collected collections

This topic is 2607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:



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


Share this post


Link to post
Share on other sites
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.

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:

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!