Jump to content
  • Advertisement
Sign in to follow this  
Zakwayda

Advice on entity management

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

This may be kind of a long post, so thanks in advance to anyone who is kind enough to read and respond. I'm getting ready to write the entity management code for my game, which is forcing me to deal with issues such as shared objects, smart pointers, handles, etc. Although I've been writing game code for a while, I don't have much experience with the subtler aspects of software engineering, so I'm a little lost as to how to proceed. Basically here is the behavior I'm looking for. All entities will inherit from a base entity class, which will have various virtual functions such as Update() and Render(). The entity manager will maintain a vector<> of Entity*, and will call each entity's update and render functions every frame. When a new entity is needed, it will be requested from the entity manager. The manager will first scan the list to see if there is a currently inactive entity of that type. If not, it will create a new one and add it to the list. In either case, a smart pointer or handle will be returned to the caller referencing the entity. So for example, let's say a bullet hits a wall. It will request a new entity of type Decal from the entity manager, and receive a reference through which it can set the decal's initial state. From then on the decal will take care of itself through its Update() and Render() calls. Then the bullet will mark itself inactive, and will be available the next time an object of type Bullet is requested from the manager. So far so good. But then come the details. The only way I can think of to make this all happen is as follows. The caller requests a new entity from the manager using a named constant, such as ENT_DECAL. The manager traverses the list and calls a virtual GetType() function (or uses c++ rtti functions) to determine if each entity is a decal or not. When it finds one (or creates one), the Entity pointer is dynamic_cast back to a Decal pointer, either by the manager or the caller. Although I don't know that much about software engineering, this doesn't sound like good design to me. From what I've gathered, rtti and dynamic_casts are often signs of bad design, and good class structure and program architecture will usually make them unnecessary. Finally, I have to decide whether to use smart pointers or handles, and figure out a good implemention. Handles seem to have the significant advantage that you can write everything out to a file and then recreate it exactly - I'm not sure how you'd do this with smart pointers. I have more questions, but I'll leave it at that for now. In summary, I guess I'm asking a) how a manager can maintain a list of base objects, but still return objects of a specific derived class on demand, and b) whether smart pointers or handles would be better for managing shared entities. Thanks very much to anyone who read that!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by jyk
The manager traverses the list and calls a virtual GetType() function (or uses c++ rtti functions) to determine if each entity is a decal or not.


Is it possible to maintain separate lists for each object type? So you can say for sure an object belongs to that type if it is in the correct list.


Thermo

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Basically here is the behavior I'm looking for. All entities will inherit from a base entity class, which will have various virtual functions such as Update() and Render(). The entity manager will maintain a vector<> of Entity*, and will call each entity's update and render functions every frame.

I like to store my entities in <map>s, since they sort and search better than vectors. Also then instead of searching for inactive entites (which I assume you're doing so you don't have to remove an entity in the middle of a vector) you can release and create new ones instead. Of course, unless you create and remove entities often enough that the memory hit would outweigh the iterative vector search.
Quote:

So far so good. But then come the details. The only way I can think of to make this all happen is as follows. The caller requests a new entity from the manager using a named constant, such as ENT_DECAL. The manager traverses the list and calls a virtual GetType() function (or uses c++ rtti functions) to determine if each entity is a decal or not. When it finds one (or creates one), the Entity pointer is dynamic_cast back to a Decal pointer, either by the manager or the caller.

I can't offer any advice about the searching, since I always remove and create new objects, but for object creation, look at the Factory pattern. Have the factory return the base object class, and then just static_cast that to the object you want. I'm not a know-all software engineer either, this is just what I do.
Quote:

Finally, I have to decide whether to use smart pointers or handles, and figure out a good implemention. Handles seem to have the significant advantage that you can write everything out to a file and then recreate it exactly - I'm not sure how you'd do this with smart pointers.

Handles are good. Smart pointers are only needed if you plan on creating multiple instances of an object really. For example my message class creates copies of a single message to dispatch to various game objects, so the messages have to be smart pointers so that when objects start deleting the messages, the pointer doesn't become invalid until all copies of that message have been deleted. I don't use smart pointers for everything though.

Share this post


Link to post
Share on other sites
Quote:
Is it possible to maintain separate lists for each object type? So you can say for sure an object belongs to that type if it is in the correct list.


Well, there will be many different Entity subclasses - at least 10. I wouldn't really want to have 10+ separate lists, and iterate through each one for update and render. That would also mean that every time you create a new entity type, you'd have to change the code in the entity manager to support a new list.

I also want to try to reuse entities rather than release and recreate them. There could be a hundred projectiles or decals at any one time, but each will have a limited lifespan. So it would seem wasteful to me not to reuse them.

Thanks for the responses - I will look into the factory pattern. Any other thoughts are welcome!

Share this post


Link to post
Share on other sites
just use an associative array and map your constants to the approbiate list... no prob, you can write a once-and-forever function which deals with this...

Thermo

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!