Resources/Developer Info & Forums/Developing for Original Sifteo Cubes

Sample code for States and IControllers?

Michael Muldoon
posted this on January 18, 2012 03:34 pm

Hi all, I'm working through the SDK and running into a bit of trouble with the Util.StateMachine and IControllers. I don't exactly understand how a transition transfers control from one controller to the other. I think just a short code snippet would help clear things up. Anyone?

Many thanks.

Mickey

 

Comments

User photo
Jochen Derwae

At the time of a state change, the StateMachine will call:

  1. oldState.OnDispose();
  2. newState.OnSetup();
  3. currentState = newState;

 

Any call to StateMachine.Tick() will be forwarded to currentState.OnTick() and to StateMachine.Paint() will be forwarded to currentState.OnPaint().

 

The question that remains now is: when does StateMachine switch states? There are 4 points at which StateMachine will do a transition (if there is a transition queued obviously) and that's:

  • at the start of StateMachine.Tick() [before currentState.OnTick()]
  • at the end of StateMachine.Tick() [after currentState.OnTick()]
  • at the start of StateMachine.Paint() [before currentState.OnPaint()]
  • at the end of StateMachine.Paint() [after currentState.OnPaint()]

 

Personally I don't like the possibility of StateMachine doing a state change between Tick and Paint, so I forked the StateMachine class, removed the 4 state change points and added an explicit ChangeState to be called in the main loop, after Tick and Paint have been called. But that's just my preference.

 

Sorry I didn't write any more of a "working" code snippet, I hope nevertheless that it's helpful.

 

Jochen

January 26, 2012 06:31 am