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

in Engine by (145 points)

2 Answers

+1 vote

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

var buttonIsPressed = false

func _ready():

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

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():

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

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

by (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:


+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

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

  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.

by (7,808 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.