how to make the input return a value only ONE time

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

im noob in godot and english isn’t my language

I’ve already done a bit of pygame and, when I wanted to change the boolean value of the variable just once when i pressed a button, I did it this way:

var activate = false

if  "some button is pressed":
    activate = true
    activate = false

if activate == true:

#console: hi
#process ended.

But in godot, this not work, the boolean value still the same:

var activate = false

if  "some button is pressed":
    activate = true
    activate = false

if activate == true:

#console: process ended.

note: i know, that isn’t the pygame language but, its just to represent if it was in godot what im trying to do.

so, i tried to use a timer, functions and other things but, none of them is really helping…

what I really wanted is a way that, for example:

if the player clicked the attack button, it would return the value true for a variable ONE time, and then, it would have to wait for a recharge to be able to attack again and so on…

what is making it difficult for me is trying to return the value appropriately ONE time, any simple way to do this?

example of the result that i want:

:bust_in_silhouette: Reply From: Gluon

If you do this

func _ready():

func Print_Hi_Function():

then this will work once. It would however have no controls, you could also add a boolean control like this

var ControlBool = false

func _process():
    if ControlBool == false:
        ControlBool = true

and this will print it once only unless somewhere else in the code you change the control boolean back to false.enter code here

ok, i will try to use this metody, Thanks for the answer

random_developer | 2022-11-17 01:27

:bust_in_silhouette: Reply From: jgodfrey

Seems like you’re trying to manage a basic cool-down cycle. Here’s a typical way to do that…

  • First, add a Timer to your scene (to manage the cool-down time)
  • Set the timer’s One Shot property to True
  • Set the timer’s Wait Time to the amount of time you want for the cool down
  • Set the timer’s Autostart property to False
  • Wire the timer’s timeout event to a script function

In your script, do something like this (untested):

var can_shoot = true # by default, we can shoot

func _physics_process(delta):
	if Input.is_action_just_pressed("fire"):
            if can_shoot: 
                can_shoot = false
                $Timer.start() # start the cool down timer
                print ("shot fired!")
               print("waiting for cool down cycle")

func _on_Timer_timeout():
    can_shoot = true # when the timer times out, we can shoot again
  • And, finally, season to taste…

ok, i will try it now

random_developer | 2022-11-17 21:37

it worked, now it’s working perfectly, thank you very much

random_developer | 2022-11-17 21:47

i will use your example to explain what is happening

you helped me a lot but, it seems that can-shoot is beeing ignored, its just changes when i release the button.
i am confused because is-action-just-pressed has not effect… its acting just like the is-action-pressed… any idea of how to resolve this?

random_developer | 2022-11-17 22:23

Hmmm… I don’t understand what you’re describing above. Maybe try again?

You should see the shot fired! message the first time you press the fire action. That should start the cool-down timer. If you press the button again (while the timer is counting down), you should see the waiting for cool down cycle message - indicating you can’t fire again until the cool-down completes. Once the timer runs out, can_shoot will be reset and you should be able to fire again - repeating the cycle…

jgodfrey | 2022-11-17 22:35

yes yes… this is working:

the function is working perfectly, the problem is that, when I HOLD the button(input), it doesn’t stop giving true, it just stops if I myself stop holding the button, I’ll show you the code

#the input is the left mouse button
#acionamento is the variable of attack

#rec_ataque1 is identic to can_shoot in your example

if Input.is_action_just_pressed("ataque1"):
	if rec_ataque1:
		acionamento = true
		rec_ataque1 = false
		acionamento = false

I think the problem is in rec-ataque1, which is being ignored, it just gets false when I release the button(input).

random_developer | 2022-11-17 22:58

What does the timer code look like? The rec_ataque1 value should be set to true in the timer’s timeout event code, like my original example…

jgodfrey | 2022-11-18 02:32

the code is working now, I had put the input code in the wrong place in the script, I’m really a noob lol

random_developer | 2022-11-18 13:05