From 71ac2d4a9f7157bbe55fe922a1a02970ed4775d7 Mon Sep 17 00:00:00 2001 From: MasterDisaster64 Date: Tue, 6 May 2025 22:47:28 +0200 Subject: [PATCH 1/2] OnAction functions now return true if an action is actually performed, false if not --- src/Base/IActionable.cs | 4 ++-- src/StateMachine/HybridStateMachine.cs | 14 ++++++++------ src/StateMachine/StateMachine.cs | 8 ++++---- src/States/ActionState.cs | 8 ++++---- src/States/ActionStorage.cs | 18 ++++++++++++++---- src/States/DecoratedState.cs | 8 ++++---- src/States/ParallelStates.cs | 18 ++++++++++++------ 7 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/Base/IActionable.cs b/src/Base/IActionable.cs index 5482e62..8d3925c 100644 --- a/src/Base/IActionable.cs +++ b/src/Base/IActionable.cs @@ -7,8 +7,8 @@ namespace UnityHFSM /// public interface IActionable { - void OnAction(TEvent trigger); - void OnAction(TEvent trigger, TData data); + bool OnAction(TEvent trigger); + bool OnAction(TEvent trigger, TData data); } /// diff --git a/src/StateMachine/HybridStateMachine.cs b/src/StateMachine/HybridStateMachine.cs index bea33d8..26996f0 100644 --- a/src/StateMachine/HybridStateMachine.cs +++ b/src/StateMachine/HybridStateMachine.cs @@ -84,16 +84,18 @@ public override void OnExit() afterOnExit?.Invoke(this); } - public override void OnAction(TEvent trigger) + public override bool OnAction(TEvent trigger) { - actionStorage?.RunAction(trigger); - base.OnAction(trigger); + var result1 = actionStorage?.RunAction(trigger) ?? false; + var result2 = base.OnAction(trigger); + return result1 || result2; } - public override void OnAction(TEvent trigger, TData data) + public override bool OnAction(TEvent trigger, TData data) { - actionStorage?.RunAction(trigger, data); - base.OnAction(trigger, data); + var result1 = actionStorage?.RunAction(trigger, data) ?? false; + var result2 = base.OnAction(trigger, data); + return result1 || result2; } /// diff --git a/src/StateMachine/StateMachine.cs b/src/StateMachine/StateMachine.cs index ff07521..b842e83 100644 --- a/src/StateMachine/StateMachine.cs +++ b/src/StateMachine/StateMachine.cs @@ -762,10 +762,10 @@ public void TriggerLocally(TEvent trigger) /// Runs an action on the currently active state. /// /// Name of the action. - public virtual void OnAction(TEvent trigger) + public virtual bool OnAction(TEvent trigger) { EnsureIsInitializedFor("Running OnAction of the active state"); - (activeState as IActionable)?.OnAction(trigger); + return (activeState as IActionable)?.OnAction(trigger) ?? false; } /// @@ -775,10 +775,10 @@ public virtual void OnAction(TEvent trigger) /// Any custom data for the parameter. /// Type of the data parameter. /// Should match the data type of the action that was added via AddAction<T>(...). - public virtual void OnAction(TEvent trigger, TData data) + public virtual bool OnAction(TEvent trigger, TData data) { EnsureIsInitializedFor("Running OnAction of the active state"); - (activeState as IActionable)?.OnAction(trigger, data); + return (activeState as IActionable)?.OnAction(trigger, data) ?? false; } public StateBase GetState(TStateId name) diff --git a/src/States/ActionState.cs b/src/States/ActionState.cs index aa646c8..c17bd17 100644 --- a/src/States/ActionState.cs +++ b/src/States/ActionState.cs @@ -55,8 +55,8 @@ public ActionState AddAction(TEvent trigger, Action /// Name of the action. - public void OnAction(TEvent trigger) - => actionStorage?.RunAction(trigger); + public bool OnAction(TEvent trigger) + => actionStorage?.RunAction(trigger) ?? false; /// /// Runs an action with a given name and lets you pass in one parameter to the action function. @@ -65,8 +65,8 @@ public void OnAction(TEvent trigger) /// Name of the action. /// Data to pass as the first parameter to the action. /// Type of the data parameter. - public void OnAction(TEvent trigger, TData data) - => actionStorage?.RunAction(trigger, data); + public bool OnAction(TEvent trigger, TData data) + => actionStorage?.RunAction(trigger, data) ?? false; } /// diff --git a/src/States/ActionStorage.cs b/src/States/ActionStorage.cs index 5d4a15e..72cb674 100644 --- a/src/States/ActionStorage.cs +++ b/src/States/ActionStorage.cs @@ -77,8 +77,13 @@ public void AddAction(TEvent trigger, Action action) /// If the action is not defined / hasn't been added, nothing will happen. /// /// Name of the action. - public void RunAction(TEvent trigger) - => TryGetAndCastAction(trigger)?.Invoke(); + public bool RunAction(TEvent trigger) + { + var action = TryGetAndCastAction(trigger); + if (action is null) return false; + action.Invoke(); + return true; + } /// /// Runs an action with a given name and lets you pass in one parameter to the action function. @@ -87,7 +92,12 @@ public void RunAction(TEvent trigger) /// Name of the action. /// Data to pass as the first parameter to the action. /// Type of the data parameter. - public void RunAction(TEvent trigger, TData data) - => TryGetAndCastAction>(trigger)?.Invoke(data); + public bool RunAction(TEvent trigger, TData data) + { + var action = TryGetAndCastAction>(trigger); + if (action is null) return false; + action.Invoke(data); + return true; + } } } diff --git a/src/States/DecoratedState.cs b/src/States/DecoratedState.cs index 6e2b51d..bea3e35 100644 --- a/src/States/DecoratedState.cs +++ b/src/States/DecoratedState.cs @@ -84,14 +84,14 @@ public void Trigger(TEvent trigger) (state as ITriggerable)?.Trigger(trigger); } - public void OnAction(TEvent trigger) + public bool OnAction(TEvent trigger) { - (state as IActionable)?.OnAction(trigger); + return (state as IActionable)?.OnAction(trigger) ?? false; } - public void OnAction(TEvent trigger, TData data) + public bool OnAction(TEvent trigger, TData data) { - (state as IActionable)?.OnAction(trigger, data); + return (state as IActionable)?.OnAction(trigger, data) ?? false; } public override string GetActiveHierarchyPath() diff --git a/src/States/ParallelStates.cs b/src/States/ParallelStates.cs index 1c8ef6b..95858d9 100644 --- a/src/States/ParallelStates.cs +++ b/src/States/ParallelStates.cs @@ -177,20 +177,26 @@ public override void OnExitRequest() } } - public void OnAction(TEvent trigger) + public bool OnAction(TEvent trigger) { + var result = false; foreach (var state in states) { - (state as IActionable)?.OnAction(trigger); + if ((state as IActionable)?.OnAction(trigger) ?? false) + result = true; } + return result; } - public void OnAction(TEvent trigger, TData data) - { - foreach (var state in states) + public bool OnAction(TEvent trigger, TData data) + { + var result = false; + foreach (var state in states) { - (state as IActionable)?.OnAction(trigger, data); + if ((state as IActionable)?.OnAction(trigger, data) ?? false) + result = true; } + return result; } public void StateCanExit() From 01c91d38ae7819b4a3b8640573aaea07b15ec227 Mon Sep 17 00:00:00 2001 From: MasterDisaster64 Date: Tue, 6 May 2025 23:28:11 +0200 Subject: [PATCH 2/2] Made method summaries more accurate --- src/StateMachine/StateMachine.cs | 4 ++-- src/States/ActionState.cs | 4 ++-- src/States/ActionStorage.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/StateMachine/StateMachine.cs b/src/StateMachine/StateMachine.cs index b842e83..ff42e52 100644 --- a/src/StateMachine/StateMachine.cs +++ b/src/StateMachine/StateMachine.cs @@ -759,7 +759,7 @@ public void TriggerLocally(TEvent trigger) } /// - /// Runs an action on the currently active state. + /// Runs an action on the currently active state. Returns false if the state doesn't have the action. /// /// Name of the action. public virtual bool OnAction(TEvent trigger) @@ -769,7 +769,7 @@ public virtual bool OnAction(TEvent trigger) } /// - /// Runs an action on the currently active state and lets you pass one data parameter. + /// Runs an action on the currently active state and lets you pass one data parameter. Returns false if the state doesn't have the action. /// /// Name of the action. /// Any custom data for the parameter. diff --git a/src/States/ActionState.cs b/src/States/ActionState.cs index c17bd17..e7abd5f 100644 --- a/src/States/ActionState.cs +++ b/src/States/ActionState.cs @@ -52,7 +52,7 @@ public ActionState AddAction(TEvent trigger, Action /// Runs an action with the given name. - /// If the action is not defined / hasn't been added, nothing will happen. + /// If the action is not defined / hasn't been added, returns false. /// /// Name of the action. public bool OnAction(TEvent trigger) @@ -60,7 +60,7 @@ public bool OnAction(TEvent trigger) /// /// Runs an action with a given name and lets you pass in one parameter to the action function. - /// If the action is not defined / hasn't been added, nothing will happen. + /// If the action is not defined / hasn't been added, returns false. /// /// Name of the action. /// Data to pass as the first parameter to the action. diff --git a/src/States/ActionStorage.cs b/src/States/ActionStorage.cs index 72cb674..750fb21 100644 --- a/src/States/ActionStorage.cs +++ b/src/States/ActionStorage.cs @@ -74,7 +74,7 @@ public void AddAction(TEvent trigger, Action action) /// /// Runs an action with the given name. - /// If the action is not defined / hasn't been added, nothing will happen. + /// If the action is not defined / hasn't been added, returns false. /// /// Name of the action. public bool RunAction(TEvent trigger) @@ -87,7 +87,7 @@ public bool RunAction(TEvent trigger) /// /// Runs an action with a given name and lets you pass in one parameter to the action function. - /// If the action is not defined / hasn't been added, nothing will happen. + /// If the action is not defined / hasn't been added, returns false. /// /// Name of the action. /// Data to pass as the first parameter to the action.