Godot 4.3 stable
Right now I’m working with 2d areas which are dragged with mouse and upon detecting each other are snapped into position. I need to make it so only 1 at a time can be “attached” to each others corresponding areas, so I’ve used the get_overlapping_areas().size() to put a limit on how many can be snapped together. I tried various ways to implement it, but no matter what it didn’t work. so I printed the array in get_overlapping_areas() and it prints 0 despite me overlapping several areas in the running project. Are the areas somehow not aware of each other despite them working as intended when it comes to snapping into each others position? Here is the code:
extends Sprite2D
var dragging = false
var of = Vector2(0, 0)
var connect = null
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if dragging:
var newPos = get_global_mouse_position() - of
position = newPos
else:
if connect != null:
position = connect
dragging = false
connect = null
func _on_button_button_down():
dragging = true
of = get_global_mouse_position() - global_position
func _on_button_button_up():
dragging = false
func is_in_same_group_as(node: Node) -> bool:
var their_groups := node.get_groups()
for our_group in get_groups():
if our_group in their_groups: return true
return false
func overlap_limit(node: Node) -> bool:
if node.get_overlapping_areas().size() < 1: return true
return false
func _on_area_2d_area_entered(area) -> void:
if dragging and overlap_limit(area) and is_in_same_group_as(area):
connect = area.get_parent().position
connect += area.position
print(area.get_overlapping_areas().size())