0 votes

onready var pistol = getnodeornull("Headposition/Glock 2021")
onready var Pistol = preload("res://Glock 2021.tscn").instance()
onready var headpos = getnode("Head_position")

var gun = false
var current_weapon = 1

func weaponselect():
if Input.is
currentweapon = 1
if Input.is
currentweapon = 2
if Input.is
currentweapon = 3
if Input.is
weapon = 4
if Input.isactionjustpressed("Sniperrifle"):
currentweapon = 5
if Input.is
weapon = 6

if current_weapon == 1 and pistol == null:
elif current_weapon != 1 and pistol != null :

func process(delta):

if i run this code ecerything works just fine as long as i dont press other number than 1 after that it shows me "queefree" in an "nullinstance" as long as i understand that code is running every frame so queue free as well i dont know how to make queue_free only once pls help

Godot version 3,4
in Engine by (21 points)

Did you make sure not to mix up Pistol and pistol?

im 100% percent sure that i dont mixed them up

1 Answer

+1 vote

There are multiple ways of doing this. I see You tried to use "getnodeornull" above. Nodes that are queued free don't immadietely become null, they first become "recentlyfreed". In order to detect recently freed node You can call "isinstancevalid". But You should not do it above in variable introduction, but in the real moment You need to check this. So :

elif current_weapon != 1 and pistol != null :
        if is_instance_valid(pistol):

That should do it for now. In the future I would recommend keeping dynamic track of nodes, that are created and freed. For example adding references of them to array when they are instanced, and using this reference later to queue them free. You will have to rethink this once You will think You need more pistols in your game :)

by (7,740 points)

well i found some different way to queue_free() it i added

Inside Pistol script:

func process(delta):
if Global.current
gun != Global.pistol:

Inside my Player script:

func weaponselect():
if Input.is
currentweapon = 1
gun = Global.pistol

i also have some onready var:

onready var Pistol = preload("res://Glock 2021.tscn")
onready var Pistolinstance = Pistol.instance()
onready var head
pos = getnode("Headposition")

it works but like 3 times then it shows error
E 0:00:03.436 addchild: Parameter "pchild" is null.
<C++ Source> scene/main/node.cpp:1160 @ addchild()
Character.gd:39 @ weapon
Character.gd:85 @ _process()
and i dont know why .Could someone be that good and help me thanks for help by the way

This is not a way to go. This does nothing.
Other ways of calling queue_free() just once :

1.You could call queuefree() once by using boolean value ( true/false). If weapon is not gun and if value is true -> queue free gun and set value to false.
2. Setget. This a type od function that executes some code at the moment chosen variable is changed. So if weapon index is changed queue
free former weapon. Read about setget in documentation
3. Signals. Perhaps the most practical and common. Connect gun to "weapon changed " signal so it would queue itself free.

Of course isinstancevalid method I gave You above is most lazy option.
Whatever You will choose, You should take a note - There is no way to call queue_free multiple times and get this error if the node queues ITSELF free.

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 Frequently asked questions and 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 [email protected] with your username.