As I concluded before, the best part of PlayMaker can give you nice visual overview of a finite-state machine (FSM), an oft used construct in rapid game prototyping.
This overview is something you surely don’t have when implementing a FSM in code.
However, from a programmer’s point of view, it’s quite clumsy to use PlayMaker to add behavior to the FSM’s states.
The solution is to take the best of both worlds: creating states and connecting them using PlayMaker, and writing the states’ behavior in your favorite Unity programming language.
To be able to do this, you have to add events in PlayMaker’s FSM Editor. These events can be used to add the state transitions in a visual way, and can be raised in your code.
The following is an example state class to use with PlayMaker:
using UnityEngine;
using HutongGames.PlayMaker;
public class Idle : MonoBehaviour {
private PlayMakerFSM fsm;
// Use this for initialization
public virtual void Start () {
// Initialize the behavior
fsm = (PlayMakerFSM)gameObject.GetComponent(typeof(PlayMakerFSM));
}
// Update code goes here
public virtual void Update() {
// Call this to raise the event of your choice
fsm.Fsm.Event("FINISHED");
}
}
To add this code to a state, you need to add a ScriptControl Action in the FSM Editor, and then add a script.
Feel free to comment to this post and share your experiences!
