Page 1 of 1

Simple C++ problem

Posted: Sat Feb 23, 2008 11:15 pm
by Ion Dune
For my game, I'm tying to make units which have x and y coordinates as well as health and other necessities. Then, in my main I will simply draw all of the units based on their x and y. I had planned to use structs, but it seemed a little old fashioned so I wanted to see what was recommended in cpp for this kind of thing.

I learned to program mostly in Basic, and what I used there was rather hodge-podge: separate arrays for every element of the unit.

Basically what I'm asking is, what is the most commonly used cpp thing used for this, or at least what would be recommended in my situation.

Posted: Sat Feb 23, 2008 11:30 pm
by Acki
I also would use structs to hold all necessary unit datas, combined with an array, probably... ;)

Posted: Sat Feb 23, 2008 11:37 pm
by Ion Dune
Hmm... so an array of structs? I tried this:

Code: Select all

typedef struct {
        int lvl;
        int x;
        int y;
        } unit;
        
unit myunits[2];

myunits[0].lvl=1;
But its not compiling. What did I do wrong?

Posted: Sun Feb 24, 2008 12:11 am
by Acki
no, the Irrlicht array class... ;)
and maybe the struct with functions (or maybe a class instead of a struct)...

Code: Select all

struct unitData{
  int lvl;
  int x;
  int y;

  movePosition(int x, int y){
    // for example: move the unit to x,y
  }
};

array<unitData> lstUnits;

void addUnit(int x, int y, int lvl){
  unitData uNew;
  uNew.x = x;
  uNew.y = y;
  uNew.lvl = lvl;
  lstUnits.push_back(uNew);
}
for further infos about the array class, have a look at the API !!! ;)

Posted: Sun Feb 24, 2008 12:44 am
by Ion Dune
EDIT: Nevermind, figured it out.

Thanks for all your help.

Re: Simple C++ problem

Posted: Sun Feb 24, 2008 11:33 am
by ebo
Ion Dune wrote:I had planned to use structs, but it seemed a little old fashioned so I wanted to see what was recommended in cpp for this kind of thing.
In C++ a struct is a class with default accessibility public. (If its not a POD, but you shouldnt be concerned about that). Conclusion: Using structs is ok.