what's the parent of the pause menu? if I'm reading that code right, you're deleting whatever it's attached to.
get_parent().queue_free() not working as intended
- Edited
Hmm, some bullets never disappear, which is strange because most of them do, and you can see how you just exited the main menu, the elements disappear little by little, it gives the feeling that the level is still loaded, or at least the elements that were already present.
I´m using a VisibilityNotifier2D on the projectiles for when they should delete.
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
This Picture was made with 2 resets for the level, so its stacking bullets...
But putting that aside, is that behavior normal? Maybe I should make a way for the user or the program to wait until every element is deleted perhaps? Or is the queue free not working correctly?
- Edited
As seen in the last image, the "MainLevel" and the rest of the elements should be eliminated, at least thats the plan.
- Edited
If for some reason get_parent.queue_free() isn't working, I could try to send a signal that receives the level itself and maybe remove them that way? Or would it not work?
The enemies are generated with this: (the level code is incomplete)
func _on_EnemyShipTimer_timeout():
if total_enemies < 5:
total_enemies += 1
var enemy = enemy_ship_scene.instance()
var enemy_spawn_location = $MobPath/MobSpawnLocation
enemy_spawn_location.offset = randi()
var direction = enemy_spawn_location.rotation + PI / 2
enemy.position = enemy_spawn_location.position
direction += rand_range(-PI / 4, PI / 4)
enemy.rotation = direction
add_child(enemy)
else:
return
And the bullets are generated from the enemy_ship script with this:
func _on_ShootTimer_timeout():
var random_number = randi() % 2 + 1
var bullet
for s in rotater.get_children():
if random_number == 2:
bullet = bullet_t1_scene.instance()
else:
bullet = bullet_t2_scene.instance()
get_tree().root.add_child(bullet)
bullet.position = s.global_position
bullet.rotation = s.global_rotation
- Edited
const bullet_t1_scene = preload("res://game_elements/enemies/EnemyBulletT1.tscn")
Now that you mention it, it looks like that, the player for example does appear within the level node
I have a test level with a static enemy and the same thing also happens, so the problem is in how the bullets are generated, since the enemies also appear in the level node.
Writing the message I just realized the error, just in the last message you see this: get_tree().root.add_child(bullet)
It looks like that is right?
Note that queue_free() is a request, and it will be removed at some later frame if it is safe to do so. I'm not sure which cases you have to look out for, but there could be times when it is not removed immediately or at all.
In general it is good to imagine the scene graph like a live action play or movie. Where you have a set, actors, props, etc. and design the hierarchy according to how it would work in real life.
For example, if you shot a bullet from a gun, the bullet is not a child of the gun. It is a separate object on the same layer as the gun (most likely the main level).
In the end it was that, by changing the way to generate bullets in the enemy ship, now they are generated in the level.
I changed the original line with this:
get_parent().add_child(bullet)
At the end the get_parent_queue_free() was working
Thanks everyone through!
Currently it is already solved and it eliminates everything directly, at least in this case, but I will keep it in mind for the future, thanks!