Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Blaise Bernier |
I’m building a simple testing GUI for my current project and I’m encountering a strange problem.
Here is the working code:
extends MarginContainer
func _on_PlayerShip_ship_moved(pos):
var x_number = int(round(pos.x))
var y_number = int(round(pos.y))
$HBoxContainer/CoordDisplay/VBoxContainer/X/Number.text = str(x_number)
$HBoxContainer/CoordDisplay/VBoxContainer/Y/Number.text = str(y_number)
if I try to assign those nodes to variables for later use, I get a “nil” value:
extends MarginContainer
onready var x_number_label = $HBoxContainer/CoordDisplay/VBoxContainer/X/Number
onready var y_number_label = $HBoxContainer/CoordDisplay/VBoxContainer/Y/Number
func _on_PlayerShip_ship_moved(pos):
var x_number = int(round(pos.x))
var y_number = int(round(pos.y))
x_number_label.text = str(x_number)
y_number_label.text = str(y_number)
What am I doing wrong?
isn’t _on_PlayerShip_ship_moved
called before _ready
?
try to print something to see which is called first.
volzhs | 2018-04-26 05:38
add a
func _ready():
pass
to the script with the onready and put a breakpoint on pass
(or write breakpoint instead of pass), then check the remote scene tree when breaks execution to see if the nodes you want exist in the tree at that time.
eons | 2018-04-27 00:13
I am having this exact same issue. Not sure if you ever got it resolved?
It seems like the scene tree is being created after the onready variables are being set. My game was functioning properly, then stopped and this seems to be the issue (after much testing).
frasando | 2021-05-07 15:13