NodeCanvas Forums › Support › Having nextState for FSM › Reply To: Having nextState for FSM
Oh yeah the code is great.
Make sure nextState = null at the end of the function. nextState has to be undefined in Update.
For the moment i don’t use it, i have to refactoring my code for testing if it is robust.
I also have done a ConditionnedAction :
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
using System.Collections; using System.Collections.Generic; using UnityEngine; using NodeCanvas.Framework; using ParadoxNotion.Design; using ParadoxNotion.Serialization; #if UNITY_EDITOR using UnityEditor; #endif public class ConditionnedAction : ActionTask { public ConditionList conditionTasks; public ActionList actionTasks; public override void OnValidate(ITaskSystem ownerSystem) { base.OnValidate(ownerSystem); SetOwnerSystem(ownerSystem); if (actionTasks == null) { actionTasks = (ActionList)Task.Create(typeof(ActionList), ownerSystem); actionTasks.executionMode = ActionList.ActionsExecutionMode.ActionsRunInParallel; } if (conditionTasks == null) { conditionTasks = (ConditionList)Task.Create(typeof(ConditionList), ownerSystem); conditionTasks.checkMode = ConditionList.ConditionsCheckMode.AllTrueRequired; } conditionTasks.OnValidate(ownerSystem); actionTasks.OnValidate(ownerSystem); conditionTasks.SetOwnerSystem(this.ownerSystem); actionTasks.SetOwnerSystem(this.ownerSystem); foreach (ConditionTask c in conditionTasks.conditions) { c.SetOwnerSystem(this.ownerSystem); } foreach (ActionTask a in actionTasks.actions) { a.SetOwnerSystem(this.ownerSystem); } } public override Task Duplicate(ITaskSystem newOwnerSystem) { var newConditionnedAction = (ConditionnedAction)base.Duplicate(newOwnerSystem); newConditionnedAction.actionTasks = (ActionList)actionTasks.Duplicate(newOwnerSystem); newConditionnedAction.conditionTasks = (ConditionList)conditionTasks.Duplicate(newOwnerSystem); return newConditionnedAction; } protected override void OnExecute() { conditionTasks.Enable(agent, blackboard); if (conditionTasks.CheckCondition(agent, blackboard)) actionTasks.ExecuteAction(agent, blackboard); } protected override void OnUpdate() { if (conditionTasks.CheckCondition(agent, blackboard)) { actionTasks.ExecuteAction(agent, blackboard); } } protected override void OnStop() { actionTasks.EndAction(null); conditionTasks.Disable(); } protected override void OnPause() { actionTasks.PauseAction(); } #if UNITY_EDITOR protected override void OnTaskInspectorGUI() { foreach (ConditionTask c in conditionTasks.conditions) { c.SetOwnerSystem(this.ownerSystem); } foreach (ActionTask a in actionTasks.actions) { a.SetOwnerSystem(this.ownerSystem); } GUILayout.Label("Conditions"); conditionTasks.ShowListGUI(); conditionTasks.ShowNestedConditionsGUI(); GUILayout.Space(10); GUILayout.Label("Actions"); actionTasks.ShowListGUI(); actionTasks.ShowNestedActionsGUI(); } public void DoSavePreset() { #if !UNITY_WEBPLAYER var path = EditorUtility.SaveFilePanelInProject("Save Preset", "", "ConditionnedAction", ""); if (!string.IsNullOrEmpty(path)) { System.IO.File.WriteAllText(path, JSONSerializer.Serialize(typeof(ConditionnedAction), this, true)); //true for pretyJson AssetDatabase.Refresh(); } #else Debug.LogWarning("Preset saving is not possible with WebPlayer as active platform"); #endif } public void DoLoadPreset() { #if !UNITY_WEBPLAYER var path = EditorUtility.OpenFilePanel("Load Preset", "Assets", "ConditionnedAction"); if (!string.IsNullOrEmpty(path)) { var json = System.IO.File.ReadAllText(path); var list = JSONSerializer.Deserialize<ConditionnedAction>(json); this.actionTasks = list.actionTasks; this.conditionTasks = list.conditionTasks; this.actionTasks.SetOwnerSystem(this.ownerSystem); this.conditionTasks.SetOwnerSystem(this.ownerSystem); foreach (var a in this.actionTasks.actions) { a.SetOwnerSystem(this.ownerSystem); } foreach (var c in this.conditionTasks.conditions) { c.SetOwnerSystem(this.ownerSystem); } } #else Debug.LogWarning("Preset loading is not possible with WebPlayer as active platform"); #endif } #endif } |
This was designed to be used with previousState and nextState condition. I will keep you informed if it worked great =) .