• Advertisement
Sign in to follow this  

Storing all objects within a Level

This topic is 3294 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'm working on a simple 2d platformer and so far I can render the player, load the level's background and jump around a bit. I have a basic object class which has a default constructor to load all the data needed to simply draw the object to the screen and eventually add in some physics for the object. Now comes time to load in these objects. I was planning on using some sort of ADT like a vector as a member of my CLevel class to store all of the current objects in the level. These objects would be loaded through an INFO file that will be alongside the background and other level assets. One downside is I was planning on using inheritance to define what type of object it was going to be (powerup, bullet, enemy) so they can have their specific update functions with their specific variables each would need. I don't think doing something like.. vector<CObject> objectManager; would be able to do it because I would lose all that extra data associated with a specific type of object. Can anyone give me some advice on a way to approach this aspect of programming a level?

Share this post


Link to post
Share on other sites
Advertisement
Why do you need to store all the data in a level together? This loses type information and, in C++, causes memory management issues, and I don't really see a sensible benefit to it.

Why not separately store power-ups in one container, bullets in another, and enemies in another?

Share this post


Link to post
Share on other sites
I actually wanted an easy way to iterate through them all for collision detection but yeah I guess I could do that if they are all separate anyways. Would you still recommend using vectors to store everything?

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeisprettyfly
I actually wanted an easy way to iterate through them all for collision detection but yeah I guess I could do that if they are all separate anyways.
You can always decide to store an additional vector of references to the actual colliding hulls, if you want (in addition to the original value vectors). However, a typical issue of collision detection is dispatching behavior based on the types of the objects, so you probably don't want to iterate through all objects for collision detection, rather you will check all power-ups against the player, all bullets against all enemies, and so on.

Quote:
Would you still recommend using vectors to store everything?
By default, yes. If you need to reference sub-objects of the objects in the vectors, you'll have to move to a list instead (because vector resizing, removal and insertion invalidate iterators and memory references).

Share this post


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

  • Advertisement