I'm trying to add additional text to a sprite and am using a Label for the purpose. This works in regards to the text is actually following the sprite around and gets displayed in the game world. But since a Label is inherited from Control and not Node2D, it does not have a Z_Index. Which has the consequence that the text always draws on top, even when the accompanying sprite is hidden below other sprites.

I looked through the various Node2D node types but couldn't find a similar label text node. It would have been great if there were a text 2D node type, maybe with the name Text2D, which worked very similar to Label but did have a Z_Index and with the purpose of actually existing in the game world, and not the interface.

Do you think that this would be possible to implement?

Looking at the source code for Node2D, it looks like you just need to call canvas_item_set_z_index through the VisualServer and pass in the canvas item you want to change the index of.

I haven't tried it myself, but I think you can do something like this:

Extends Label
export (int) var z_index = 0
func _ready():
	VisualServer.canvas_item_set_z_as_relative_to_parent(self.get_rid(), true)

Hopefully this helps!

Thanks for the help!

I created a new scene (from Label) and attached a new script with the suggested code. But gets an error: 'Method get_rid is not declared in the current class'

I tried also: VisualServer.canvas_item_set_z_as_relative_to_parent(RID(self), true)

but the RID constructor doesn't accept 'self'.

I'm quite new to GDScript and Godot so havn't yet dived deep into stuff like this. Am probably missing something simple.

No worries, it is not your fault. I thought the get_rid function was exposed to nodes extending CanvasItem, but it appears I was wrong. Sorry, I should have double checked.

I tested the following code and it seems to work:

extends Label

export (int) var Z_Index = 0;

func _ready():
	VisualServer.canvas_item_set_z_index(get_canvas_item(), Z_Index);

Strangely it doesn't work in the editor when the script is set to tool mode, but it works when the game is running.

Thanks, the code now seems to work. It's possible to set a z_index on the label and also make it relative. I combined the two calls to visualserver:

extends Label

export (int) var z_index = 0

func _ready(): VisualServer.canvas_item_set_z_as_relative_to_parent(get_canvas_item(), true) VisualServer.canvas_item_set_z_index(get_canvas_item(), z_index)

However, I found that I was wrong in my initial assumption that the original Label doesn't follow it's parent's "z-behaviour". It does actually. It was me wrongly putting the z_index on another subnode. When I changed it the label (the original type) now hides below other sprites when its parent node hides. Sorry...

But the new code may come in handy later, if a need arises to being able to put a z_index other than 0, on the Label.

Thanks a lot, learned something new :)

Great!

Figuring out how to add a Z-index to a CanvasItem node was interesting and I learned new things as well. No worries that you ended up not needing the code, I am just glad a solution was found. :smile: