-
Notifications
You must be signed in to change notification settings - Fork 129
Transition Classes
In their most basic form, transitions describe when the state machine should change from one state to another. UnityHFSM provides multiple kinds of transitions that you can use in your project for different situations, e.g. regular transitions, trigger transitions, transitions from any ... (see the feature overview for more information).
What is common to all of them is that you need some code that decides whether a transition is "active", i.e. should occur. This is what the transition classes are for. UnityHFSM provides multiple transition classes that you can use to significantly reduce the amount of code required to achieve certain behaviours.
In other words, the state machine transition types ("add ... transition") determine when a transition is checked (and how it behaves in the case of exit transitions), whereas the transition classes decide if a state change should happen once checked.
As with the state types, the base transition classes use generics to allow you to use different types for the identifiers of states. Thanks to class overloads, boilerplate code is kept to a minimum for the most common use case (using strings for the state names).
The base class of all transition classes is the TransitionBase<TStateId>
class. The higher-level transition types inherit from this class and provide additional features, such as the support for conditions.
If you are implementing you own transition class, it is recommended to inherit from TransitionBase<TStateId>
.
- Base class of all transitions.
- Condition-less => the transition should always occur.
new TransitionBase("FromState", "ToState", forceInstantly: false);
- Generic type parameters:
TransitionBase<TStateId>
new TransitionBase<int>(0, 1, forceInstantly: false);
- Generic overloads:
-
TransitionBase
=TransitionBase<string>
-
- Default transition class.
- Lets you define a custom condition that is checked to see whether the state machine should transition or not.
new Transition("From", "To");
new Transition("From", "To", forceInstantly: true);
new Transition("Idle", "Follow", self => DistanceToPlayer < 5);
- Generic type parameters:
Transition<TStateId>
- Generic overloads:
-
Transition
=Transition<string>
-
- Waits a certain delay before transitioning.
- Lets you define a custom condition that is checked after the delay has elapsed.
// Waits for 2 seconds
new TransitionAfter("From", "To", 2);
new TransitionAfter("From", "To", 2, forceInstantly: true);
new TransitionAfter("From", "To", 2, self => DistanceToPlayer < 5);
- Generics and overloads are the same as
Transition
(TransitionAfterDynamic<TStateId>
)
- Same as
TransitionAfter
but lets you dynamically compute the delay value:
new TransitionAfterDynamic("From", "To", self => ComputeDelay());
new TransitionAfterDynamic(
"From",
"To",
delay: self => ComputeDelay(),
condition: self => DistanceToPlayer < 5
);
- It also has the option to only evaluate the dynamic delay when the
from
state enters. This is useful, e.g. when the delay of a transition should be random. E.g.
fsm.AddTransition(new TransitionAfterDynamic(
"From", "To", t => Random.Range(2, 10), onlyEvaluateDelayOnEnter: true
));
- Generics and overloads are the same as
Transition
(TransitionAfter<TStateId>
)
- Transition types that trigger when a specific key is pressed / released / ...
new TransitionOnKey.Down("A", "B", KeyCode.Space); // Like Input.GetKey(...)
new TransitionOnKey.Pressed("A", "B", KeyCode.Space); // Like Input.GetKeyDown(...)
new TransitionOnKey.Released("A", "B", KeyCode.Space); // Like Input.GetKeyUp(...)
new TransitionOnKey.Up("A", "B", KeyCode.Space); // Like !Input.GetKey(...)
- Generics and overloads are the same as
Transition
(TransitionOnKey<TStateId>
).
- Transition types that trigger when a specific mouse button is pressed / released / ...
new TransitionOnMouse.Down("A", "B", 0); // Like Input.GetMouseButton(...)
new TransitionOnMouse.Pressed("A", "B", 0); // Like Input.GetMouseButtonDown(...)
new TransitionOnMouse.Released("A", "B", 0); // Like Input.GetMouseButtonUp(...)
new TransitionOnMouse.Up("A", "B", 0); // Like !Input.GetMouseButton(...)
- Generics and overloads are the same as
Transition
(TransitionOnMouse<TStateId>
).
- Similar to the
DecoratedState
class, this type wraps a transition and allows you to define additional behaviour with the transition. It does not interfere with the behaviour of the wrapped transition.
var myTransition = new TransitionAfter("A", "B", delay: 2);
var decoratedTransition = new DecoratedTransition(
myTransition,
// Called when the 'from' state is activated.
beforeOnEnter: transition => { },
afterOnEnter: transition => { },
// Called every time the wrapped transition is checked.
beforeShouldTransition: transition => { },
afterShouldTransition: transition => { },
// Called on the 'onTransition' callback.
beforeOnTransition: transition => { },
afterOnTransition: transition => { },
// Called on the 'afterTransition' callback.
beforeAfterTransition: transition => { },
afterAfterTransition: transition => { }
);
-
It can be useful for debugging or when you want to extend the functionality of common code.
-
If you want to decorate multiple transitions, you can use the
TransitionDecorator
class which takes the custom callbacks once and creates aDecoratedTransition
instance for every transition provided to itsDecorate
method. -
Generics and overloads are the same as
Transition
(DecoratedTransition<TStateId>
).