0 votes
extends Camera2D


onready var player = get_node("/root/MainScene/Player")

func _process (delta):

    position.x = player.position.x
asked May 23 in Engine by unskilledmoss (14 points)
edited May 23 by kidscancode

1 Answer

0 votes

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: https://docs.godotengine.org/en/3.2/getting_started/step_by_step/scene_tree.html#tree-order

answered May 23 by kidscancode (17,132 points)

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

enter image description here

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

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.