0 votes

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?

in Engine by (24 points)

isn't _on_PlayerShip_ship_moved called before _ready?
try to print something to see which is called first.

add a

func _ready():

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.

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

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.