The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

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

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 Frequently asked questions and 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 [email protected] with your username.