There are a few ways to reference a PlayMakerFsm in your scripts:
1. Add a public PlayMakerFSM variable and link to the component.
C# example:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
using UnityEngine;
using HutongGames.PlayMaker;
public class MyBehaviour : MonoBehaviour
{
public PlayMakerFSM fsm;
void Update()
{
// getting fsm variables by name
FsmFloat floatVariable = fsm.FsmVariables.GetFsmFloat("myFloat");
// setting fsm variable value
floatVariable.Value = 0.5f;
// sending an event
fsm.SendEvent("myEvent");
}
}
2. Use GetComponent to get an PlayMakerFSM Component on a GameObject.
C# example:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: |
using UnityEngine; using HutongGames.PlayMaker; public class MyBehaviour : MonoBehaviour { private PlayMakerFSM fsm; void Update() { // find the PlayMakerFSM on a GameObject fsm = GameObject.FindWithTag("MyTag").GetComponent<PlayMakerFSM>(); // getting fsm variables by name FsmFloat floatVariable = fsm.FsmVariables.GetFsmFloat("myFloat"); // setting fsm variable value floatVariable.Value = 0.5f; // sending an event fsm.SendEvent("myEvent"); } } |
Unity script example:
1: 2: 3: 4: 5: 6: 7: |
var LivesFSM : PlayMakerFSM; //assign the FSM on the Lives obj LivesFSM = gameObject.FindWithTag("Lives").GetComponent.<PlayMakerFSM>(); var LivesRemaining : int; LivesRemaining= LivesFSM.FsmVariables.GetFsmInt("Remaining_Lives").Value; |
Another Unity script example:
1:
2:
3:
4:
5:
6:
var playerFSM : PlayMakerFSM;
playerFSM = gameObject.FindWithTag("playerModel").GetComponent.<PlayMakerFSM>(); //this finds the object with the FSM of interest
playerFSM.FsmVariables.GetFsmInt("I-am-a-FSM-fariable").Value = 500; //this sets the value of an Int for an FSM.
playerFSM.Fsm.Event("I-am-a-FSM-Event"); // This fires an event to the FSM
3. Use GetComponents and the FsmName property to find a specific FSM on a GameObject that has multiple FSMs
Get all PlayMakerFSM components on a GameObject using GetComponents<PlayMakerFSM>(), then loop through them and find the one with the right FsmName.
EDIT: There is now a static utility method in PlayMakerFSM to make this easier:
public static PlayMakerFSM FindFsmOnGameObject(GameObject go, string fsmName)