Hey all! Okay I seem to have a knack for making stuff fire twice. xD

I have this function that checks for a left mouse click (anywhere, a global click), I set it up like in the example below. print(_event.as_text()) fires twice for one click - why is that? Shouldn't it fire just once for 1 click? I didn't add to the code cause I want to iron this kink out first.

func input(event):
if _event is InputEventMouseButton and _event.button_index == BUTTON_LEFT and event.pressed:
print(
event.as_text()) # this line runs twice

The script is attached to a Node2D who is a child of root node of type Node.

I have Godot 3.4.4, Windows 10.

Would appreciate anyone's help!

I tried your code, and it works fine. Only 1 click. But you had a few typos, not sure if you wrote by mistake on the forum.

extends Node2D

func _input(event):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
		print(event.as_text())
  • Owl replied to this.

    cybereality Hey! I accidentally shifted between event and _event here on the forums - but not in Godot.

    To make sure, I copy pasted your code into my function - and I'm getting the same error. I get two outputs.

    What could be the reason for two outputs here?

    This is a copy of what I'm seeing in the output window:

    InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(91, 93.333336), button_mask=1, doubleclick=false
    InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(91, 93.333336), button_mask=1, doubleclick=false

    Edit: apparently I'm getting 2 messages, but the code only runs once. If I add the line rec += 1 (an integer that is assigned 0 when declared), in both outputs it shows as 1. Can't really explain it then.

    Are you sure that there's only one instance of that node and script?

    • Owl replied to this.

      DaveTheCoder For sure. Only one instance - in this scene or otherwise - and only it uses this script. Is there a way by code to check how many instances are running of this node?

      Can you take a screenshot of the tree on the left side? I would bet the script is attached twice somewhere. Note that if you have a saved scene, the script can be attached one the scene and within the scene, and it is hard to tell the difference.

      • Owl replied to this.

        cybereality Sure. The scene is called SceneManager, its script is called scene_manager (the one with the func _input() above). It appears in the first image.

        TextController has another script called load_json_file.
        Data has another script called data_saving.
        Text - a scene on its own - has a script called text.

        Text (the scene), when opened in its own scene window, appears in the second image. It's comprised of Node2D (root), and its children are NinePatchRect, MarginContainer, Label.

        None of the scripts, except the one in question, has the _input() function.

        • Owl replied to this.

          Owl Update: so, it's more than that. Apparently every node in the scene (the first scene, with TextController as a root node) seems to enter the scene twice, as whenever I run print("testing") in the _ready() function of each node, I get two outputs.

          What may cause them to enter the scene twice?

          With SceneManager I do have a few onready vars to refer to the other nodes, but that shouldn't be a problem should it? I have it written like so:

          onready var text_controller = get_node("../../TextController")
          onready var data = get_node("../Data")
          onready var text = get_node("../Text")

          Update: I do have TextController auto loaded, if it makes a difference.

          • Owl replied to this.

            Owl Okay, I think that's it. Because the scene is auto-loaded, and because I'm on the scene when I'm testing it, it runs twice. One for when it's auto-loaded, and once because I'm F6-ing the current scene.

            I suppose that must be it.

            Do you have a scene instanced inside itself as a child scene? (I'm not even sure if that's possible.)

            The .tscn scene files are text files. You could look at them and see if anything looks odd.

            • Owl replied to this.

              DaveTheCoder The scene in the text editor looks like this (is there anything suspicious here?):

              [gd_scene load_steps=5 format=2]

              [ext_resource path="res://load_json_file.gd" type="Script" id=1]
              [ext_resource path="res://scene_manager.gd" type="Script" id=2]
              [ext_resource path="res://data_saving.gd" type="Script" id=3]
              [ext_resource path="res://Text.tscn" type="PackedScene" id=4]

              [node name="TextController" type="Node"]
              script = ExtResource( 1 )

              [node name="SceneManager" type="Node2D" parent="."]
              script = ExtResource( 2 )

              [node name="Data" type="Node" parent="."]
              script = ExtResource( 3 )

              [node name="Text" parent="." instance=ExtResource( 4 )]
              script = null

              That file looks okay, then.

              If you upload your project folder as a .zip, I or someone else could take a look at it. (Exclude the .import subfolder from the .zip, since it tends to be big and gets automatically regenerated.)

              • Owl replied to this.

                DaveTheCoder Thanks so much for the offer!

                But I think I've determined the problem. I was testing the scene that was also auto-loaded, so basically it was running it twice (because I was running it, and because it was also auto loaded). When I was testing another scene, I only got 1 output from the ready() function of the auto-loaded scene (meaning it was loaded just once - when it was auto loaded). I've done something similar before... I think the lesson is don't test a scene if it's auto loaded ^_^ run a test through a different scene.