Is there any way to check if a button has been pressed only once?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ismaelgame7
:warning: Old Version Published before Godot 3 was released.

Hello people, how are you?

I wanted to check if a button was pressed only once, But I do not know how to do this.
I was wondering if you have any way to do this.
In the game maker it is easier to do this.

Someone help me please

:bust_in_silhouette: Reply From: Qws

You can add a script to that button. For example like this:

var buttonIsPressed = false

func _ready():
    pass

func _on_YourButton_pressed():
   if(buttonIsPressed == true):
      #doNothing, because it's pressed before
   else:
      buttonIsPresed = true
      doSomething()

So you have to make a variable where you can store data when the button is pressed.
You can also do with numbers.

var buttonIsPressed = 0

func _ready():
    pass

func _on_YourButton_pressed():
   if(buttonIsPressed >= 0):
      #doNothing, because it's pressed before
   else:
      butonIsPressed = (1 + buttonIsPressed) #increasing the count with 1+
      doSomething()

This way you set exactly how many times you can press the button.

I wanted to check one of my keyboard keys if I was pressed once.
Because I wanted to make a double jump to my project.
A friend has already helped me in this and here is an example for anyone who wants to use:

Dropbox - Double jump.rar - Simplify your life

ismaelgame7 | 2017-07-15 22:48

:bust_in_silhouette: Reply From: eons

You want to check press once? there are many ways to do that.

The simplest is to add a flag, if the key/action was pressed, change that flag and don’t allow more presses.

Then do the opposite on _input with release and the same flag to enable it again.

Like (pseudocode):

export(int) var max_jumps = 2
var jump_remaining = max_jumps
var jump_unlocked = true
...

_fixed_process:
  ...
  if jump_unlocked && jump_remaining>0 && Input.is_action_pressed("jump"):
    do_jump_stuff()
    jump -= 1
    jump_unlocked = false #can't jump yet (lock)
  ...

_input(event)
  ...
  if !jump_unlocked && event.is_action_released("jump"):
   jump_unlocked = true #can try to jump again (unlock)
  ...

This should work with key input too, and if checking for pressed on _input be careful with echoes (event.is_echo()).


Godot 3 will have just_pressed for input actions to make this more easy.