reading data from classes

Started by
6 comments, last by Iron Eye 21 years, 5 months ago
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
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
Advertisement
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 &#111;n a brutal rampage is the obvious choice.    
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
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...
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
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?:

  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]
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
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 -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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.

This topic is closed to new replies.

Advertisement