Evan Kinsler
Member
The subject is really self-explanatory on what I'm asking.
Can global variables be used in an enum?
Can global variables be used in an enum?
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.
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;
}
enum WEAPONS
{
rifle = -27,
sword = 5;
bow = -3,
dagger = 0,
}
// 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?
Yes your code there will work. A value from an enum can be used in a global variable. Just make sure the enum exists firstI’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?
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.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, myEnum.value2 will not become 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?
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.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?