Skip to content

Commit a2ad323

Browse files
committed
Added a method for swapping substate models
1 parent b9299a5 commit a2ad323

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

Runtime/StateGraph/StateMachineController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class StateMachineController : MonoBehaviour
3636
/// Jumps to a state via a JumpIn state using the JumpId to identify the JumpIn node.
3737
/// </summary>
3838
/// <param name="id">The JumpIn id of the destination JumpIn node</param>
39-
public void JumpTo(JumpId id)
39+
public virtual void JumpTo(JumpId id)
4040
{
4141
_stateMachine?.JumpTo(id);
4242
}
@@ -73,7 +73,7 @@ public virtual void Reset()
7373
/// <summary>
7474
/// Initializes the state machine when the script instance is being loaded.
7575
/// </summary>
76-
public void Awake()
76+
public virtual void Awake()
7777
{
7878
CreateStateMachine();
7979
_activated = _stateMachine != null;
@@ -83,7 +83,7 @@ public void Awake()
8383
/// Starts the state machine if it has been activated.
8484
/// This is called on the frame when a script is enabled just before any of the Update methods are called the first time.
8585
/// </summary>
86-
public void Start()
86+
public virtual void Start()
8787
{
8888
if(!_activated) return;
8989

@@ -96,7 +96,7 @@ public void Start()
9696
/// Updates the state machine if it has been activated.
9797
/// This is called every frame, if the MonoBehaviour is enabled.
9898
/// </summary>
99-
public void Update()
99+
public virtual void Update()
100100
{
101101
if (!_activated) return;
102102
_stateMachine?.Update();
@@ -106,7 +106,7 @@ public void Update()
106106
/// Performs physics-based updates on the state machine if it has been activated.
107107
/// This is called every fixed framerate frame, if the MonoBehaviour is enabled.
108108
/// </summary>
109-
public void FixedUpdate()
109+
public virtual void FixedUpdate()
110110
{
111111
if (!_activated) return;
112112
_stateMachine?.FixedUpdate();
@@ -115,7 +115,7 @@ public void FixedUpdate()
115115
/// <summary>
116116
/// Cleans up the state machine when the MonoBehaviour will be destroyed.
117117
/// </summary>
118-
public void OnDestroy()
118+
public virtual void OnDestroy()
119119
{
120120
if (!_activated) return;
121121
_stateMachine?.OnDestroy();

Runtime/StateGraph/States/BaseSubStateMachineState.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ public abstract class BaseSubStateMachineState : State
1515
public StateMachineModel Model
1616
{
1717
get => _model;
18-
protected set => _model = value;
18+
set => _model = value;
1919
}
2020

2121
[SerializeField] private StateMachineModel _model;
22+
private bool _started;
23+
private bool _entered;
2224

2325
public override void OnAwakeState()
2426
{
@@ -29,12 +31,14 @@ public override void OnAwakeState()
2931
public override void OnStartState()
3032
{
3133
SubStateMachine?.Start();
34+
_started = true;
3235
}
3336

3437
public override void OnEnterState()
3538
{
3639
if(SubStateMachine == null) return;
37-
40+
41+
_entered = true;
3842
SubStateMachine.Model.SetParent(SubStateMachine.Model);
3943
SubStateMachine.OnComplete += OnSubStateComplete;
4044
SubStateMachine.Enter();
@@ -56,6 +60,7 @@ public override void OnExitState()
5660

5761
SubStateMachine.OnComplete -= OnSubStateComplete;
5862
SubStateMachine.Exit();
63+
_entered = false;
5964
}
6065

6166
public override void OnDestroyState()
@@ -64,6 +69,23 @@ public override void OnDestroyState()
6469

6570
SubStateMachine?.OnDestroy();
6671
}
72+
73+
protected virtual void SwitchModel(StateMachineModel value)
74+
{
75+
if (!value) return;
76+
77+
SubStateMachine?.OnDestroy();
78+
SubStateMachine = null;
79+
80+
_model = value;
81+
CreateStateMachine();
82+
83+
if (!GameObject.activeInHierarchy || !_started) return;
84+
SubStateMachine?.Start();
85+
86+
if (!_entered) return;
87+
SubStateMachine?.Enter();
88+
}
6789

6890
protected virtual void CreateStateMachine()
6991
{

0 commit comments

Comments
 (0)