Skip to content

Commit 5ed687f

Browse files
committed
Added a reference to the StateMachineController on the State which removes the need to get the controller via GetComponent. The Controller can then be cast to a derived implementation to retrieve references from it directly rather than having to add and retrieve references from SharedData which comes with an overhead
1 parent f3c9261 commit 5ed687f

6 files changed

Lines changed: 14 additions & 12 deletions

File tree

Runtime/StateGraph/State.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public abstract class State : ScriptableObject
1818
public StateMachine StateMachine { get; set; }
1919
public ISharedData SharedData { get; set; }
2020
public TransitionEventData TransitionData { get; set; }
21+
public StateMachineController Controller { get; set; }
2122

2223
/// <summary>
2324
/// The OnStart method is mapped the state machines Awake method

Runtime/StateGraph/StateMachine.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ public class StateMachine
2828
private Dictionary<JumpId, StateNodeModel> _jumpNodeLookup = new();
2929
private CancellationTokenSource _cancellationTokenSource = new();
3030

31-
public StateMachine(StateMachineModel model, GameObject gameObject)
31+
public StateMachine(StateMachineModel model, StateMachineController controller)
3232
{
3333
SharedData = new SharedData();
34-
Initialize(model, gameObject);
34+
Initialize(model, controller);
3535
}
3636

37-
public StateMachine(StateMachineModel model, GameObject gameObject, ISharedData sharedData = null)
37+
public StateMachine(StateMachineModel model, StateMachineController controller, ISharedData sharedData = null)
3838
{
3939
SharedData = sharedData ?? new SharedData();
40-
Initialize(model, gameObject);
40+
Initialize(model, controller);
4141
}
4242

4343
public void Update()
@@ -128,10 +128,10 @@ public void OnDestroy()
128128
Model = null;
129129
}
130130

131-
private void Initialize(StateMachineModel model, GameObject gameObject)
131+
private void Initialize(StateMachineModel model, StateMachineController controller)
132132
{
133133
Model = StateMachineModel.CreateInstance(model);
134-
Model.Initialize(gameObject, this, SharedData);
134+
Model.Initialize(controller, this, SharedData);
135135

136136
CreateNodeLookupTable();
137137
CreateTransitionLookupTable();

Runtime/StateGraph/StateMachineController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void CreateUniqueId()
146146
private void CreateStateMachine()
147147
{
148148
if(!_model) return;
149-
_stateMachine = new StateMachine(_model, gameObject);
149+
_stateMachine = new StateMachine(_model, this);
150150
}
151151
}
152152
}

Runtime/StateGraph/StateMachineModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ protected override void ValidateSubAssets()
135135
StateMachineValidator.Validate(this);
136136
}
137137

138-
public void Initialize(GameObject gameObject, StateMachine stateMachine, ISharedData sharedData)
138+
public void Initialize(StateMachineController controller, StateMachine stateMachine, ISharedData sharedData)
139139
{
140-
if (!gameObject || stateMachine == null) return;
140+
if (!controller || stateMachine == null) return;
141141

142142
foreach (var stateNode in Nodes)
143143
{
@@ -146,7 +146,8 @@ public void Initialize(GameObject gameObject, StateMachine stateMachine, IShared
146146
var instantiatedState = Instantiate(stateNode.State);
147147
if (!instantiatedState) continue;
148148

149-
instantiatedState.GameObject = gameObject;
149+
instantiatedState.GameObject = controller.gameObject;
150+
instantiatedState.Controller = controller;
150151
instantiatedState.StateMachine = stateMachine;
151152
instantiatedState.SharedData = sharedData;
152153
stateNode.State = instantiatedState;

Runtime/StateGraph/States/BaseParallelSubStateMachineState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected virtual void CreateStateMachines()
8686
{
8787
if (model == null) continue;
8888

89-
var subSubStateMachine = new StateMachine(model, GameObject, SharedData);
89+
var subSubStateMachine = new StateMachine(model, Controller, SharedData);
9090
subSubStateMachine.SetParent(StateMachine);
9191
SubStateMachines.Add(subSubStateMachine);
9292
}

Runtime/StateGraph/States/BaseSubStateMachineState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected virtual void CreateStateMachine()
9797
{
9898
if(!_model) return;
9999

100-
SubStateMachine = new StateMachine(_model, GameObject, SharedData);
100+
SubStateMachine = new StateMachine(_model, Controller, SharedData);
101101
SubStateMachine.SetParent(StateMachine);
102102
}
103103

0 commit comments

Comments
 (0)