• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Legacy GM GMS 1.451 enum problem

B

bigweight

Guest
Hello guys, I'm having a bit of trubles with my state machines, in particular I have this code:

Code:
enum state {
idle,
Chase,
Investigate,
Search,
}
Cstate = state.idle;

the problem is that state does not turn red and the game does not even recognize the state variable when I go to the state event and I write:

Code:
switch (state) {

 case idle:
 scrIdle()
 break;

 case Chase:
 scrChase();
 break;

 case Investigate:
 scrInvestigate();
 break;

 case Search:
 scrSearch();
 break;
}
I get the error that he doesn't know what ''idle'' is basically...what's wrong with this enum?
 

Simon Gust

Member
Well, it's normal that sometimes enums don't turn red.
In this case however, you forgot to add the enum parent to the enum
->
state.idle, state.Chase etc.

Just don't trust the colors. I have normal variables that are technically enums and are red just because there is somewhere an enum in the project.
 

Dmi7ry

Member
And don't mix enums and variables.
For example, write each enum in CamelCase:
Code:
enum State { Idle, Chase, Investigate, Search }
and use snake_case for variables:
Code:
var state = State.Idle;
...
switch state
{
    case State.Chase:
}
 
B

bigweight

Guest
Well, it's normal that sometimes enums don't turn red.
In this case however, you forgot to add the enum parent to the enum
->
state.idle, state.Chase etc.

Just don't trust the colors. I have normal variables that are technically enums and are red just because there is somewhere an enum in the project.
sorry, I'm kinda new, I didn't understand what I forgot
 

Simon Gust

Member
sorry, I'm kinda new, I didn't understand what I forgot
So when you create an enum
Code:
enum color
{
 red = 0,
 green = 1,
 blue = 2
}
You have this parent enum called "color" and sub enums (red, green and blue here).
When you read an enum like setting a variable to an enum you have to write the enums parent and the sub enum together wit a dot-operator

Code:
var my_color = choose(color.red, color.green, color.blue);
Only then, the compiler knows which enums to use. Since you can have different parents with the same sub enums.

Important is, that you don't have any variables called after enum parents.
In this case variables, resource names etc. that are called "color" are invalid and give erros when trying to use them.
 
B

bigweight

Guest
So when you create an enum
Code:
enum color
{
 red = 0,
 green = 1,
 blue = 2
}
You have this parent enum called "color" and sub enums (red, green and blue here).
When you read an enum like setting a variable to an enum you have to write the enums parent and the sub enum together wit a dot-operator

Code:
var my_color = choose(color.red, color.green, color.blue);
Only then, the compiler knows which enums to use. Since you can have different parents with the same sub enums.

Important is, that you don't have any variables called after enum parents.
In this case variables, resource names etc. that are called "color" are invalid and give erros when trying to use them.
I changed everything as you said
Code:
enum States {

idle,
Chase,
Investigate,
Search,
}

Cstate = States.idle;
Code:
switch (States) {

 case States.idle:
 scrIdle()
 break;
 
 case States.Chase:
 scrChase();
 break;
 
 case States.Investigate:
 scrInvestigate();
 break;
 
 case States.Search:
 scrSearch();
 break;
}
but I still get the same error :
''Push :: Execution Error - Variable Get -1.States(100041, -2147483648)
at gml_Object_pEnemy_StepNormalEvent_1 (line 20) - case States.idle:''

basically it's like I'm not even writing an enum
 

Simon Gust

Member
I changed everything as you said
Code:
enum States {

idle,
Chase,
Investigate,
Search,
}

Cstate = States.idle;
Code:
switch (States) {

 case States.idle:
 scrIdle()
 break;
 
 case States.Chase:
 scrChase();
 break;
 
 case States.Investigate:
 scrInvestigate();
 break;
 
 case States.Search:
 scrSearch();
 break;
}
but I still get the same error :
''Push :: Execution Error - Variable Get -1.States(100041, -2147483648)
at gml_Object_pEnemy_StepNormalEvent_1 (line 20) - case States.idle:''

basically it's like I'm not even writing an enum
You are still trying to switch the parent enum. You should instead switch the Cstate variable
->
Code:
switch (Cstate) {

case States.idle:
scrIdle()
break;

case States.Chase:
scrChase();
break;

case States.Investigate:
scrInvestigate();
break;

case States.Search:
scrSearch();
break;
}
 
Top