How to Y-Sort these instanced nodes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hontohowsu

I have an Oak tree node that Y-sorts correctly because it’s in the world scene. But once the player chops the tree down, it instances into a stump scene here:

    @onready var stump = preload("res://noob_stump_1.tscn")

    func create_stump():
	var Stump = stump.instantiate()
	Stump.global_position = global_position
	get_tree().get_root().add_child(Stump)

Then the stump has the same code which instances the Oak tree again. (respawn)
Both the stump and the respawned oak tree do not Y-sort they are always in front

:bust_in_silhouette: Reply From: ShatteredReality

don’t add it to the root, add it to the same scene as the original tree

how can I do that? if I do just add_child(Stump) it does not instance

hontohowsu | 2023-06-06 21:42

get_parent().add_child(Stump)

ShatteredReality | 2023-06-06 21:46

ohhh duh. thanks a lot!

hontohowsu | 2023-06-06 21:47

:bust_in_silhouette: Reply From: crossbito

Put the world scene in a group and add the stump to that element.

@onready var stump = preload("res://noob_stump_1.tscn")

func create_stump():
var stumpInstance = stump.instantiate()
var world = get_tree().get_nodes_in_group("worldScene")[0]
world.add_child(stumpInstance)
stumpInstance.global_position = global_position

And please, don’t use capital letters for variables. When something is capitalized, it’s because it represents a class. It would be better to call the instance “stumpInstance” or something similar.