Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic mutt arrray thing. 1

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
Me and a couple of guys are writing a game. The only thing code-wise stopping us is our lack of a dynamic mutt array. What i mean by this, is an array that has no fixed length and can have elements added and removed from it. It also needs to be able to accept a bunch of different types. Like array[1] could be an int and array[2] could be a char. The reason for such an array would be for the main object array. All the objects in our game would be in the same array, and have the ability to be spawned and deleted during run-time. I looked into vectors, but i cannot get them to work. It wont let me assign an iterator to a variable. I'm also not sure as to if vectors accept multiple types.

My Idea is to make my own array class. Since all data is bytes, couldnt i just make a zero defined byte array and when i need to add to it, save its contents, delete it, and create a new byte array?

I just need a dynamic mutt array, one way or another. Man I miss my javaScript arrays ;-)
 
vector class from STL seem to provide what you need.

> "It also needs to be able to accept a bunch of different types"

It is possible through class inheritance and polymorphism. Say you have the class GameObject and the classes of all of what is displayed and managed in the game scene inherit GameObject then it's ok with vectors.
For example for a car racing game let's say you have the classes :
GameObject <- Car
<- OptionItem
<- BananaSkin
<- Accelerator
<- etc.
Classes Car, OptionItem, ... inherits from GameObject.
Then you can hold all your game objects in the same vector with :
Code:
vector<GameObject*> game_objects;
//fill game_objects
game_objects.push_back (new Car (...));
game_objects.push_back (new Car (...));
game_objects.push_back (new BananaSkin (...));
...
//the first car was destroyed by a bomb, remove it
delete game_objects[0];
game_objects.erase (game_objects.begin ());
...

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top