Loading...
Logo
Processing Forum

State Machines

in Programming Questions  •  10 months ago  
So I am trying to put together a state machine, and am running into some difficultly. 

I created a tab in my sketch called "states.java" with the following lines, 

Copy code
  1. public enum states { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };

  2. states state = new states;
Then in my main sketch tab I have the flow like so:

Copy code
  1. switch (state){

  2. case STATE_1 : 
  3. // if this does that, do something
  4. }
  5. state = STATE_2;
  6. break;

  7. case STATE_2: 
  8. // if this does that, do something
  9. }
  10. state = STATE_3;
  11. break;
  12. //etc etc

but I am receiving an error saying "cannot find anything named "state". What am I missing?

Cheers!



Replies(9)

Re: State Machines

10 months ago
There is no "new states", these are kind of constants, not instantiable. Just declare:
states state;
Suggestion: write States instead, to show it is a type of your own, akin to classes.

Re: State Machines

10 months ago
tried this. I get a NullPointerException error

Re: State Machines

10 months ago


Do you have

Copy code
  1. public enum States { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };

  2. States state ;


Where do you get a NullPointerException error?      

Re: Re: State Machines

10 months ago
Copy code
  1. public enum States { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };

  2. States state ;

that above approach gives me the error, "cannot find state" in my main tab.

Re: State Machines

10 months ago


I don't use enums, I don't know how it is...

Copy code
  1. // -------------------------------------------
  2. // program
  3. final int statePlay     =0;
  4. final int stateWon      =1;
  5. final int stateDead     =2;
  6. final int statePause    =3;
  7. final int stateStartUp  =4;
  8. final int stateNewLevel =5;
  9. //

  10. int stateOfGame = stateStartUp;

  11. //
  12. // -------------------------------------------

Re: State Machines

10 months ago
This works

States.java
Copy code
  1. public enum States { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };

Main sketch tab

Copy code
  1. States state = States.STATE_2;

  2. switch (state) {
  3. case STATE_1 :
  4.   println("STATE 1");
  5.   break;
  6. case STATE_2:
  7.   println("STATE 2");
  8.   break;
  9. case STATE_3:
  10.   println("STATE 3");
  11.   break;
  12. }
Try changing the state in line 1 to prove it works.

Re: State Machines

10 months ago
Thanks everyone!

I am going with quarks solution which is working great for me!

Re: State Machines

10 months ago
Coming back late, but indeed, you still need to give an initial state. I didn't assign it because I don't know what your logic is, and I thought it was obvious... My bad.
BTW, I hope you are going to give more descriptive names to your states. 

Re: Re: State Machines

10 months ago
No problem! Thanks for the suggestion. 

Yes, depending on my loops, they should have more descriptive names :)