Projectiles disappear when made child of main screen

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

I have an enemy that spews out area2ds as projectiles (toxic clouds) if they remain a child of the enemy. If I set them as a child of the main screen, they disappear (and performance takes a big hit for some reason).

	var c = cloud.instance()
c.transform = $Fans.transform

var target = get_parent()

I’ve also tried the following to get the parent node with the same results.

var target = get_tree().get_root().get_node("MainScreen")

If I print out “target” it shows me the MainScreen node in both cases.
If I just leave the area2ds as a child of the enemy, they don’t collide with the player.
Any of that make sense?

:bust_in_silhouette: Reply From: Inces

Your main screens surely has transform origin of Vector(0,0), or - if it doesn’t inherit Node2D/Spatial - has no position property at all. That makes all of your projectiles spawn in one point, on the left corner of the screen, propably not visible to You. I bet frame drops because projectiles amassed in one point collide with each other endlessly.
Sollution is to manually set global position of bullets before You add them as a child of main screen ( provided, that bullets handle rest of the movement by themselves )
No collision with player is a different problem. Make sure to properly set collision layers and masks.

I eventually figured out that the position needed to be set. It’s a little wonky because they seem to build up in one spot and then start moving. Because they’re clouds, I mask that by setting the sprite as visible a split second after being assigned. The main screen is a node2d so I’m not sure why it doesn’t inherit. Is there a proper way to set that up?
I also figured out the collision issues, yes they were unrelated to the parenting. I’m still very new to this architecture so I’m learning what all the signals do.

ImpossibleCastle | 2022-01-26 20:53

If it is Node2D, then it does have position property and passes it to its children. But position only indicates origin point of a node in space. So even if your screen is a large rectangle covering viewport it still has tiny origin, which is 0,0 by default. Adding children to screen sets their position to screens origin. Enemies on the other hand are represented by small sprites, and their position is easier to imagine. Adding child to enemy will add it close to its Sprite origin. Still it is much better to add bullets as child of screen, otherwise they will be moving along enemies and be prone to enemies transforms.

Projectiles have slow start ? Perhaps they are instanced too fast, on top of each other ? You need to set their collision so they don’t collide with themselves

Inces | 2022-01-27 16:03

Thanks for the info.

ImpossibleCastle | 2022-01-27 21:57