1. Create new action

Create new action

from https://hutonggames.fogbugz.com/default.asp?W350

Playmaker automatically discovers any class derived from FsmStateAction in the project. All discovered actions appear in the Action Browser.

Actions are modular building blocks that define the behavior of a state. Writing a custom action is very similar to writing a MonoBehaviour script. However, unlike MonoBehaviours, Actions can be moved around in the State Inspector and they execute in sequence. See Actions Overview.

The main differences from MonoBehaviours:

Owner

Reference the GameObject that owns the FSM using the Owner property. For example:

Owner.transform.position = new Vector3(0,0,0);

Awake

Called when an action is first loaded. Use this for expensive initialization operations.

Also add physics handlers here. E.g., Fsm.HandleOnCollisionEnter = true;

See MonoBehaviour Events below.

OnEnter

Instead of using Awake(), Start(), or OnEnable() use OnEnter(). OnEnter is called when the state becomes active. This lets you initialize an action. Many actions also do all their work in OnEnter.

OnUpdate

Instead of Update() use OnUpdate(). OnUpdate should look very similar to a MonoBehaviour Update.

OnExit

Instead of OnDisable(), or OnDestroy() use OnExit(). OnExit is called before leaving the current state. This lets you cleanup or perform optional operations on leaving a state.

Finish

If an action has a definable completion criteria, it should call Finish() when it is met. When all actions on a state have finished, a FINISHED event is sent that can be used to transition to another state.

Events

Playmaker FSMs are event driven - they change state when they receive events. An action can send an event using Fsm.Event(eventName). NOTE: If the event causes a state change, OnExit is called immediately on all actions in the current state that have started but not finished; the FSM then switches state, and OnEnter is then called on each action in the new state.

Variables

Public action variables are exposed in the Action Editor the same way public MonoBehaviour variables are exposed in the Unity Inspector. An FSM also has a list of named variables that can be used in action parameters. Fsm Variable types use an Fsm prefix in front of their wrapped type (e.g., FsmInt, FsmFloat, FsmString...). Typically you get/set the underlying value of the fsm variable using the Value property (e.g., numLives.Value -= 1). See Using Fsm Variables.

Logging

Actions can call Log(), LogWarning(), and LogError() to create nicely formatted log entries. Warnings and errors appear in the PlayMaker Log Window and the Unity Console. Other log entries show only in the PlayMaker Log Window.

ErrorCheck

Actions can provide an optional ErrorCheck() function. This function is called by the Error Checker to validate action setup. Use this function to perform custom setup checks and return an error string (or null if no errors are found). Note: You can also use RequiredField and CheckForComponent Attributes to validate parameters. See Action Attributes.

Every Frame

Many actions have an option to repeat every frame. By convention this is defined as: public bool everyFrame, and should be the last parameter in the action. Actions that repeat every frame do not call Finish().

Reset

Actions should provide a Reset() function that initializes parameters to useful default values.

MonoBehaviour Events

Actions have built in support for the following MonoBehaviour events:

    • OnGUI

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnGUI = true; See SetGuiDepth for an example.

    • OnFixedUpdate

        • to use this, you have to explicitly declare its usage using Fsm.HandleFixedUpdate = true; Set SetVelocity for an example.

    • OnLateUpdate

    • DoCollisionEnter

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnCollisionEnter = true; See CollisionEvent for an example.

    • DoCollisionStay

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnCollisionStay = true; See CollisionEvent for an example.

    • DoCollisionExit

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnCollisionExit = true; See CollisionEvent for an example.

    • DoTriggerEnter

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnTriggerEnter = true; See TriggerEvent for an example.

    • DoTriggerStay

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnTriggerStay = true; See TriggerEvent for an example.

    • DoTriggerExit

        • to use this, you have to explicitly declare its usage using Fsm.HandleOnTriggerExit = true; See TriggerEvent for an example.

The code in these functions will generally be identical to code used in a MonoBehaviour.

Example

from https://hutonggames.fogbugz.com/default.asp?W351

A simple action written in C# that gets the System DateTime and stores it in a string variable:

This action should be fairly self explanatory, but here's a quick overview:

Attributes

You can mark up actions with Attributes like ActionCategory, Tooltip, and UIHint. See Action Attributes.

[ActionCategory(ActionCategory.Time)]

Says this action should appear in the Time category in the Action Browser.

Note, you can use a built-in category in the ActionCategory enum, or use a string to define a custom category.

[Tooltip("...")]

Used on classes and public fields to show descriptions and rollover tooltips in the Playmaker editor.

[UIHint(UIHint.Variable)]

UIHint.Variable gives the user a variable dropdown allowing them to select a variable.

Reset

All actions should initialize public variables to useful default values.

Reset is called when the action is added to a state and when the user selects Reset from the Action Editor Settings Menu.

OnEnter

OnEnter is called when the state becomes active. Many simple actions do all their work in OnEnter.

storeString.Value = DateTime.Now.ToString(format.Value);

This statement is the heart of this simple action, storing DateTime as a string using the supplied format.

Note the use of the Value property to get at the FsmString's wrapped string value.

If an action can finish, it should call Finish() when it finishes.

When all actions on a state have finished, the system sends a FINISHED event that can be used to transition to a new state.