Please see the screenshots for a visual of this not working.
UID setting is handled by GameData (autoload class) and anything that is a GameObject (physically present in game world) requests a UID upon being ready if its UID is not already set (in other words not already non-zero)
This is threaded with a MutEx thjat should be structured perfectly fine but obviously I am missing something here as it's not working perfectly fine.
GameData
extends Node
static var cell_dimensions := Vector3(1.0, 1.0, 1.0)
var object_uid : int = 100_000_000_000_000
var object_uid_mutex := Mutex.new()
var game_object: GameObject
static func set_dimensions(dimension_data: Vector3) -> void:
cell_dimensions = dimension_data
static func get_dimensions() -> Vector3:
return cell_dimensions
func threaded_set_object_uid(object: GameObject) -> void:
object_uid_mutex.lock() # Lock the thread and data
# make a new thread and set the UID
# CRITICAL INFO MUTEX STATE IS LOCKED
threaded_set_object_uid_logic(object)
# for the object and increment the GlobalData UID var
object_uid_mutex.unlock() # and then unlock the lock.
# CRITICAL INFO MUTEX STATE IS UNLOCKED
func threaded_set_object_uid_logic(object: GameObject):
increment_object_uid(1)
object.set_uid(object_uid)
func get_new_uid_int(incrementor: int) -> int:
return object_uid + incrementor
func increment_object_uid(incrementor: int) -> void:
object_uid += incrementor
func uid_request_received(object: GameObject) -> void:
var thread := Thread.new()
await thread.start(threaded_set_object_uid.bind(object))