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()
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
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:
create a laser scene
add all the laser functionality there (like moving forward and animations if it has them)
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()
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.