Godot Version
4.3
Question
I am working on a project in which I am trying to implement multi-threading for the first time; And the main issue / roadblock I am running into now and have in the past is when trying to access nodes, every time and way I have tried gives a null reference, I have tried:
-
%NodeName
-
$Scene/…/NodeName
-
$Scene.get_child(…).get_child(Node Index )
-
get_tree().root.get_child(Scene Index ).get_child(…).get_child(Node Index )
All turn up null/nill at runtime, but only when on separate threads, all of these work on the main thread. How are you suppose to access/reference nodes on separate threads?
Thanks.
I unfortunately never worked with GDScript before, but I did run into a similar issue before when I was implementing a third party DLL into my game using C#.
When I was working on that, I discovered that I can do something like this:
Dispatcher.SynchronizationContext.Post(dispatchedDevice =>
{
Array<Node> items = _itemsContainer.GetChildren();
foreach (Node item in items)
{
_itemsContainer.RemoveChild(item);
item.QueueFree();
}
}, device);
I would assume the Dispatcher is available one way or another via GDScript as well, as it’s a Godot built-in. There seems to be very little documentation about this unfortunately, but I hope this sets you on the right path!
Hopefully someone with more GDScript knowledge will be able to help with something more specific.
Would that work for purposes other than deleting it? Also I would then need to reference the _itemsContainer which seems to move the issues upwards also.
Oh no, all the code you see inside that is custom logic I wrote. None of that is needed, obviously. What you need is the Dispatcher and the SynchronizationContext, which is supposed to be able to “interact” with other threads.
They may have other names for GDScript, but after googling I was unable to find info on both of those for searching ‘godot 4.3’ followed by there names. Could you send me a link to the docs for them so I can look into them further?
Thanks for the help!
After some research it turns out that GDScript handles this very differently. Take a look here: