Player The difference in usage between the two

Hello,

I’m not a native English speaker, so please forgive me if this is a little offensive or confusing.

I would like to ask what is the difference between the usage of these two players? They also complete the player’s behavior of setting coordinates in the ready phase. All I can think of is that when using a Group, you can set up an adventure team centrally. Is there anything I’m missing?Thank you.

@onready var player = $Player

func _ready():
    player.global_position = start.get_spawn_pos()
var player = null

func _ready():
	player = get_tree().get_first_node_in_group("player")
	if player != null:
		player.global_position = start.get_spawn_pos()

hi fireice502 and welcome to the community :wave:

the first thing that springs to mind, is that using the @onready ensures that the player node is loaded and in the scene tree before getting a reference to it or doing anything to it.

thats more down to how the scene tree notes arent always loaded in the same order sometimes. just to guarantee that a node is loaded up and available before we try and find it.

2 Likes

Thanks for your reply. I think it is a good practice to check whether an object reference has been loaded whenever you use it. :smiley:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms