#Issue removing object from scene with queue_free()

3 messages · Page 1 of 1 (latest)

uneven verge
#

Hey party people, I'm having some trouble with 'despawning' an object from one of my scenes with queue_free(). Keep getting an error "Invalid call. Nonexistent function 'queue_free' in base 'Nil"."

The ball var should be accessible by the _on_DeadZone_body_entered() function, so I'm not sure what I need to adjust to make queue_free() work...any help would be appreciated!!!

Code is attached as screenshot as well as typed out below!

====CODE START====
extends Node

var ball_prefab = preload("res://_scenes/Ball/Ball.tscn")
var ball

func _ready():

pass

func _physics_process(delta):
if Input.is_action_just_pressed("launch"):
if $Player.launchable():
ball = $Player.launch_ball(ball_prefab)
add_child(ball)
else:
print("Cannot launch ball")

func _on_DeadZone_body_entered(body):
ball.queue_free()
$Player.reset_ball()
====CODE END====

red quiver
#
var ball    <--- not set to anything

func _physics_process(delta):
    if Input.is_action_just_pressed("launch"):
        if $Player.launchable():
            ball = $Player.launch_ball(ball_prefab)   <--- only gets set when launch is pressed
            add_child(ball)
        else:
            print("Cannot launch ball")

func _on_DeadZone_body_entered(body):
    ball.queue_free()     <--- is run whether ball is assigned or not
    $Player.reset_ball()

are you certain you aren't running into a situation where this node doesn't have something being detected before ball is defined?

uneven verge
#

Hmmm, ball should be defined within the _physics_process before the body_entered signal is triggered...lemme double check