HBoxContainer strange behaviour

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

Hey there

So I have an hboxcontainer, with a custom separation I’d like to set through script, and it works fine. The weird behaviour comes in when I try to use an onready var as reference for the hboxcontainer.

The following code works:

func _ready():
	$Control/HBoxContainer.add_constant_override("separation", get_viewport().size.x-100)


func _on_HBoxContainer_resized():
	$Control/HBoxContainer.add_constant_override("separation", get_viewport().size.x-100)

while this does not:

onready var h_container = $Control/HBoxContainer


func _ready():
	h_container.add_constant_override("separation", get_viewport().size.x-100)


func _on_HBoxContainer_resized():
	h_container.add_constant_override("separation", get_viewport().size.x-100)

with the following error message being flagged on the _on_HBoxContainer_resized() signal:

Invalid call. Nonexistent function 'add_constant_override' in base Nil.

Anyone able to shed light on why this happens?