Skip to content

Commit 190bc88

Browse files
committed
extract Fsm -> scene name into method
remove whitespace at end of lines
1 parent f25cefe commit 190bc88

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

Hooks.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static void UnHookStateExited(FSMData data, Action<PlayMakerFSM> onStateE
171171
}
172172
}
173173
/// <summary>
174-
/// Hook that gets called when a state is entered by a transition (could be global or local). The transition from which it happened is passed into the action.
174+
/// Hook that gets called when a state is entered by a transition (could be global or local). The transition from which it happened is passed into the action.
175175
/// </summary>
176176
/// <param name="data">The data necessary to find the fsm to be edited</param>
177177
/// <param name="onStateEnteredFromTransition">The action that will be invoked when the state is entered, the parameter passed into the action is the fsm and the transition from which the state enter happened</param>
@@ -260,9 +260,30 @@ public static FSMHookHandle<Action<PlayMakerFSM, string>> CreateStateEnteredViaT
260260
public static FSMHookHandle<Action<PlayMakerFSM, string>> CreateStateExitedViaTransitionHook(FSMData data, Action<PlayMakerFSM, string> onStateExitViaTransition) =>
261261
new(StateExitedViaTransitionData, data, onStateExitViaTransition);
262262

263+
private static string GetSceneName(Fsm self)
264+
{
265+
if (self == null)
266+
{
267+
return "";
268+
}
269+
if (self.Owner == null)
270+
{
271+
return "";
272+
}
273+
if (self.Owner.gameObject == null)
274+
{
275+
return "";
276+
}
277+
if (self.Owner.gameObject.scene == null)
278+
{
279+
return "";
280+
}
281+
return self.Owner.gameObject.scene.name;
282+
}
283+
263284
private static void EnterState(Action<Fsm, FsmState> orig, Fsm self, FsmState state)
264285
{
265-
string sceneName = self.GameObject.scene.name;
286+
string sceneName = GetSceneName(self);
266287
string gameObject = self.GameObjectName;
267288
string fsmName = self.Name;
268289
string stateName = state.Name;
@@ -279,23 +300,22 @@ private static void EnterState(Action<Fsm, FsmState> orig, Fsm self, FsmState st
279300
{
280301
onStateEnter_3.TryInvokeActions(self.FsmComponent);
281302
}
282-
303+
283304
orig(self, state);
284305
}
285-
306+
286307
private static bool DoTransition(Func<Fsm, FsmTransition, bool, bool> orig, Fsm self, FsmTransition transition, bool isGlobal)
287308
{
288309
// a check in the normal code
289310
if (transition.ToFsmState == null)
290311
{
291312
return orig(self, transition, isGlobal);
292313
}
293-
294-
string sceneName = self.GameObject.scene.name;
314+
315+
string sceneName = GetSceneName(self);
295316
string gameObject = self.GameObjectName;
296317
string fsmName = self.Name;
297318

298-
299319
if (StateExitedViaTransitionData.TryGetValue(new FSMData(sceneName, gameObject, fsmName, self.ActiveStateName), out var onStateExitedViaTransition_1))
300320
{
301321
onStateExitedViaTransition_1.TryInvokeActions(self.FsmComponent, transition.EventName);
@@ -308,8 +328,7 @@ private static bool DoTransition(Func<Fsm, FsmTransition, bool, bool> orig, Fsm
308328
{
309329
onStateExitedViaTransition_3.TryInvokeActions(self.FsmComponent, transition.EventName);
310330
}
311-
312-
331+
313332
if (StateEnteredFromTransitionData.TryGetValue(new FSMData(sceneName, gameObject, fsmName, transition.ToState), out var onStateEnteredFromTransition_1))
314333
{
315334
onStateEnteredFromTransition_1.TryInvokeActions(self.FsmComponent, transition.EventName);
@@ -322,14 +341,13 @@ private static bool DoTransition(Func<Fsm, FsmTransition, bool, bool> orig, Fsm
322341
{
323342
onStateEnteredFromTransition_3.TryInvokeActions(self.FsmComponent, transition.EventName);
324343
}
325-
326-
344+
327345
return orig(self, transition, isGlobal);
328346
}
329-
347+
330348
private static void ExitState(Action<Fsm, FsmState> orig, Fsm self, FsmState state)
331349
{
332-
string sceneName = self.GameObject.scene.name;
350+
string sceneName = GetSceneName(self);
333351
string gameObject = self.GameObjectName;
334352
string fsmName = self.Name;
335353
string stateName = state.Name;
@@ -346,7 +364,7 @@ private static void ExitState(Action<Fsm, FsmState> orig, Fsm self, FsmState sta
346364
{
347365
onStateExit_3.TryInvokeActions(self.FsmComponent);
348366
}
349-
367+
350368
orig(self, state);
351369
}
352370

@@ -368,7 +386,7 @@ private static void TryInvokeActions(this Action<PlayMakerFSM> action, PlayMaker
368386
}
369387
}
370388
}
371-
389+
372390
private static void TryInvokeActions(this Action<PlayMakerFSM, string> action, PlayMakerFSM fsm, string transition)
373391
{
374392
if (action != null)

0 commit comments

Comments
 (0)