Sign in to follow this  

tracking a std vector

This topic is 4166 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 currently making a kind of RTS game, and because of the fact that I wanted to use preexisting classes and stuff (cause it's cool and fast) I now have two kinds of classes for the building - the graphics and the "obstacle" part, the graphics part lets you see it and the obstacle part makes things not bang into it. I have two std vectors, one to store a group of each class. When ever I want to build a new building I add a "graphic" to the graphics vector and an obstacle in that spot to the obstacle vector. Now my problem is, when a certain building is destroyed (which for reasons not important for this post happens with the graphic) I can take the graphic out of the graphic vector but how can I take the correct obstacle out of the obstacle vector? I thought of these possibilities: 1) when a building is destroyed, cycle through all the obstacles and check which one is in that position, if one is found erase it. 2) let each graphic get the index of the obstacle in the obstacle vector in its constructor, whenever the graphic kills itself it will erase the obstacle in that place, but that means that I'll have to tell all the buildings that save an obstacle after the one erased to subtract one. 3) same as two but instead of erasing turn it's size to 0. I know that all of these are terrible hacks, is there a better way? Please tell me if the situation isn't clear. Thanks.

Share this post


Link to post
Share on other sites
If your vector of collision objects contains only the buildings, you can just use the same index into the collision vector as the graphics vector.

Alternatively, I'd make the graphics object have a pointer to a collision object, and store a vector of pointers to collision objects instead of the objects themselfs. The graphics object can "own" the collision object.

Share this post


Link to post
Share on other sites

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