Sign in to follow this  

The beauty of vectors

This topic is 4559 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 once racked my brain trying to figure out how to control statistics for a game character. I thought about Arrays. I knew that the type of data I insert into each memory block of an Array would never change. If memory block 1 was an integer then it would always be an integer. It might be changed to a higher or lower number, but still it will remain an integer. But Arrays are not powerful enough to handle the Class Structure for my needs. The minute I am creating a whole new character out of my Class Structure, the Array fails. You can't put 2 different pieces of data in the same memory block. What I mean is that you can not have an Array of Arrays as far I as I know. So, the way I designed my program led me to the Vector. However, the vector itself was not enough. I needed to build a vector of arrays. And that wasn't enough either. My characters had to be identified within a Class structure. So I had a Vector of Arrays of the Class Characters. I found this to be an ingenious way of managing data. With this simple approach I could create an unlimited number of NPCs (non playing characters) each with their own unique statistics and keep track of them all with 1 powerful vector:-) The Vector is like an Array. It is easy to search through it and easy to sort the contents of it like an Array. The beauty of a Vector is that it is capable of holding Arrays. My data structure worked perfectly. I could call up the function, Hitpoints, and add or subtract hitpoints. I could call up the function, Experience, and add or subtract experience. The more functions I plugged into my Class the more data my Array would hold and the more powerful my Vector became. Since Arrays are the easiest structure to organize data with (in my opinion) I could save and load all of my data easily. I would just go TOP to Bottom through my Character Class's Variables. I hope this makes sense for anyone reading this because this really helped me understand how to manage complex data for a Role Playing Game Character's statistics or a Sports Player's Statistics. The applications for this are endless.

Share this post


Link to post
Share on other sites
Very nice poem.

You are a natural born artist.

Vectors are useful data structures. I would rather use an std::list<> structure, for dynamism reason, when it comes to game entities, however.

Share this post


Link to post
Share on other sites
Heh ya in the game i'm working on i have all the players in a vector. vector<cPlayer> players. That way i can call a method of the cPlayer class on each instance. Say i want to display all the players. I just traverse the vector with .display(); Its pretty cool. I've been thinking about making a base class in which all other classes of my game will derrive from and that way i can make all have x y getx gety width ect then i can have one method isClicked() and it will work whatever class type i put in there. Not sure if it will work but just an idea :-)
-CORY

Share this post


Link to post
Share on other sites
you are right. that will work, and ive done it plenty of times. what i did was define and INTERFACE for all game objects, with methods like this:

getX()
getY()
update( float timeDelta )
render() or render( surface ) //depends on API

and a couple of others.

you can implement this interface, and then derive off of those base classes, so if you need to process game objects it doesnt matter if its a player, a background, an item, an enemy, a special effect, etc.

Share this post


Link to post
Share on other sites

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