Seperate child from parent without affecting position set

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

Hello, bit of a noob here.

I have created some enemies who fire fireballs. I set the position of the fireballs like this

		var f = Afireball.instance()
		f.position = $Position2D.position

which works, the fireballs fire from the enemy. The fireballs are however linked to the enemy so when the enemy moves the fireballs move with it. I did some investigation and found a set as toplevel which seemed like the answer. I tried this in the child creation

		f.set_as_toplevel(true)

and also just set this in the fireballs ready function

		set_as_toplevel(true)

but in both cases doing this did seperate the fireball from the enemy but also meant they no longer start at the position I set but in the top left corner of the screen. Does anyone know what I am doing wrong? I dont understand why they no longer start at the position I set? For reference the position is a position 2d node in the parents scene.

:bust_in_silhouette: Reply From: Inces

just use global_position instead of position. position counts in local space, which usually is 0,0. When set as top level it no longer referes to another nodes local position, but is set to global 0,0 - which is top left of the screen

Also, usually better practice is to child projectiles to higher scope node like game menager or something, not to the projectile user

Thank you for the suggestion but that didnt work. The fireballs now end up generating at a random distance to the side of the enemy.

YuleTide | 2022-05-09 23:00

Hello with some experimentation I was able to do it, I set a var in the fireball to the global position of the enemies wand, then I created the fireball, then set the fireball as toplevel in its own scripts ready function and then move it to the position held in the var with the enemies wand.

This works, its a bit messy but did the job. Thanks for your suggestion it set me on the path to the solution.

YuleTide | 2022-05-10 11:36

I can’t see the reason it wouldn’t work in easy way :slight_smile:
first set as top level, next :

 fireball.global_position = $Position2D.global_position

Did You do exactly that ? :wink:

Remeber, using toplevel your fireball is still childed to the enemy. That means if enemy dies, fireball will disappear too. As I mentioned, it is always better to child projectiles to something neutral :slight_smile:

Inces | 2022-05-10 13:04

Hello

Thanks, yes that is exactly what I did but it didnt work for me. I originally thought it was generating the fireballs at random points when I used the code

 f.global_position = $Position2D.global_position

but when I zoomed the camera out and experimented a bit I realized that actually what was happening was it was dependant on where the enemy spawned. So for example if the enemy spawned 100 pixels above the zero line on the y axis and 400 pixels from the zero line on the x axis the fireball was appearing 100 pixels above and 400 to the side of the enemy. It was as if it was zeroing the fireball at the enemies wand and then offsetting by how far the enemy spawned from 0,0.

I dont know why but my code seemed to fix it so I am happy with it for now. In future projects I will try doing it your way with the higher scope node to see if that is easier.

YuleTide | 2022-05-10 14:49

I think I know what happened. You instance fireball on enemy ready(), but shoot them much later. You should create and shoot fireball in the same moment for this to work :slight_smile:

Inces | 2022-05-10 16:33

No sorry, the code was in the

func _process(delta)

and creates a fireball each time a variable has been set to true (based on a signal from other nodes).

YuleTide | 2022-05-10 18:53

I bet there is something wrong there :wink:
Your problem i solved, but if You show the whole code of fireball creation and shooting along with signal emission, I can prove there is something wrong with it, if You want :slight_smile:

Inces | 2022-05-10 20:40