How do I sort all sprites on screen according to their Y-Value, including sprites of child-nodes (YSort-node?) [Godot2D]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mcsora
:warning: Old Version Published before Godot 3 was released.

Hey guys,

this question bugs me for a while now. I am working on a project right now and decided to deal with this when i have to, so I can focus on core-stuff for now. So I wanted to take the time to … fully understand this matter and find the best solution until then.

Imagine you have a simple tilemap. Nothing too special, its a grass plain and you control a character. And on this grass plain your character can create small forests (in this example consisting of maybe three trees).
The forest-pattern of the trees is always the same so you decided to put the three tree-sprites into one seperate forest.tscn - scene.
What happens now, when spawning the forest is the following:

const forest_scene = preload("PATH")
[...]
var new_node = forest_scene.instance()
main_game.add_child(new_node)

Now let’s assume the main_game is the node of the main scene and is a YSort-Node and the character is also a direct child of the main_game node.
The result would be that if your character moves “behind” the forest (YValue is further at the top) the character disappears behind all three tree sprites. Even if it wouldn’t make sense.

What I want to ask you now is if you know any solution to this problem, so that all 4 sprites (character and the three tree sprites) can be sorted seperately, but the forest as a scene could still be used.

Maybe i am thinking of this wrong… maybe the ysort-node is not supposed to be used for this kind of problem… should I write a script that sorts all sprites on _process using the Z-Value?.. Maybe one of you guys knows a trick i didn’t think of by now!

Thanks in advance! :slight_smile:

Your forest design is then probably wrong, though I understand why you put it in a scene. You either need to separate the trees so they can be sorted independently by the YSort node, or you have to get rid of YSort and roll your own solution.

If trees are already separate objects, that could mean that the YSort node doesn’t sorts nested nodes individually, and that could be the subject of an issue on Github. I didn’t use that node much yet, so I can’t tell which are its real limitations.

Zylann | 2017-01-20 11:55