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

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

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

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002233
×