Problem with Queue.free()

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

Hi, i’m using godot for maybe 3 months. I have a Scene called Pistol, and it haves an Are2D node responsible for detects if the player enters his area, and after that, store the value in a Global variable called take_weapon.
In another Scene, called Mundo (World in portuguese) i’m going to use that value to delete the instance that i’ve created for the Pistol Scene, thing is, for some reason i can’t delete the instance.

Sorry if it was hard to understand, english is not my main language. Here’s the code of my game to help you guys. Thanks in advance.

Mundo
- Player
- AnimatedSprite
- CollisionShape2D
- TileMap

extends Node2D

var pistolPath = preload(“res://Pistol.tscn”)
var count = 0

func _ready():
pass

func _process(delta):
var p = pistolPath.instance()
while count == 0:
get_parent().add_child(p)
count += 1

if Global.take_weapon == false:
	print("False")
elif Global.take_weapon == true:
	print("True")
elif Global.take_weapon == null:
	print("Null")

if Global.take_weenter code hereapon == true:
	p.queue_free()
else:
	pass

Pistol
- AnimatedSprite
- Area2D
- CollisionShape2D

extends Node2D

var on_floor = true
var can_take

func _process(delta):
if on_floor:
$AnimatedSprite.play(“idle”)
else:
$AnimatedSprite.play(“usando”)

Global.take_weapon = can_take

func _on_Area2D_body_entered(body):
can_take = true

func _on_Area2D_body_exited(body):
can_take = false

:bust_in_silhouette: Reply From: Inces

I think I know what is going on. You are instancing millions of guns every frame, but only first gun is childed. So every time area is entered one of those millions orphaned guns is killed :P. You can check if this is true in remote panel in editor - You should notice hordes of nodes amassing above scene tree, not childed to anything.
Sollution is simple - instance gun under while condition :slight_smile:

Hey, thanks for answering me! I tried to do this:

while count == 0:
	var p = pistolPath.instance()
	get_parent().add_child(p)
	count += 1

And that error appear in this line:

p.queue_free()

The identifier “p” isn’t declared in the current scope.

Matt101 | 2021-10-28 22:37

Yes, while is a loop and when You use “var” keyword in it, this variable becomes local for the loop and can’t be used anywhere else.

You will have to introduce variable “p” one level higher, but don’t set its value yet. Like this :

func process()
    var p
    while count == 0 :
            p = pistolpath.instance()
            get_parent.add_child(p)
            count += 1

Inces | 2021-10-29 07:40

Just a little problem. After having made these changes that error appeared in that line:

p.queue_free()

Nonexistent function 'queue_free" in base ‘Nil’

Matt101 | 2021-10-29 12:57

Queue free is called more than once after take_weapon becomes true, because there is nothing to set it back to false. Set take_weapon to false right after you queue_free p.

Generally You picked uncomfortbale code structure, everything is happening in process. I would like to point You in direction od SETGET. This is a special type of function that will trigger only once when chosen variable is changed, it will make tasks like yours much easier and requirring less code :wink:

Inces | 2021-10-29 13:06

Thank you so much, everything is working fine now. As i said before, i’m new with godot and coding in general.
Btw: You helped me a lot with that tip, and thanks for being so patient with me :slight_smile:

Matt101 | 2021-10-29 18:51