fix: check that we're accessing the tree only when possible.

This commit is contained in:
redglow
2026-07-17 15:35:12 +02:00
parent d689e9ea1d
commit 7c3bea58f8
+17 -5
View File
@@ -1,8 +1,9 @@
#if TOOLS #if TOOLS
using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Godot; using Godot;
using Godot.Collections; using Array = Godot.Collections.Array;
namespace GodotHostTest.GodotDI.Editor; namespace GodotHostTest.GodotDI.Editor;
@@ -18,9 +19,10 @@ public partial class Plugin : EditorPlugin
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private ScopeInspectorPlugin? _plugin; private ScopeInspectorPlugin? _plugin;
private SceneTreeTimer? _sceneTreeTimer; private SceneTreeTimer? _sceneTreeTimer;
private readonly object _sceneTreeTimerLock = new();
private bool _treeChangesRegistered; private bool _treeChangesRegistered;
public override void _EnterTree() public override void _EnterTree()
@@ -54,7 +56,7 @@ public partial class Plugin : EditorPlugin
OnSceneChanged(root); OnSceneChanged(root);
} }
private void OnSceneChanged(Node? sceneRoot) private async void OnSceneChanged(Node? sceneRoot)
{ {
// register to tree changes, in case the registration wasn't possible before // register to tree changes, in case the registration wasn't possible before
RegisterTreeChanges(); RegisterTreeChanges();
@@ -63,16 +65,26 @@ public partial class Plugin : EditorPlugin
if (sceneRoot == null) return; if (sceneRoot == null) return;
// apply debounce // apply debounce
var editedSceneRoot = EditorInterface.Singleton.GetEditedSceneRoot();
if (!editedSceneRoot.IsInsideTree()) await ToSignal(editedSceneRoot, Node.SignalName.TreeEntered);
var tree = editedSceneRoot.GetTree();
if (tree == null)
throw new InvalidOperationException("editedSceneRoot.GetTree() was null even after TreeEntered");
lock (_sceneTreeTimerLock)
{
if (_sceneTreeTimer != null) if (_sceneTreeTimer != null)
{ {
_sceneTreeTimer.SetTimeLeft(DebounceDelay); _sceneTreeTimer.SetTimeLeft(DebounceDelay);
return; return;
} }
_sceneTreeTimer = _sceneTreeTimer = tree.CreateTimer(DebounceDelay);
EditorInterface.Singleton.GetEditedSceneRoot().GetTree().CreateTimer(DebounceDelay);
_sceneTreeTimer.Timeout += () => ActualOnSceneChanged(sceneRoot); _sceneTreeTimer.Timeout += () => ActualOnSceneChanged(sceneRoot);
} }
}
private void ActualOnSceneChanged(Node sceneRoot) private void ActualOnSceneChanged(Node sceneRoot)
{ {