Spawn an effect without it getting covered by others (but without using Z order and top_level)

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

In my project, during the actual game I keep all the game characters in a particular Node2D, appropriately named “character container”. Every time a character gets attacked, its hitbox spawns an “ouch!” special effect (a simple AnimatedSprite2D, like a speech balloon) using
owner.add_sibling(special_effect_ouch)

This works great!

The problem is that each new special effect ends up right below the character that spawned it in the Project Tree hierarchy. So what often happens is that other characters that are already in the Project Tree cover the freshly spawned special effect.

Is there a way for me to make sure the special effects are spawned on the bottom of the whole container Node2D?

For now I’m trying to avoid using Z index because I want to use that for something else later and I’m also trying to avoid using something like getting owner of the owner. I tried assigning the “character container” node a unique name and spawning the special effect using something like
%character_container.add_sibling(special_effect_ouch) but this doesn’t work.

make a container just for effects
if the effects doesnt need to follow the player when it spawned, this method is the easiest way to ensure the effect always on top

after making a container, let’s say it’s a control node, put it at the lowest in the scene hierarchy or its own canvas layer
then you add child the effect into this container, and set its global_position

I thought of that too, but how would I access this new effects-only container node? The problem is that the hitbox detector (which is spawning the special effect) is a standalone scene nested in the player so it doesn’t have access to the effects-only container node, which would be nested in the Level.

The only way I can think of making it work is using signals via signal bus, but there is probably a simpler way to go about it.

ok this is a stupid solution but what I did in the end is just find the first parent named “characters” and made the hit detector spawn the special effect there. It works perfectly, apparently looking for nodes by name can be performance intense but in my case it’s not an issue so:

var EC # effect container
EC = find_parent( "characters" )
EC.add_child(notification)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.