tracking a std vector

Started by
0 comments, last by Evil Steve 17 years, 9 months ago
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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
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.

This topic is closed to new replies.

Advertisement