NodeCanvas Forums › Support › Having nextState for FSM
Hello, can you add the next state in FSM to use it in the OnExit() function ?
Hello,
I am sorry, but I am not sure I understood the question.
Can you please clarify what you mean? 🙂
Thanks.
Join us on Discord: https://discord.gg/97q2Rjh
Sure.
For the moment we have previousSate and currentState. But I want to created “ConditionnedAction” and the condition will be : “If the previousState is … launch action”, or “If the nextState is…launch action”. I will use this with SuperActionState in the onEnterActionList and onExitActionList. But i need the nextState the moment where the exitActionList is called.
Same thing, on the OnExit() of a state, it will be cool to have the next state, for doing different thing at this moment depending of the next state.
Ok I understand what you mean now.
Even though I think it is generally not a very good design approach to handle things on exit, based on what the upcomming state would be (maybe Im wrong), it is something easy to add.
Please do the following changes in FSM.cs just to confirm that is indeed what you are after and let me know.
1) Add public FSMState nextState{get; private set;}
in the class.
2) In EnterState method and right after the first two checks, add nextState = newState;
so that the code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
///Enter a state providing the state itself public bool EnterState(FSMState newState){ if (!isRunning){ Debug.LogWarning("Tried to EnterState on an FSM that was not running", this); return false; } if (newState == null){ Debug.LogWarning("Tried to Enter Null State"); return false; } nextState = newState; //... //... |
Let me know.
Thanks.
Join us on Discord: https://discord.gg/97q2Rjh
Hum, I don’t known if it’s a good design. But i use it when, for example, my character is in a particular state and in this state an action has to be done if it go to a specific state. But It does it only if it will go on this specific state.
But I can put the action in the beginning of the specific state with a previousState check, but I think it’s conceptually incorrect… Maybe I’m wrong or not…
Hello,
I would personally put the desired action at the beginning on the next sate, instead of at the end of the current one. 🙂
You can also add an intermediate state that has the action you want attached, but of course that is not ideal since it needs extra states to be created solely for this reason.
Does making this change to the code provides what you want to do?
If so I can add a “nextState” property in the next version. Just let me know of so.
Thanks.
Join us on Discord: https://discord.gg/97q2Rjh
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 =) .