reading data from classes
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
You could make an array of monsters. Then you could just do this:
for(int i=0; i<NumMonsters; i++){ //Put Monster in the buffer<br></font><br>}<br> </pre></DIV><!–ENDSCRIPT–> <br><br>Proceeding on a brutal rampage is the obvious choice.
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...
What''s the problem? Have the monster class store all the pertinent information, then make an array of monsters.
So when I declare that, would it be like so?:
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:
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]
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]
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 -- ¦þ
______________________________
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 -- ¦þ
______________________________
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.
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.
Quiggy.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement