GMS 2.3+ State Machine enum error

wilmer

Member
I get this error in my state machine "enum assignment must be an integer constant". This state machine has 4 scripts that tell you what animation the character should have in each state, which looks like this:

enum Man {
stopped = Man_ stopped,
running = Man_running,
attacking = Man_attacking,
dying = Man_dying
}
What am I doing wrong ?
 

TsukaYuriko

☄️
Forum Staff
Moderator
The error message states that you must assign integer constants to enum elements - which means that what you are assigning is not an integer constant. An integer constant is 0, 1, 2, 3, 4... you get the idea.

You seem to be trying to use an enum like a dictionary, or a map, or a key-value pair... whichever name sounds more familiar. They are not, and you shouldn't use them like that. What you can do, however, is using a complementary structure which acts as a translation table (between enum element and corresponding desired value) of sorts, such as an array:
GML:
enum State
{
    IDLE,
    WALKING,
}

animations[State.IDLE] = animation_idle;
animations[State.WALKING] = animation_walking;

state = State.IDLE;
animation = animations[state];
 
Top