While the recommended way of working with FSMs and NodeCanvas in general, is to create Action & Condition Tasks and use those on an Action State for example, it is very possible to create your own FSM States Nodes as an alternative.
To do so you simply have to create a class deriving from FSMState and override the methods as needed, then call Finish() when the state has finished. As soon as you create that class, it will show up to be added to the FSM canvas. You can even specify a custom Icon if you want so. Here is a simple delay State:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using UnityEngine; using NodeCanvas.Framework; using ParadoxNotion.Design; namespace NodeCanvas.StateMachines{ [Category("My States")] [Icon("SomeIconName")] //Icon must be in a Resources folder public class SampleState : FSMState { public BBParameter<float> timeout; private float timer; //Called once before the state Enters for the first time protected override void OnInit(){ Debug.Log("Init"); } //When the state is entered. Not when it is resumed though protected override void OnEnter(){ Debug.Log("Enter"); } //As long as the state is active protected override void OnUpdate(){ timer += Time.deltaTime; if (timer >= timeout.value) Finish(); } //When the state was active and another state entered thus this exits. Also when the whole FSM stops. protected override void OnExit(){ Debug.Log("Exit"); timer = 0; } //When the state was active and FSM paused protected override void OnPause(){ Debug.Log("Pause"); } } } |
That’s it, you now have a custom delay state. Remember that double clicking on a node, opens it up in your IDE.
Finally, here are some important inherited properties and methods from the FSMState class.
FSM FSM
The parent FSM graph of this state.
Component graphAgent
The agent of the FSM.
Blackboard graphBlackboard
The blackboard of the FSM.
float elapsedTime
The time in seconds this state is running.
void Finish()
Call to finish the state.
void SendEvent(string name)
Call to send an event to the FSM graph.
© Paradox Notion 2014-2024. All rights reserved.