1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GM:S 1.4 GMS 1.451 enum problem

Discussion in 'Legacy GameMaker Community Tech Support' started by bigweight, Nov 21, 2017.

  1. bigweight

    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?
     
  2. Dmi7ry

    Dmi7ry Member

    Joined:
    Jun 24, 2016
    Posts:
    481
    Try:
    1. switch Cstate
    2. case state.idle:
    etc.
     
  3. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,262
    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.
     
  4. Dmi7ry

    Dmi7ry Member

    Joined:
    Jun 24, 2016
    Posts:
    481
    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:
    }
     
  5. bigweight

    bigweight Guest

    sorry, I'm kinda new, I didn't understand what I forgot
     
  6. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,262
    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.
     
  7. bigweight

    bigweight Guest

    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
     
  8. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,262
    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;
    }
    
     
  9. Dmi7ry

    Dmi7ry Member

    Joined:
    Jun 24, 2016
    Posts:
    481
  10. bigweight

    bigweight Guest

Share This Page