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
asked Jun 5, 2019 in Engine by Andre van Tonder (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
answered Jun 5, 2019 by Dlean Jeans (3,888 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.