GMS 2.3+ Can global variables be used in an enum?

kburkhart84

Firehammer Games
You would have to show us exactly what you are trying to do. The enum itself as I understand it is basically a type of variable, though if I'm not mistaken it get global scope automatically. And the values inside the enum have to be integers.

EDIT*****

The manual states that it won't work at all with variables, and only works with enums that were previously created.

Notice in the above example we use another enum to create an expression for "violet". This only works if the enum being referenced was created before the enum that is using it in an expression, but it will not work for variables or functions, since the enum value must be able to be evaluated as a constant when the project is Compiling.
 

Neptune

Member
GML:
enum WEAPONS
{
rifle, //defaults to 0
sword, // 1
bow, // 2
dagger,
}

global.my_weapon = WEAPONS.sword; //global var now contains the integer value 1

switch(global.my_weapon)
{
     case WEAPONS.sword:

     break;
}
Hope that helps. You wouldnt put variable names as the enumerated values though...
Enums default to 0, and increment each entry by default, however if needed, you can define each one like so:

GML:
enum WEAPONS
{
rifle = -27,
sword = 5;
bow = -3,
dagger = 0,
}
 
Last edited:
GML:
// So if you just make a regular enum,

myEnum {
   value1: 1;
   value2: 2;
   value3: 3;
}

// When you do this:

global.globalvalue = myEnum.value2;

// myEnum.value2 becomes a global variable?
I’m only asking if it is possible.
 
GML:
// So if you just make a regular enum,

myEnum {
   value1: 1;
   value2: 2;
   value3: 3;
}

// When you do this:

global.globalvalue = myEnum.value2;

// myEnum.value2 becomes a global variable?
I’m only asking if it is possible.
Yes your code there will work. A value from an enum can be used in a global variable. Just make sure the enum exists first
 
I also want to ask if you can put enums into an array. Essentially something like this.


GML:
enumArray = [

myEnum1 {
   value1: 1;
   value2: 2;
   value3: 3;
}

myEnum2 {
   value1: 1;
   value2: 2;
   value3: 3;
}];

// And then you can reference one of the enum's values by doing this...

enumArray[1].value1;

// Is this possible?
 

kburkhart84

Firehammer Games
I also want to ask if you can put enums into an array. Essentially something like this.


GML:
enumArray = [

myEnum1 {
   value1: 1;
   value2: 2;
   value3: 3;
}

myEnum2 {
   value1: 1;
   value2: 2;
   value3: 3;
}];

// And then you can reference one of the enum's values by doing this...

enumArray[1].value1;

// Is this possible?
I don't think that would work. If you want to be sure, try it, and see if you don't get an error when you try to run it.
 

TsukaYuriko

🌠
Forum Staff
Moderator
GML:
// So if you just make a regular enum,

myEnum {
   value1: 1;
   value2: 2;
   value3: 3;
}

// When you do this:

global.globalvalue = myEnum.value2;

// myEnum.value2 becomes a global variable?
I’m only asking if it is possible.
No, myEnum.value2 will not become a global variable.
global.globalvalue will become a global variable that holds the value 2. This 2 has nothing to do with the enum's value apart from being the same numeric value.

Enums are global, so myEnum is global regardless of whether you do anything to try to cause it (or to prevent it). All of the enums will therefore not "become" global, but they will "be" global, whether that last line is there or not.

I also want to ask if you can put enums into an array. Essentially something like this.


GML:
enumArray = [

myEnum1 {
   value1: 1;
   value2: 2;
   value3: 3;
}

myEnum2 {
   value1: 1;
   value2: 2;
   value3: 3;
}];

// And then you can reference one of the enum's values by doing this...

enumArray[1].value1;

// Is this possible?
No, enums can not be used this way. Enums themselves are not valid data types (as opposed to enum elements, which are integers) and can not be assigned to anything - not to variables, and therefore also not to array elements.
 

Neptune

Member
Once you declare an enum somewhere, you can access anywhere... Different objects, scripts, creation code etc
 
Top