Godot Version
4.4.1
Question
i have Problems with mulithreading.
script:
var factions_units : Dictionary [Faction,Array]
...
func get_unit_faction(p_unit : Unit)->Faction:
print("Faction Keys" + str(factions_units.keys()))
for faction : Faction in factions_units:
print(faction.name)
if factions_units[faction].has(p_unit):
return faction
return null
Faction is a Node2D containing some meta information about the faction and the unit nodes.
class_name Faction
extends Node2D
@export var is_ai_controlled : bool = true
@export var allys : Array[Faction]
@export var enemys : Array[Faction]
func _ready() -> void:
y_sort_enabled = true
When i use WorkerThreadPool the outputs are
outpunts Faction Keys[, ]
outpunts Green
The dictionary is not changed while the WorkerThreadPool ist busy. Why are there two empty(?) keys but the faction node can still be accessed?
The Programm than crasches on if factions_units[faction].has(p_unit):
Invalid access to property or key of type ‘Node2D (Faction)’ on a base object of type ‘Dictionary[Faction, Array]’.
if im doing it without a WorkerThreadPool the output is
Faction Keys[Green:<Node2D#134150622907>, Red:<Node2D#136314883900>]
Green
The Script works without a WorkerThreadPool
since its working without threads, i wonder what is causing this? Its my first time working with threads. Im only Reading and not mutating data. According to the documentation, dicts a threadsafe if you want to read them.
factions_units[factions_units.keys()[0]][0].name
gives me the node name of the unit i expected, The data seems to be here but not accessible as im used to.