Hi!
Godot 3.5 RC8
I have an autoloaded Events.gd class

extends Node
signal click_cell(value)

In Cell.gd class I work with this signal

extends Area2D

func _ready():
	Events.connect("click_cell", self, "clickCell")

func _on_Cell_input_event(_viewport, event, _shape_idx):
	if InputEventMouseButton and event.is_pressed() and event.button_index == BUTTON_LEFT:
		Events.emit_signal("click_cell", [self])

func clickCell(arr):
	print(arr[0])

But when I click on an object, instead of one response, I get four!

@Cell@3:[Area2D:1386]
@Cell@3:[Area2D:1386]
@Cell@3:[Area2D:1386]
@Cell@3:[Area2D:1386]

The logic of work is such that it is necessary to catch the click_cell signal
How to fix this, I do not understand and ask for your help.
Thank you!

  • duane replied to this.
  • CrySani

    I'm guessing you have four objects spawned. One global signal is connected to each object, so every time it's sent, every object will respond with the same argument. That's assuming _on_Cell_input_event() is called from _input() or similar. It would help if more of your code was visible.

    If you do this, they'll only respond when they get a signal meant for them.

    func clickCell(arr):
    	if arr[0] == self:
    		print(arr[0])

    _on_Cell_input_event

    What causes that event to be triggered?

    You might add to that function:
    print_debug(event)

    CrySani if InputEventMouseButton and event.is_pressed() and event.button_index == BUTTON_LEFT:

    "if InputEventMouseButton" isn't doing anything. You probably meant "if event is InputEventMouseButton".

      duane
      Yes you are right. Corrected, but the problem is not solved

      If you make an intermediate script, and catch a signal in it, then everything is in order.
      The problem is, if we send a new click_cell signal from the _on_Cell_input_event signal, then it fires 4 times

        CrySani

        I'm guessing you have four objects spawned. One global signal is connected to each object, so every time it's sent, every object will respond with the same argument. That's assuming _on_Cell_input_event() is called from _input() or similar. It would help if more of your code was visible.

        If you do this, they'll only respond when they get a signal meant for them.

        func clickCell(arr):
        	if arr[0] == self:
        		print(arr[0])

          What is triggering the callback and how many objects are in the scene? Also, the logic doesn't really make much sense. You are sending a signal to a global only to call a local function. You could eliminate the signal altogether and just call the function.

            duane
            You are absolutely right!
            All such objects receive this signal, and display exactly the object on which the mouse button was pressed.
            I couldn't figure it out without your help.
            Thanks again!

            cybereality
            Yes, that's possible. But the logic of work is such that copies of classes should not know about other copies. Just catch signals. And the signal is generated by the copy that was clicked on.

            Question, what is invoking '_on_Cell_input_event', what signal is it connected to? Because that function's argument signature is not familiar to me.