Queue_free doesn't work!

Hello! I’m making now bullet hell game!
And I faced a problem situation
The problem is that queue_free does not work!
This is my code and I can’t understand why the problem happen…
Could you help me?

[Game gd script](main script)

@onready var laser = $laser

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
laser.queue_free()

func _on_laser_area_entered(area: Area2D) -> void:
if area is enemy:
laser.queue_free()

[enemy gd script]

class_name enemy
extends Area2D

What exactly are you trying to accomplish? Since there is only one laser instance, there is probably no need to get rid of it.

2 Likes

queue_free() will not instantly free the instance, but mark it as a resource to be freed at the end of the current frame

Unlike with Object.free, the node is not deleted instantly, and it can still be accessed before deletion. It is also safe to call queue_free multiple times. Use Object.is_queued_for_deletion to check if the node will be deleted at the end of the frame.

In your situation you might want to either use free() to directly free the laser

At least I think this is your issue, the code you posted is missing tabulations, so perhaps you have other errors

1 Like

Oops! I forgot to post this! I want the laser to disappear when it hits the enemy!


Thank you so much!!
How can I use queue_free wisely?

I’m not sure if your code correct. I mean, you have ONE variable, called laser in your main game script.

@onready var laser = $laser

you can only be referencing ONE laser at a time (so you can’t be working with a lot of bullets at the same time), and that laser is preadded to the scene.

What you have to do is:

  1. create a laser scene
  2. add all the laser functionality there (like moving forward and animations if it has them)
  3. in the laser scene, add the _on_laser_area_entered. And on that area enter just do this
func _on_laser_area_entered(area: Area2D) -> void:
    if area is enemy:
        queue_free()
  1. Now, on your game script you don’t have to do anything, just reference your laser scene and then instantiate it, nothing more. The laser class will handle the rest.
1 Like

I’ve already tried this method, but it didn’t work…

try remove from tree before queue_free()

1 Like

How can I do this??

Uh… all of a sudden it’s working properly. Thank you very, very much! Thanks to you, everything has been solved!

1 Like

first you needed
obraz
and from parent node you remove child

this way you have guaranteed your node is not show any more on screen
after this you just queue_free() to save memory.

Aha!!!
There’s a way like this…!!!
Thank you so, so much!!!

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