0 votes

Why are onready variables referenced in another node null in _ready()?

For example:

in my game node:

onready var player = $Player

in my another node:

onready var game = $"/root/Game"

func _ready():
    print(game.player) # null
in Engine by (12 points)

If your other node is in the Game scene, you can actually access the Game node via the owner property:

func _ready():
    owner.player.shoot()

1 Answer

0 votes

This is because _ready in your other node is called before your Game node.
This is what I find myself doing in my Game node:

var player

func _enter_tree():
    player = $Player
by (4,155 points)
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.