Skip to content

Commit

Permalink
add TryGetState and obsolete the original GetState method (#40)
Browse files Browse the repository at this point in the history
* add trygetstate and obsolete the original getstate method

* bump version
  • Loading branch information
PrashantMohta authored Nov 3, 2024
1 parent 2e922c2 commit afcf587
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
42 changes: 29 additions & 13 deletions Futils/FsmUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,27 @@ public static FsmState AddState(this PlayMakerFSM fsm, string stateName)
return fsm.AddState(new FsmState(fsm.Fsm) { Name = stateName });
}

[Obsolete("Satchel.FsmUtil.GetState is deprecated, please use Satchel.FsmUtil.TryGetState or Satchel.FsmUtil.GetValidState instead.")]
public static FsmState GetState(this PlayMakerFSM fsm, string stateName)
{
return fsm.Fsm.GetState(stateName);
}

public static bool TryGetState(this PlayMakerFSM fsm, string stateName, out FsmState state)
{
state = fsm.Fsm.GetState(stateName);
return state != null;
}

public static FsmState GetValidState(this PlayMakerFSM fsm, string stateName)
{
if (fsm.TryGetState(stateName,out var state))
{
return state;
}
throw new ArgumentException($"FSM \"{fsm.FsmName}\" does not have state \"{stateName}\"");
}

public static FsmState CopyState(this PlayMakerFSM fsm, string origStateName, string newStateName)
{
var newState = new FsmState(fsm.Fsm.GetState(origStateName))
Expand Down Expand Up @@ -180,7 +196,7 @@ public static void ChangeTransition(this FsmState state, string onEventName, str
}
public static void ChangeTransition(this PlayMakerFSM fsm, string fromStateName, string onEventName, string toStateName)
{
fsm.GetState(fromStateName).ChangeTransition(onEventName, toStateName);
fsm.GetValidState(fromStateName).ChangeTransition(onEventName, toStateName);
}

public static void AddGlobalTransition(this PlayMakerFSM fsm, string onGlobalEventName, string toStateName)
Expand Down Expand Up @@ -210,7 +226,7 @@ public static FsmStateAction GetAction(this FsmState state, int index)
}
public static FsmStateAction GetAction(this PlayMakerFSM fsm, string stateName, int index)
{
return fsm.GetState(stateName).GetAction(index);
return fsm.GetValidState(stateName).GetAction(index);
}
public static T GetAction<T>(this FsmState state, int index) where T : FsmStateAction
{
Expand All @@ -219,7 +235,7 @@ public static T GetAction<T>(this FsmState state, int index) where T : FsmStateA

public static T GetAction<T>(this PlayMakerFSM fsm, string stateName, int index) where T : FsmStateAction
{
return fsm.GetState(stateName).GetAction<T>(index) as T;
return fsm.GetValidState(stateName).GetAction<T>(index) as T;
}

public static T[] GetActions<T>(this FsmState state) where T : FsmStateAction
Expand All @@ -237,7 +253,7 @@ public static T[] GetActions<T>(this FsmState state) where T : FsmStateAction
}
public static T[] GetActions<T>(this PlayMakerFSM fsm, string stateName) where T : FsmStateAction
{
return fsm.GetState(stateName).GetActions<T>();
return fsm.GetValidState(stateName).GetActions<T>();
}

public static T GetFirstActionOfType<T>(this FsmState state) where T : FsmStateAction
Expand Down Expand Up @@ -272,23 +288,23 @@ public static void InsertAction(this FsmState state, FsmStateAction action, int
}
public static void InsertAction(this PlayMakerFSM fsm, string stateName, FsmStateAction action, int index)
{
fsm.GetState(stateName).InsertAction(action, index);
fsm.GetValidState(stateName).InsertAction(action, index);
}
public static void AddAction(this FsmState state, FsmStateAction action)
{
state.InsertAction(action, state.Actions.Length);
}
public static void AddAction(this PlayMakerFSM fsm, string stateName, FsmStateAction action)
{
fsm.GetState(stateName).AddAction(action);
fsm.GetValidState(stateName).AddAction(action);
}
public static void AddFirstAction(this FsmState state, FsmStateAction action)
{
state.InsertAction(action, 0);
}
public static void AddFirstAction(this PlayMakerFSM fsm, string stateName, FsmStateAction action)
{
fsm.GetState(stateName).AddFirstAction(action);
fsm.GetValidState(stateName).AddFirstAction(action);
}

public static void RemoveAction(this FsmState state, int index)
Expand All @@ -307,7 +323,7 @@ public static void RemoveAction(this FsmState state, int index)
}
public static void RemoveAction(this PlayMakerFSM fsm, string stateName, int index)
{
fsm.GetState(stateName).RemoveAction(index);
fsm.GetValidState(stateName).RemoveAction(index);
}

public static void DisableAction(this FsmState state, int index)
Expand All @@ -317,7 +333,7 @@ public static void DisableAction(this FsmState state, int index)

public static void DisableAction(this PlayMakerFSM fsm, string state, int index)
{
fsm.GetState(state).DisableAction(index);
fsm.GetValidState(state).DisableAction(index);
}

public static void AddCustomAction(this FsmState state, Action method)
Expand All @@ -326,7 +342,7 @@ public static void AddCustomAction(this FsmState state, Action method)
}
public static void AddCustomAction(this PlayMakerFSM fsm, string stateName, Action method)
{
fsm.GetState(stateName).AddAction(new CustomFsmAction() { method = method });
fsm.GetValidState(stateName).AddAction(new CustomFsmAction() { method = method });
}

public static void AddCustomAction(this FsmState state, Action<FsmState> method)
Expand All @@ -335,23 +351,23 @@ public static void AddCustomAction(this FsmState state, Action<FsmState> method)
}
public static void AddCustomAction(this PlayMakerFSM fsm, string stateName, Action<PlayMakerFSM> method)
{
fsm.GetState(stateName).AddAction(new CustomFsmAction() { method = () => method(fsm) });
fsm.GetValidState(stateName).AddAction(new CustomFsmAction() { method = () => method(fsm) });
}
public static void InsertCustomAction(this FsmState state, Action method, int index)
{
state.InsertAction(new CustomFsmAction() { method = method }, index);
}
public static void InsertCustomAction(this PlayMakerFSM fsm, string stateName, Action method, int index)
{
fsm.GetState(stateName).InsertAction(new CustomFsmAction() { method = method }, index);
fsm.GetValidState(stateName).InsertAction(new CustomFsmAction() { method = method }, index);
}
public static void InsertCustomAction(this FsmState state, Action<FsmState> method, int index)
{
state.InsertAction(new CustomFsmAction() { method = () => method(state) }, index);
}
public static void InsertCustomAction(this PlayMakerFSM fsm, string stateName, Action<PlayMakerFSM> method, int index)
{
fsm.GetState(stateName).InsertAction(new CustomFsmAction() { method = () => method(fsm) }, index);
fsm.GetValidState(stateName).InsertAction(new CustomFsmAction() { method = () => method(fsm) }, index);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Utils/AssemblyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class AssemblyUtils
/// <summary>
/// Version of this Assembly
/// </summary>
public static string ver = "0.9.1";
public static string ver = "0.9.2";

/// <summary>
/// Gets Version string
Expand Down

0 comments on commit afcf587

Please sign in to comment.