State - ThiagoDAraujoS/CSharp-State-Machine GitHub Wiki
States are Abstract Classes that define a generic State to be run by the Engine.
Each state has to be nested inside the class holding its state machine's engine.
All states are required to be decorated with the [StateFrom("State Machine Name")] attribute.
Every state machine must have one state decorated with the [Initial] attribute.
public class Example {
Engine stateMachine;
public Example(){
stateMachine = new Engine("state machine name", this);
}
public void Update(){
stateMachine.Run();
}
[Initial][StateFrom("state machine name")]
public class FirstState : State{
//reference to the external *Reference* object having this state machine
Example reference;
//Happens once when the state machine is initialized
public override Awake(){
//Cast the object m saved within the abstract class, onto an Example type.
reference = (Example)m;
}
//Happens every time this state is loaded as current state
public override Start(){ }
//Happens every time Engine.Run() is called and its the current state
public override Update(){ }
//Happens every time the state is unloaded.
public override End(){ }
}
}
Fields
Owner's reference
public Object m;
Reference to the object holding this state machine.
Public Virtual Methods
Awake
public override void Awake();
This method is called by the engine as it's initialized for the first time.
Start
public override void Start();
This method is called by the engine when it loads this state as the current running state.
Update
public override void Update();
This method is called by the engine if this state is the current running one when its engine.run() is called.
End
public override void End();
This method is called by the engine when it unloads this state as the current running state.
Decorators
State From Name
[StateFrom(String name)]
The StateFrom decorator binds the nested State class as part of a state machine of name "name".
This decorator can only target state machines declared on the outer class or a parent of it, effectively enhancing its inherited state machine with new states.
Parameters
- name is the unique name Id for the state machine system bound to this state.
Initial State
[Initial]
Sets the decorated state as the first state loaded in the state machine.
Override State
[Override]
Overrides a state previously declared on a parent state machine.