Godot Version
Godot 4.4.1
Question
Here’s my code:
func _on_key_1_pressed() -> void:
overlaps=$lane1/lanechecker1.get_overlapping_areas()
if overlaps.size()>0:
for n in overlaps[0].get_children():
overlaps[0].remove_child(n)
n.free()
overlaps[0].free()
I tried to use this to remove the first node in the array provided by get_overlapping_areas() but i doesn’t seems to work
Hi,
If you’re looking to remove the first node only, then you don’t need any loop.
Something like this:
overlaps=$lane1/lanechecker1.get_overlapping_areas()
if overlaps.size() > 0:
overlaps[0].queue_free()
Also, it may not work if you’re removing the area node, while you want to remove the parent of that area. In that case, use get_parent()
on the node to free:
overlaps=$lane1/lanechecker1.get_overlapping_areas()
if overlaps.size() > 0:
overlaps[0].get_parent().queue_free()
Let me know if that helps.
The node i wanted to delete can be deleted properly thanks to your suggestion now, but the children nodes aren’t deleted? If it helps the node i wanted to delete was instanced using code
Being instantiated from the code or not does not matter when it comes to node being freed.
When a node is freed, all of its children will also be removed from the tree. That means that, by detecting an area and freeing its parent, the area will be removed on the fly.