0 votes

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 onfloor = true
var can
take

func process(delta):
if on
floor:
$AnimatedSprite.play("idle")
else:
$AnimatedSprite.play("usando")

Global.take_weapon = can_take

func onArea2Dbodyentered(body):
can_take = true

func onArea2Dbodyexited(body):
can_take = false

Godot version v.3.3.2
in Engine by (46 points)

1 Answer

+1 vote
Best answer

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

by (5,643 points)
selected by

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.

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

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'

Queue free is called more than once after takeweapon becomes true, because there is nothing to set it back to false. Set takeweapon 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 ;)

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

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