Archived

This topic is now archived and is closed to further replies.

Iron Eye

reading data from classes

Recommended Posts

Alright, I made a class called "MONSTER" this will store the x, y for a monster. So say I create two instances of it, for example: MONSTER firstmonster; firstmonster.name = "bob"; firstmonster.hp = 100; //and so on //then MONSTER secondmonster; secondmonster.name = "joe"; secondmonster.hp = 50; //and so on ------------------------------------------- I was curious is it possible to read data for both monsters, without knowing the names of the instances, kinda hard to explain what I''m trying to say. I have a function that draws everything to the screen. What I need to do is cycle through every position of each monster to draw them, so it would be something like: for every instance of MONSTER { // put it in buffer } Thanks, Kris

Share this post


Link to post
Share on other sites
You could make an array of monsters. Then you could just do this:


  
for(int i=0; i<NumMonsters; i++)
{
//Put Monster[i] in the buffer

}


Proceeding on a brutal rampage is the obvious choice.

Share this post


Link to post
Share on other sites
I need to store quite a bit of information on each monster, such as name current x, y postion min damage max damage, etc etc, I don''t see how I could get that all into an array...

Share this post


Link to post
Share on other sites
So when I declare that, would it be like so?:

  
MONSTER monsters[i];

Then just keep adding to the array for each monster I create?

So to subtract from the hp of the second monster I create, would thus be:

  
monsters[2].hp--;

Havn't tested it, but to me it looks like it'll work... Thanks!

Kris


EDIT: Forgot to make source tags

[edited by - Iron Eye on November 5, 2002 8:08:20 PM]

Share this post


Link to post
Share on other sites
Careful, to get the 2nd monster in the array, you''d have to use:
monsters[1].hp--;
Remember that the first element of an array has an index of 0, not 1.

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
quote:
Original post by Iron Eye
Then just keep adding to the array for each monster I create?



No, that won''t work. You can''t resize an array at runtime, you would have to re-allocate the whole thing each time you add to it. You should take a look at std::vector if you need to use a re-sizable array.

Share this post


Link to post
Share on other sites
Or you could set up a linked list. This way you can add and delete monsters as you go. But it''s not the easiest thing to get going if you''ve never done one before...

Quiggy.

Share this post


Link to post
Share on other sites