Skip to content

Commit d3e5d3a

Browse files
committed
For error thrown in the AddProperties method of BaseStateNodeView
1 parent 7a3266a commit d3e5d3a

4 files changed

Lines changed: 27 additions & 22 deletions

File tree

Editor/StateGraph/Nodes/BaseStateNodeView.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using UnityEngine;
1818
using UnityEngine.Assertions;
1919
using UnityEngine.UIElements;
20-
using Debug = UnityEngine.Debug;
2120

2221
namespace Nonatomic.VSM2.Editor.StateGraph.Nodes
2322
{
@@ -136,11 +135,12 @@ protected virtual void UpdatePosition()
136135
{
137136
var position = NodeModel.Position;
138137

139-
var rect = this.GetPosition();
138+
var rect = GetPosition();
140139
rect.position = position;
141140

142141
SetPosition(rect);
143142

143+
if(StateMachineModel == null) return;
144144
EditorUtility.SetDirty(StateMachineModel);
145145
EditorApplication.delayCall += () => AssetDatabase.SaveAssets();
146146
}
@@ -403,6 +403,9 @@ protected virtual VisualElement CreatePropertyContainer()
403403

404404
protected virtual void AddProperties(VisualElement container)
405405
{
406+
if (NodeModel == null) return;
407+
if (NodeModel.State == null) return;
408+
406409
var scrollView = new ScrollView();
407410
container.Add(scrollView);
408411

Editor/StateGraph/StateNodeGraphStateManager.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,29 @@ public class StateNodeGraphStateManager : NodeGraphStateManager
1414

1515
public StateNodeGraphStateManager(string id) : base(id)
1616
{
17-
17+
// ...
1818
}
1919

2020
public void LoadModelFromStateController()
2121
{
22-
if (string.IsNullOrEmpty(StateControllerId)) return;
23-
22+
// Look for the controller that matches the stored ID.
2423
var stateMachines = GameObject.FindObjectsByType<StateMachineController>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
25-
var stateMachineController = stateMachines.FirstOrDefault(smc => smc.Id == StateControllerId);
26-
if (!stateMachineController) return;
27-
28-
SetModel(stateMachineController.Model);
24+
var controller = stateMachines.FirstOrDefault(smc => smc.Id == StateControllerId);
25+
26+
if (controller)
27+
{
28+
SetModel(controller.Model);
29+
}
30+
else
31+
{
32+
Debug.LogWarning($"StateMachineController with ID {StateControllerId} not found.");
33+
}
2934
}
3035

3136
public void SetStateControllerId(string id)
3237
{
38+
if (string.IsNullOrEmpty(id)) return;
39+
3340
StateControllerId = id;
3441
SaveState();
3542
}
@@ -43,16 +50,14 @@ protected override void ResetState()
4350
public override void LoadState()
4451
{
4552
base.LoadState();
46-
4753
StateControllerId = EditorPrefs.GetString(GetKey(StateMachineControllerIdKey));
4854
}
4955

5056
public override void SaveState()
5157
{
5258
base.SaveState();
53-
5459
if (!Model) return;
55-
60+
5661
EditorPrefs.SetString(GetKey(StateMachineControllerIdKey), StateControllerId);
5762
}
5863
}

Editor/StateGraph/VisualElements/StateGraphView.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,8 @@ private void HandlePlayModeStateChanged(PlayModeStateChange stateChange)
221221
var stateManager = (StateNodeGraphStateManager) StateManager;
222222
var gridPos = stateManager.GridPosition;
223223

224-
switch (stateChange)
225-
{
226-
case PlayModeStateChange.EnteredPlayMode:
227-
case PlayModeStateChange.EnteredEditMode:
228-
case PlayModeStateChange.ExitingEditMode:
229-
case PlayModeStateChange.ExitingPlayMode:
230-
stateManager.LoadModelFromStateController();
231-
PopulateGraph(StateManager.Model, recentre: true);
232-
break;
233-
}
224+
stateManager.LoadModelFromStateController();
225+
PopulateGraph(StateManager.Model, recentre: true);
234226

235227
EditorApplication.delayCall += ()=>
236228
{

Runtime/StateGraph/StateMachineController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,10 @@ private void CreateStateMachine()
148148
if(!_model) return;
149149
_stateMachine = new StateMachine(_model, this);
150150
}
151+
152+
private void OnValidate()
153+
{
154+
CreateUniqueId();
155+
}
151156
}
152157
}

0 commit comments

Comments
 (0)