NodeCanvas is built around the concept that the nodes of the graph are responsible only for the flow and not what and if it happens. Tasks are responsible for that and thus there are two kinds of Tasks those being Action Task and Condition Task. Tasks are assigned to nodes that require them and thus Tasks can be reusable between different types of systems. This concept allows to decouple the graph design from the actual implementation of actions or conditions thus allow for a non-destructive workflow.
NodeCanvas includes a Task Wizard utility window that makes it easier for you to create new tasks. The Task Wizard can be accessed via the right click menu in your “Assets” and under “Create/ParadoxNotion/NodeCanvas/New Task”.
In short, to create an Action Task you must derive from the ActionTask base class and override the virtual methods as needed. When the Action is complete you must call EndAction(bool) to end the Action either in success or failure depending on the bool argument passed and that’s it!
Following is the core API that you can use to create your own action tasks.
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 |
//This is the agent for whom the action will take place. public Component agent {get;} //This is the blackboard that you can use to read/write variables manualy if needed. public IBlackboard blackboard {get;} //This is the time in seconds the action is running. public float elapsedTime {get;} //Called only the first time the action is executed and before anything else. //Return null if everything is OK. Return an info string if there is a problem. virtual protected string OnInit() //Called once when the action is executed. virtual protected void OnExecute() //Called every frame while the action is running. virtual protected void OnUpdate() //Called when the action stops for any reason. //Either because you called EndAction or cause the action was interrupted. virtual protected void OnStop() //Called when the action is paused comonly when it was still running while the behaviour graph gets paused. virtual protected void OnPause() //Send an event to the behaviour graph. Use this along with the CheckEvent condition. protected void SendEvent(string) //Similar to above, but sends a value along with the event. protected void SendEvent<T>(string, T) //You can use coroutines from within action task as normal. protected Coroutine StartCoroutine(IEnumerator) //You must call this to end the action. You can call this from wherever you want, //although typicaly is done in either OnExecute or OnUpdate public void EndAction(bool) |
A simple action would be like this:
1 2 3 4 5 6 7 8 9 10 |
using UnityEngine; using NodeCanvas.Framework; public class SimpleAction : ActionTask{ protected override void OnExecute(){ Debug.Log("My agent is " + agent.name); EndAction(true); } } |
A simple delay would be like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
using UnityEngine; using NodeCanvas.Framework; public class WaitAction : ActionTask { public float timeToWait; protected override void OnUpdate(){ if (elapsedTime > timeToWait) EndAction(true); } } |
In short, to create a Condition Task you must derive from the ConditionTask base class, override the virtual OnCheck method and return the result.
Following is the core API for creating custom condition tasks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//This is the agent for whom the action will take place. public Component agent {get;} //This is the blackboard that you can use to read/write variables manualy if needed. public IBlackboard blackboard {get;} //Same as action task. virtual protected string OnInit() //This is called when the condition is checked. Override and return true or false. virtual protected bool OnCheck() //This is a helper method to make the condition return true or false from outside the OnCheck method. protected void YieldReturn(bool) |
A simple condition example.
1 2 3 4 5 6 7 8 9 |
using UnityEngine; using NodeCanvas.Framework; public class EmptyCondition : ConditionTask{ protected override bool OnCheck(){ return true; } } |
© Paradox Notion 2014-2024. All rights reserved.