• Advertisement
Sign in to follow this  

Entity management + Reusing.

This topic is 3735 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

Hi all, Sometime ago (about a year), I was discussing how to build a entity manager, an easy one, with some folks around game dev. They were really helpfull and we shared some nice ideas about it. I built my entity manager based on the things we talk but this days I have some needs with the entity manager and it is reusing entites. My entity manager now is just 1 std::map< entity pointer, entity pointer>, so I use the pointer intsance as id to search for it. As you can see, I create/delete entites as I need, don´t need, something that is not good. So, I need some type of reusing mechanism. I have thought about pools, but for this I will need to manually add a pool for every entity type I have. With pools it will be ease to start with say for example a given number of entities preallocated and then when newing in the scene entity getting a free one from there (writing an allocator for it or overriding new). I know I could use some type of registering mechanism in the entity manager something like std::map<EntityType, pool>. So when a new type of entity is written I could have a macro or something doing a g_EntityMgr.AddEntityType("Light",100), being the first the type of the entity and the second the number of preallocated lights to use when newing a new one. This way when I do a EntityMgr.createEntity("Light"), I will be getting a pointer into this pool. So, to get a intance from this pool we will do from the allocator/new or whatever m_Pools["Light"].GetFreeOne() Another thing is that my entities could new in the initialization some more things, so I have thought that I could something like a clean method that the entity manager will run on the entity when it is "dirty", to avoid newing this subparts of the entity again. Well, this are some thoughts, I just would like to share with you this and see if anyone has other ideas, better ways to do it, etc... Thanks in advance, HexDump.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by HexDump
As you can see, I create/delete entites as I need, don't need, something that is not good.


Why would that be a bad thing? Did you run some tests and saw that it was too slow or something? Or is this just a gut feeling you have? From personal experience, I had no trouble with such an approach - in my particular situation, of course.

Anyway, it depends. If constructing an object is expensive, and needs to happen frequently, then object pooling can be a good optimization. However, not every entity type will benefit from it, depending on how your game deals with entities. Usually, players, triggers, doors, and so on, are created when loading a level, and perhaps dynamically created in specific situations. Projectiles and particles on the other hand, are created much more frequently, so these might be candidates for pooling or other optimizations. However, I'd run some tests first to see if it's really a bottleneck before spending/wasting time optimizing it.

Share this post


Link to post
Share on other sites
Well, you could be right, anyway, I will try to make it with pools :D.

Thanks for the answers,
HexDump.

Share this post


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

  • Advertisement