How can I click and drag an object without signaling to all the other objects of the same class?

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

My goal is to develop a card game where I can have multiple card objects to manipulate. However, with my current implementation, every time I try to drag just one card, it drags every existing card. I assume what is happening is that the on-click signal is broadcasting to every single one, but I have no idea how to implement this idea without using signals or attaching an ID to each card for the signal to check. Can I process the click event without using signals? If not, how would you suggest implementing/checking card names?

Here is my code:

extends Area2D

var can_grab = false
var grabbed_offset = Vector2()

var enlarged = false

func _on_Card_input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton:
        can_grab = event.pressed
        grabbed_offset = position - get_global_mouse_position()

func _process(delta):
    if Input.is_mouse_button_pressed(BUTTON_LEFT) and can_grab:
        # Increases size when dragging
        if not enlarged:
            self.scale = Vector2(self.scale.x * 1.2, self.scale.y * 1.2)
            enlarged = true
        # Changes position to mouse position
        position = get_global_mouse_position() + grabbed_offset

    if not Input.is_mouse_button_pressed(BUTTON_LEFT):
        # Decreases size when no longer dragging
        if enlarged:
            self.scale = Vector2(self.scale.x / 1.2, self.scale.y / 1.2)
            enlarged = false
:bust_in_silhouette: Reply From: scrubswithnosleeves

Hmm, I don’t think the issue is using signals. Your code looks like it should work Except you should use global_position instead of position when calculating grabbed_offset.

I assume you are instancing the cards at runtime? Does this happen if you instance the cards in the editor and then run the game?