Need help with queue_free() in a signal.

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

I want to make to make it so that when I enter an Area2D zone and press “E”, the parent of the Area2D gets removed from the main scene. The script is attached to the Area2D node, and the Area2D is a child to the thing I want removed, which is a pistol in this case.

This is the code I have written for this:

func _on_PickupArea_body_entered(body):
   weapon_text.visible = true
   if Input.is_action_just_pressed("pickup"):
	  get_parent().queue_free()

But when I enter the area and press the “E” key, nothing happens.
What am I missing?

:bust_in_silhouette: Reply From: timothybrentwood

The body_entered signal only fires when a body just enters the area therefore the player would need to press the pickup input just as they enter in order for your code to work. Something like this should work, just connect the body_exited signal:

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("pickup"):
		get_parent().queue_free()
		weapon_text.visible = false
		set_process_input(false)
		
func _on_PickupArea_body_entered(_body):
	weapon_text.visible = true
	set_process_input(true)
	
func _on_PickupArea_body_exited(_body):
	weapon_text.visible = false
	set_process_input(false)

Ok this fixed the main problem, thank you. But now I have another problem. I have many other weapons in the main scene, and the Area2D I have is it’s own seperate scene that I have instanciated as a child to the different weapons I have. So now when I press the “E” key, it deletes ALL the other weapons that I have on the scene. How can I make it so that it ONLY deletes the weapon in the Area2D that I have entered?

LyguN | 2021-06-23 15:11

Nvm, I solved it. Thanks for the help!

LyguN | 2021-06-23 20:02