Hi! I think my Area2D ain’t working, i wanted to program it so the instances of a scene were deleted when they collided with the Area2D but nothing happens, here’s the code.
new_gift is a variable in a Global singleton and i set it to be equivalent to gifts.instantiate(), i called @export var gifts: PackedScene on both the game scebe and the global script even though it is declared as an instance of the gifts scene only in the game scene because i cannot call it globally in the Global script. Here’s the whole game scene’s code:
extends Node
@export var gifts: PackedScene
@onready var sleigh: Sprite2D = $Sleigh
var gift_list = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
# Correct function name for the button signal
func _on_button_pressed() -> void:
Global.new_gift = gifts.instantiate()
var sleigh_where = sleigh.position
Global.new_gift.position = sleigh_where
add_child(Global.new_gift)
gift_list.append(Global.new_gift)
func _on_area_2d_area_entered(area: Area2D) -> void:
remove_child(Global.new_gift)
Did you check that you connected the area_entered signal to _on_area_2d_area_entered method?
Is this method firing properly when you want it? E.g. put a print("test") statement within the method to see if the method is firing properly.
If it is firing, then you can also double check if Global.new_gift variable holds the proper reference that you expect.
Try to debug and let us know the outcome. It would help if you showed a screenshot of your scene structure.
Yeah i checked, everything is connected.
I tried putting a test, nothin’s printed.
I checked if the print works and it does, this is the new code
extends Node
@export var gifts: PackedScene
@onready var sleigh: Sprite2D = $Sleigh
@onready var floor: Area2D = $Floor
var gift_list = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
# Correct function name for the button signal
func _on_button_pressed() -> void:
Global.new_gift = gifts.instantiate()
var sleigh_where = sleigh.position
Global.new_gift.position = sleigh_where
add_child(Global.new_gift)
gift_list.append(Global.new_gift)
print("giftin")
func _on_floor_area_entered(area: Area2D) -> void:
remove_child(Global.new_gift)
print("test")
It’s because you are removing the last created gift, not the one that hit the floor.
remove_child(Global.new_gift)
You should instead remove the gift that hit the floor. It’s right there, provided to you by the signal. I would also use queue_free() instead of remove_child(), unless you have a specific reason to use the latter. Your script should look something like that now:
btw Yeah I recently realized it was truly unnecessary to store new_gift on Global, I did it when I forgot that I could call globally a variable (I needed the new_gift variable global to call it in different scopes) and then define the variable inside a scope. (I hope you understand what I mean)
I know it’s the dumbest thing I could forget, because I anyways defined a variable that was declared in Global in a scope.
Hope that all works for you now.
If you have no further issues - mark my previous response as a solution, so that others know the issue is solved in case they ever encounter a similar problem.