#Threaded variable setting call not functioning consistently regular run vs. breakpoint

1 messages · Page 1 of 1 (latest)

woven fern
#

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))
#

GameObject

class_name GameObject extends Area3D

# CLASSDOC INFO This is the base class that all physical objects in the game MUST inherit
# if they do not inherit this class they will not have a UID, or other very
# relevant information.

@export var object_uid: int = 0

func _ready() -> void:
    if object_uid == 0:
        obtain_uid()
    print("Object UID:" + str(object_uid))
    
    
func _enter_tree() -> void:
    pass


func _exit_tree() -> void:
    pass    


func set_uid(value: int) -> void:
    object_uid = value
    
    
func obtain_uid() -> void:
    GlobalData.uid_request_received(self)
quiet birch
#

You aren’t waiting enough time for the uid to be set in ready. You need to use await all the way down, or just use signals.

#

Also consider using WorkerThreadPool instead of thread directly.

woven fern
#

I dont think you need to call await do you? My thinking being because the mutex blocks the access for anything else trying to access the data in the critical section while locked.

quiet birch
#

The await would be an alternative to using wait_to_finish depending on how you end up threading. Basically just a stylistic preference on async code.