Skip to content

Make OnAction() return true or false based on whether an action was performed #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Base/IActionable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace UnityHFSM
/// </summary>
public interface IActionable<TEvent>
{
void OnAction(TEvent trigger);
void OnAction<TData>(TEvent trigger, TData data);
bool OnAction(TEvent trigger);
bool OnAction<TData>(TEvent trigger, TData data);
}

/// <inheritdoc />
Expand Down
14 changes: 8 additions & 6 deletions src/StateMachine/HybridStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TData>(TEvent trigger, TData data)
public override bool OnAction<TData>(TEvent trigger, TData data)
{
actionStorage?.RunAction<TData>(trigger, data);
base.OnAction<TData>(trigger, data);
var result1 = actionStorage?.RunAction<TData>(trigger, data) ?? false;
var result2 = base.OnAction<TData>(trigger, data);
return result1 || result2;
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/StateMachine/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,26 +759,26 @@ public void TriggerLocally(TEvent trigger)
}

/// <summary>
/// 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.
/// </summary>
/// <param name="trigger">Name of the action.</param>
public virtual void OnAction(TEvent trigger)
public virtual bool OnAction(TEvent trigger)
{
EnsureIsInitializedFor("Running OnAction of the active state");
(activeState as IActionable<TEvent>)?.OnAction(trigger);
return (activeState as IActionable<TEvent>)?.OnAction(trigger) ?? false;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <param name="data">Any custom data for the parameter.</param>
/// <typeparam name="TData">Type of the data parameter.
/// Should match the data type of the action that was added via <c>AddAction&lt;T&gt;(...).</c></typeparam>
public virtual void OnAction<TData>(TEvent trigger, TData data)
public virtual bool OnAction<TData>(TEvent trigger, TData data)
{
EnsureIsInitializedFor("Running OnAction of the active state");
(activeState as IActionable<TEvent>)?.OnAction<TData>(trigger, data);
return (activeState as IActionable<TEvent>)?.OnAction<TData>(trigger, data) ?? false;
}

public StateBase<TStateId> GetState(TStateId name)
Expand Down
12 changes: 6 additions & 6 deletions src/States/ActionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ public ActionState<TStateId, TEvent> AddAction<TData>(TEvent trigger, Action<TDa

/// <summary>
/// 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.
/// </summary>
/// <param name="trigger">Name of the action.</param>
public void OnAction(TEvent trigger)
=> actionStorage?.RunAction(trigger);
public bool OnAction(TEvent trigger)
=> actionStorage?.RunAction(trigger) ?? false;

/// <summary>
/// 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.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <param name="data">Data to pass as the first parameter to the action.</param>
/// <typeparam name="TData">Type of the data parameter.</typeparam>
public void OnAction<TData>(TEvent trigger, TData data)
=> actionStorage?.RunAction<TData>(trigger, data);
public bool OnAction<TData>(TEvent trigger, TData data)
=> actionStorage?.RunAction<TData>(trigger, data) ?? false;
}

/// <inheritdoc />
Expand Down
22 changes: 16 additions & 6 deletions src/States/ActionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,30 @@ public void AddAction<TData>(TEvent trigger, Action<TData> action)

/// <summary>
/// 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.
/// </summary>
/// <param name="trigger">Name of the action.</param>
public void RunAction(TEvent trigger)
=> TryGetAndCastAction<Action>(trigger)?.Invoke();
public bool RunAction(TEvent trigger)
{
var action = TryGetAndCastAction<Action>(trigger);
if (action is null) return false;
action.Invoke();
return true;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="trigger">Name of the action.</param>
/// <param name="data">Data to pass as the first parameter to the action.</param>
/// <typeparam name="TData">Type of the data parameter.</typeparam>
public void RunAction<TData>(TEvent trigger, TData data)
=> TryGetAndCastAction<Action<TData>>(trigger)?.Invoke(data);
public bool RunAction<TData>(TEvent trigger, TData data)
{
var action = TryGetAndCastAction<Action<TData>>(trigger);
if (action is null) return false;
action.Invoke(data);
return true;
}
}
}
8 changes: 4 additions & 4 deletions src/States/DecoratedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public void Trigger(TEvent trigger)
(state as ITriggerable<TEvent>)?.Trigger(trigger);
}

public void OnAction(TEvent trigger)
public bool OnAction(TEvent trigger)
{
(state as IActionable<TEvent>)?.OnAction(trigger);
return (state as IActionable<TEvent>)?.OnAction(trigger) ?? false;
}

public void OnAction<TData>(TEvent trigger, TData data)
public bool OnAction<TData>(TEvent trigger, TData data)
{
(state as IActionable<TEvent>)?.OnAction<TData>(trigger, data);
return (state as IActionable<TEvent>)?.OnAction<TData>(trigger, data) ?? false;
}

public override string GetActiveHierarchyPath()
Expand Down
18 changes: 12 additions & 6 deletions src/States/ParallelStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TEvent>)?.OnAction(trigger);
if ((state as IActionable<TEvent>)?.OnAction(trigger) ?? false)
result = true;
}
return result;
}

public void OnAction<TData>(TEvent trigger, TData data)
{
foreach (var state in states)
public bool OnAction<TData>(TEvent trigger, TData data)
{
var result = false;
foreach (var state in states)
{
(state as IActionable<TEvent>)?.OnAction(trigger, data);
if ((state as IActionable<TEvent>)?.OnAction(trigger, data) ?? false)
result = true;
}
return result;
}

public void StateCanExit()
Expand Down