Waiting for variable assignment in ready function

Godot Version

4.2.1

Question

How do you wait until a variable gets an expected value?
image
My current solution for my particular case is seen above, it waits one frame, but I find it a stupid solution as it offers 0 flexibility (and sometimes doesn’t work)
Essentially I have a ‘Player’ class that has an exported variable ‘entity_data’ which holds a resource that contains all entity data. I’m wanting to access a variable in player’s entity_data as soon as possible after loading the scene, so I want the script to wait till entity_data != null, but the below code doesn’t work for whatever reason
image
Is there some way I can do this within the ready function?

Instead of a timer with 0.0 seconds, use await get_tree(). process_frame. You can only await coroutines and signals, and a bool is neither

If you you filling entity_data during player initialization, @onready should be enough when referencing player. If this not work, you can try setters and getters.
But maybe i incorrectly interpreting your question.

if something happens after resource was set, try to

var entity_data:
	set(value):
		entity_data=value
		#code that reacts on property set goes here
		custom_signal_data_ready.emit()

this will make code react regardless of init/enter_tree/ready stage
personally I use construction:

signal data_ready
var rendered:={}
var is_ready:=false
func do_stuff()->void:
	#etc magic code that fills rendered
	is_ready=true
	data_ready.emit()

When someone tries to get resources from rendered it checks if is_ready==true
if not then

await data_ready
texture=rendered['super_texture']

this will make use of desynced access to resources

1 Like

There isn’t really a way to do this, except by using your own signal like solver10 mentioned. However there is another slightly inconvenient method:

func _ready():
	while entity_data == null:
		await get_tree().process_frame
	variable = entity_data
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.