Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gold Dragon on Sat 10/05/2008 02:01:27

Title: enums in Arrays
Post by: Gold Dragon on Sat 10/05/2008 02:01:27
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?
Title: Re: enums in Arrays
Post by: Khris on Sat 10/05/2008 08:40:07
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:
enum Direction {
  Top=0,
  Bottom=1,
  Left=2,
  Right=3
};

Then one can use "int array[4];"
Title: Re: enums in Arrays
Post by: monkey0506 on Sat 10/05/2008 08:45:44
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.
Title: Re: enums in Arrays
Post by: Radiant on Sat 10/05/2008 10:42:06
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


#define myDirection 5

int Array[myDirection];

Title: Re: enums in Arrays
Post by: Kweepa on Sat 10/05/2008 18:37:43
Quote from: KhrisMUC on Sat 10/05/2008 08:40:07
Perfectionists would assign values to the enum:

You can avoid that ugliness with:

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

int array[kDirection_Count];
Title: Re: enums in Arrays
Post by: Radiant on Sat 10/05/2008 23:36:04
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.
Title: Re: enums in Arrays
Post by: Kweepa on Sun 11/05/2008 19:23:32
I'm reasonably sure you're right. It doesn't matter for my code snippet though.