0 votes

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.

in Engine by (49 points)

1 Answer

0 votes

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

by (6,329 points)

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.

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.

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

 fireball.global_position = $Position2D.global_position

Did You do exactly that ? ;)

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 :)

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.

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 :)

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).

I bet there is something wrong there ;)
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 :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.