Link - ThiagoDAraujoS/CSharp-State-Machine GitHub Wiki

public delegate bool Link();

Links are special predicates that help the engine to evaluate if its current state state should transition to a new state.
These special methods should be placed inside their related state and decorated with the special attributes.
These methods run every time the state machine is updated.

Decorators

Link To State

[LinkTo(String stateName)]
[LinkTo(Type stateType)]

The LinkTo decorator tells the engine this links follow up state.
The special keyword "All" can be used here alternately as an argument in order to indicate that rather than linking to a particular state, this link will be planted in every state pointing back to its owner State instead.

//Generic LinkTo predicate
[LinkTo(typeof(WalkState))]
public bool IsWalking(){
    return Input.GetAxis("Horizontal") > 0f;
}

Is important to be aware that a Link to "All" will also build a link from its owning state into itself, in order to avoid this behavior a [Mask(State)] is advised.
For instance the "All" command is useful on a character's state machine where every other state should navigate to a "death state" regardless when that link evaluates true

Parameters

  • stateName is the unique name Id for the state this link will segue to, it can also be the keyword "All".
  • stateType is the type of the state this link will segue to.

Mask States

[Mask(params Type[] maskedStateList)]

The Mask attribute is to be used together with the [LinkTo("All")] attribute, it tells the Link To "All" decorator what states it should avoid.

//Link To All with masks predicate
[LinkTo(typeof("All"))][Mask(typeof(DeadState), typeof(ImmortalState)]
public bool IsDead(){
    return ((Character)m).Hp = 0;
}

Parameters

  • maskedStateList is a list of state types that the Link To "All" will avoid connecting with.

Reverse

[Reverse]

The Reverse decorator indicates that rather than linking to a particular state, this link will be planted in its targeted State pointing back to its owner State instead.

//Reverse link to state predicate
[Reverse][LinkTo(typeof(OnGroundState))]
public bool IsFlying(){
    return Input.GetAxis("Jump") && CanFly;
}

Reverse is useful specifically for inherited state machine systems, for instance:
Imagine you have an already set up state machine system on a parent master object, and you want to make a child class with new states extending the parent's state machine.
Since the parent object doesn't know anything about its child's implementation, you can't declare links from a parent state into a "theoretical" child state.
To work around that you can set up reverse links on a child state instead, and that link will be built on the parent state pointing towards the child state implementing it, effectively extending the parent's state machine with new states.