Simple C++ problem

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Simple C++ problem

Post 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.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I also would use structs to hold all necessary unit datas, combined with an array, probably... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post 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?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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 !!! ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

EDIT: Nevermind, figured it out.

Thanks for all your help.
ebo
Posts: 38
Joined: Sun Feb 19, 2006 5:39 pm

Re: Simple C++ problem

Post 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.
Post Reply