Label invisible without top_level = true, z_index innefective

Godot Version

4.2.2.stable

Question

Hello, I am setting up a basic 2D game and wanted to implement damage numbers. (Think numbers that pop up when you cause damage to an enemy.)

I am doing this by creating a Label object and spawning it as a child of my DamageNumberComponent which is in turn a child of my Bat enemy.

Minimal code example:

# Func connected to signal
func display_damage_number(damage, _body) -> void:
	var number = Label.new()	
	number.global_position = self.get_parent().position
	number.text = str(damage)

	# Setting high z_index
	number.z_index = 5

	call_deferred("add_child", number)

This works and creates a Label, but this label is invisible.

After spending a lot of time messing with the editor I realized that it does display if I set number.top_level = true.

Is this intended behaviour? As far as I understand this is used to override the z_index and always display on top, but shouldn’t me setting up the z-index as 5 already be solving this issue?
(I tried setting z_index to 2048 and the results were the same)

Thanks in advance!

edit:

Bat (CharacterBody2D)
|
| - DamageNumberComponent (Node2D)

Is DamageNumberComponent of type Node? the basic Node type doesn’t have position or layers, try childing it to the bat instead

1 Like

Sorry should have been more specific.

DamageNumberCompononent is a Node2D with the above script and is a child of Bat that is a CharacterBody2D, which is a child of a YSort node.

Removing the Bat from the Ysort made no difference though