onready not intialising with instance()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bizt

I’ve a few onready var statements in my scripts…

Here is my script:

extends Node

onready var state_machine = $StateMachine

...

Here is my test code:

extends "res://addons/gut/test.gd"

var match_scene = preload("res://Match/Match.tscn")

var match_instance

func before_each():
	match_instance = match_scene.instance()

func test_onready_vars():
	assert_not_null(match_instance.state_machine, "state_machine ready") # fails

However, when I want to test these they aren’t initialised. I understand this is due to the fact they haven’t been assigned to any tree. Isn’t there a way that I can init onready properties, as it does when I run the game, when testing?

I’ve tried the following which seems to work OK, but makes my code a little untidy if I have to do it for every onready:

extends Node

var state_machine: StateMachine

func _ready():
	state_machine = $StateMachine

...

And how do You test them ? With tool mode in editor ? It is very specific testing if You somehow don’t call ready().

assigning value to variable in ready() function is exactly the same as introducing this variable using onready keyword

Inces | 2022-02-21 21:30