0 votes

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

asked Jul 15, 2017 in Engine by ismaelgame7 (143 points)

2 Answers

+1 vote

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.

answered Jul 15, 2017 by Qws (97 points)

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:

https://www.dropbox.com/s/56rsurshrbaizqb/Double%20jump.rar?dl=0

+1 vote

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.

answered Jul 16, 2017 by eons (7,764 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.