Some way to ignore YSort in a Control node?

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

The player can open this menu whenever in game proper. The problem is, as the menu is created as a child within the Player node, and the player is in the YSort of the scene, the menu is also obeying the sorting.


I’m currently thinking of putting the code to create the instance of the menu on the map scene instead of the player script, so it can stay out of the YSort. But if I have too many maps, this will become a hassle.

How the scene's currently arranged

Is there a way to make a child node ignore the YSort? Or is there a way to make a Control node to ignore the parent’s relative Z, or set its own? I have tried set("z_index", 3) and set("z_as_relative", false) on the created instance, before adding it as a child, but it didn’t make a difference.

The canvas_items nodes have the property show on top and the function set as top level. I do not know if they can work in this case but you can try.
CanvasItem — Godot Engine (stable) documentation in English

estebanmolca | 2021-08-24 13:33

:bust_in_silhouette: Reply From: MisterMano

Thanks to the comment from @estebanmolca, I’ve managed to resolve this! Thanks, man!

So, first I tried instance.set("show_on_top", true) before add_child(instance), but nothing happened. Then, I tried instance.set_as_top_level(true) next, which made the menu not show up at all, not even behind the tileset. I found that very weird.

After further messing around, I noticed that the menu was actually showing when “show_on_top” was true, but it was using the scene’s origin instead of the player position! Since the player’s starting position on the scene was far from it, I couldn’t see it right away.

So, I had to do this:

instance = pausemenu.instance()
instance.set_as_top_level(true)
instance.set_position(position) #the position parameter here is the Player's

At first, the only problem was that the menu was being drawn down and right from the player’s origin. I had to mess around with the rectangle’s (the main node of the menu) anchor until it aligned up properly.

EDIT: After having trouble with the menu showing under anything that had a Z greater than the player’s, including a tileset, I found the definitive solution: create a Node2D with a non-relative, high Z Index and add_child from within it, like this:

#menulayer is a Node2D. It's sole purpose is to have a big z index
pauseinstance = pausemenu.instance()
$menulayer.add_child(pauseinstance)
:bust_in_silhouette: Reply From: Inces

You can also access z_index manually with :

var rid = get_canvas_item()
VisualServer.canvas_item_set_z_index(rid,anyvalue)