im trying to figure out what in this code is causing it to return null for player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unskilledmoss
extends Camera2D
onready var player = get_node("/root/MainScene/Player")
 
func _process (delta):
 
    position.x = player.position.x
:bust_in_silhouette: Reply From: kidscancode

Note: I fixed your code formatting.

Your problem is due to the order in which nodes become ready. “MainScene” is not ready when the Camera2D node becomes ready, so that path is not valid yet.

See: Using SceneTree — Godot Engine (3.2) documentation in English

ok my camera should be ready last the way its lined up unless im misunderstanding how nodes load.

unskilledmoss | 2020-05-23 22:54

_ready() happens in reverse tree order. The camera is the first node to be ready. Then “Tile”, then “Player”, and finally “Mainsece”. A parent is not ready until all of its children are.

Since Camera is ready first, its onready code runs. But since that code tries to access “Mainsece”, it fails.

kidscancode | 2020-05-23 23:29