How do I stop events from firing twice?

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

I have a very simple project, and it seems events fire twice by default for some reason. Is there a reason for this, or a way to prevent it? I could probably implement a solution in _process(delta), but if there’s a way that isn’t a workaround, I’d prefer that fix instead.

Thanks in advance.

I think we need more information. What do you mean by “events”? Do you have a code sample?

Socrates | 2018-04-30 15:26

For example, triggering code with is_action_pressed()

It will always happen twice, which disrupts simple toggles and such.

Here’s my current input management script

https://drive.google.com/file/d/11E7bhQZwESGlJEZw6Rqac_PQ9oBRCouC/view?usp=sharing

Daimoth | 2018-04-30 16:02

:bust_in_silhouette: Reply From: mokalux

Hello there,

An answer would be to use the connect function in _ready().

Example:

get_node(“mybutton”).connect(“pressed”, self, “mybuttonpressed”)

And then write the function to be called:

**func mybuttonpressed():

do something

pass**

And voilà!

Hope this helps.

:bust_in_silhouette: Reply From: Socrates
is_action_pressed

fires repeatedly as long as the button is pressed. If you want it to just fire once, you can use:

is_action_just_pressed

It’s actually not a button. I have a script tied to a generic node that handles all my input in an autoloaded scene so I can handle input in a scene-agnostic way. In that script, I handle all clicks and key presses with the _input(event) function.

Perhaps it would be easier if I shared the project, as it’s still quite small and should clarify things.

https://drive.google.com/open?id=1ZUbGzh_zIo1XJLreLgtXNDgoM3FsXFkF

If you wish to peruse the project, follow that link and click Hallowspire toward the top of the page, then download the folder.

Thanks in advance, sorry for the lack of clarity.

But yeah all events firing twice is making my click to move stuff difficult as well. The code triggers twice, so the sprite always moves twice as far in the direction of the click than intended.

Edit: actually, I’ve decided to ditch that approach and decentralize my input handling code. For some reason, if you do it the way I was doing it, events fire twice. I’m not sure why, but it’s not worth the hassle.

Daimoth | 2018-04-30 22:11

I have the same problem with my L2 (and presumably R2) buttons.
L2 and R2 are xbox-one controller trigger (variable) button types.
I gather my input in the _physics_process function.
I have a print line to debug which shows me the frame number:

print("get_tree().get_frame() = ", get_tree().get_frame()))

The other (not L2, R2) ‘buttons’ on the xbox controller do not have this issue so I think it is a problem when the variable-trigger type buttons only.
I get the input like this using the ‘Just Pressed’ variation as to prevent double input when holding the button down:

input_just_pressed[button_map.l2] = Input.is_action_just_pressed("ui_l2_" + str(input_controller))

When I press L2 once it registers twice in two different consecutive frames:

get_tree().get_frame() = 306
get_tree().get_frame() = 307

I will most likely have to add code to account for this ‘debounce’ input issue. Something along the line of keeping track of the delta for inputs as I do for input duration counts for other gameplay (holding buttons for x seconds etc).

Thanks,
Shaun

sjharb | 2021-05-29 17:41

Ok I found my issue. I am storing the inputs in an array:

input_just_pressed[button_map.l2]

But I was not collecting the input for certain open menu/hud selection conditions. By not collecting the input and updating my button press array, the value was still set to ‘true’ (i.e. pressed) for my input processing. So if anyone else is storing these input values they will want to be aware of such a coding mistake.

Thanks,
Shaun

sjharb | 2021-05-29 18:00