enums in Arrays

Started by Gold Dragon, Sat 10/05/2008 02:01:27

Previous topic - Next topic

Gold Dragon

How do I declare an integer Array that uses an enum value as the counter.

Normally you do..

int Array[10];

and you retrieve values by calling Array[1];

now do I that with enum values..

enum Direction {Top, bottom, Left, right};

int Array[Direction];

Array[Top];




Does this work? or how do I do it?
Want to see how I am doing and how far I've gotten in my game? Visit my forum  http://www.freepowerboards.com/DragonKnight

Khris

It won't work like that because the standard declaration of an array has to use a constant.
But since you know the size of the array, you can simply use "int array[5];"

Afaik, enum values start off at 1, so in this case, Top=1, bottom=2, ...
Thus the array needs to have 5 elements (array[0] to array[4]). array[0] won't be used here.

Perfectionists would assign values to the enum:
Code: ags
enum Direction {
  Top=0,
  Bottom=1,
  Left=2,
  Right=3
};

Then one can use "int array[4];"

monkey0506

Spot on answer Khris. However it's important to note that AGS already has a built-in enumeration named Direction, so you will have to choose a different name.

Oh, and just FYI, the AGS standard (the way all built-in enumerated values are defined) is to prefix all enumerated values with 'e' (such as eTop, eBottom, etc.) just to clarify what the values are, but it isn't required.

Radiant

Quote from: Gold Dragon on Sat 10/05/2008 02:01:27
enum Direction {Top, bottom, Left, right};

int Array[Direction];

What does work, if you must, is

Code: ags

#define myDirection 5

int Array[myDirection];


Kweepa

#4
Quote from: KhrisMUC on Sat 10/05/2008 08:40:07
Perfectionists would assign values to the enum:

You can avoid that ugliness with:
Code: ags

enum EDirection
{
  kDirection_Left, // if you want you can put "= 0" here
  kDirection_Right, 
  kDirection_Up, 
  kDirection_Down, 
  kDirection_Count
};

int array[kDirection_Count];
Still waiting for Purity of the Surf II

Radiant

Quote from: SteveMcCrea on Sat 10/05/2008 18:37:43
  kDirection_Left, // if you want you can put "= 0" here

I'm reasonably sure AGS enumerations start at one unless explicitly specified otherwise.

Kweepa

I'm reasonably sure you're right. It doesn't matter for my code snippet though.
Still waiting for Purity of the Surf II

SMF spam blocked by CleanTalk