Sign in to follow this  

Entity interactions with a single container

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

I've been using one container per entity subclass in order to support efficient checking for interactions like character-on-item or projectile-on-character. It works okay, but I have to remember to update and draw each container that I add. For this reason I may want to use a single container of base entity pointers in future. But on the interactions between the subclasses, I'm stumped. Containers of entity ID numbers are just a variation of my current scheme with more bookkeeping, plus I can't do quick random access if the main container is a list, and have to slowly find a free slot for a new entity if the main container is an array or vector. And runtime type-checking is just...no. [Edited by - Iori Branford on November 18, 2008 3:06:07 PM]

Share this post


Link to post
Share on other sites
Remembering to update and draw every container is not that hard, is it? In terms of code, that's two additional line for every container (that hardly matter in the grand scheme of things) and in terms of brainpower you'll notice that you're not updating/rendering something the second youtest it.

As a side note, you don't need to find a free slot for a new entity in a vector: you can keep a stack of free identifiers so you can reuse one in constant time.

Share this post


Link to post
Share on other sites
Yeaaahhhh ... I just went through the same dilemma. I spent weeks on it. Don't make the same mistake.

Honestly if this starts hindering you and you're stuck, just do something. I'm all about figuring out how to do something the best way you can imagine (even if you're wrong), and looking for information to help, but you eventually have to move on when you can't find the best way.

I think ultimately in this case there are no best ways. You kinda have to figure out what you need. In my case, I started with multiple containers of like objects. I needed collision detection, so each object just sent the collision detection system an unrelated bounding primitive. Once the collision detection system detected a collision, I needed some way to decide what to do.

Well, that should be easy, I'll just have a callback for each different object type and it can do whatever it needs to. Problem solved! Then I tried to implement it... oops. Turns out each object needed to know the type of object that hit it anyway to figure out what to do... so we're back to needing a way to identify each object type at run time.

I considered visitor pattern designs, multi-dispatching functionality, and more... ultimately I settled on a simple object identification system (strings in my simple case, but an enum could just as easily have sufficed), which led me back to "why do they need separate containers?". Now I have one container of all the base objects.

Of course this ended up being the ideal way to do things in my game. What about yours? It may be that logic is easier to handle if the different types are in separate containers. Maybe a component based system is what you need... (a whole new post would have to be made to explain why a component based system wasn't the best way for me... the only thing that benefited me in that regard was the bounding-primitive "component" that I used).

Not sure if that helped... but just keep working on it, and change it once you find you need to - cause your current method might be exactly what you need.

Cheers
-Scott

[Edited by - popsoftheyear on November 18, 2008 4:54:30 PM]

Share this post


Link to post
Share on other sites
I'll put in a vote for a single container of entities, but as has been said, the best thing to do is probably just to pick an approach and go with it (you'll find out soon enough whether or not it was a good choice).

Personally, I've found a component-based approach to be extremely effective. For me this approach has been much more successful than multiple containers of entities, or a single container of entities related through inheritance. YMMV, of course.

Share this post


Link to post
Share on other sites

This topic is 3311 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this