03. StateMachineBehaviour<T…>

  04. Features No Comments

Init(args) contains new generic versions of the StateMachineBehaviour base class, extending it with the ability to receive up to six objects during initialization.

For example the following AttackBehaviour class depends on an object that implements the IAttackable interface and a ScriptableObject asset of type AttackBehaviourSettings:

public class AttackBehaviour : StateMachineBehaviour<IAttackable, AttackBehaviourSettings>

When you create a component that inherits from one of the generic StateMachineBehaviour base classes, you’ll always also need to implement the Init function for receiving the initialization arguments.

public IAttackable Target { get; private set; }
private AttackBehaviourSettings settings;

protected override void Init(IAttackable target, AttackBehaviourSettings settings)
{
   Target = target;
   this.settings = settings;
}

State Machine Behaviour Initializers

You can use state machine behaviour initializers to specify the Init arguments that are used to initialize an instance of a StateMachineBehaviour<T…> derived class.

To achieve this do the following:

  1. Create a state machine behaviour that derives from StateMachineBehaviour<T…>.
  2. Create an AnimatorController asset.
  3. Add your state machine behaviour to one or more states in the AnimatorController.
  4. Add an Animator component to a GameObject.
  5. Assign the AnimatorController asset to the Animator. After this you should see an Init section inside the Animator component in the Inspector.
  6. Click on the + button in the Init section and select the option to generate an initializer for your state machine behaviour.
  7. Once the initializer class has been generated use the Animator’s Inspector to specify the initialization arguments.

Note that when initialization arguments are provided to a state machine behaviour using an initializer, the arguments will only get injected after the Awake and OnEnable event functions have already finished for the state machine behaviour. If you need to perform one-time setup for a state machine behaviour after they have received their Init arguments, you can either:

  1. Execute your setup function at the beginning of the OnStateEnter function. Use a boolean variable to keep track of whether or not setup has been performed yet or not.
  2. Execute your setup function at the end of the Init function.

OnAwake

Do not add an Awake function to classes that inherit from one of the generic StateMachineBehaviour classes, because the classes already define an Awake function. If you need to do something during the Awake event, override the OnAwake function instead.

OnReset

Do not add a Reset function to classes that inherit from one of the generic StateMachineBehaviour classes, because the classes already define a Reset function. If you need to do something during the Reset event, override the OnReset function instead.

Leave a Reply

Your email address will not be published. Required fields are marked *