This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

New godoter here. :)

So I am attempting to pause the game with one hit of the button then if you press that same button again it unpauses. But instead of that happening it just pauses for a second or two, I've tried some things to fix this but I'm so stumped right now. Not sure what to do to fix this, thanks in advance.

I've got a scene with a control node, a popup, and a label if that helps to know in any way.

extends Control
var Is_pause_on = false
func _input(event):
if event. is_action_pressed("PauseButton") and (Is_pause_on == false):
    get_tree().paused = (true)
    yield(get_tree().create_timer(10000), "timeout")
    Is_pause_on = true
    event. is_action_pressed("PauseButton") and (Is_pause_on == true)
    get_tree().paused = (false)
    Is_pause_on = false
in Engine by (15 points)
edited by

1 Answer

0 votes
Best answer

First of all, please use the Code sample function to format your code, as you've done for extends control, its prettier and easier to read :) :

Your problem is coming from your condition

if event. isactionpressed("PauseButton") and (Ispauseon == false):
gettree().paused = (true)
yield(gettree().createtimer(10000), "timeout")
Ispauseon = true
event. isactionpressed("PauseButton") and (Ispauseon == true)
gettree().paused = (false)
Ispause_on = false

Let's see what happens when you press for example ui_down ( native to any project, usally mapped with the key down of your keyboard)

func _input(event):
    if event.is_action("ui_down"):
        print("the action is ui_down")
    if event.is_action_pressed("ui_down"):
        print(" ui_down key is pressed ")
    if event.is_action_released("ui_down"):
        print(" ui_down key is released ")

results :

the action is ui_down
ui_down key is pressed 
the action is ui_down
ui_down key is released 

Every frames the button ui_down is pressed the function _input will be called. In this case it's even called two times the same frame . But event.is_action_pressed("ui_down"), will be true only the first time ( this is speciifc to the function _input(event), more details here ->
The same way the event.is_action_released("ui_down"), will be true only ... well when you release the button.

And here's the problem in your function, the else condition.
The first time you will enter in your condition. It's what you are expecting. But because the function will be call a second time ( the same frame if you release the button, or the next frame if you still hold it), you will enter in the else part .
So in the same time you are pausing the game and unpausing it.

I recomend you to look at , to implement a good pause button.

Otherwise to suit your code, here's my answer

var Ispauseon = false

func _input(event):
    if event.is_action_pressed("PauseButton") :
        Ispauseon = !Ispauseon   # get the inverse of ispauseon, if it's false it become true, and inverse.
        get_tree().paused = Ispauseon  #
by (288 points)
selected by

Oof I really should've read the docs more closely, though I got confused with the code they used.

func _on_pause_button_pressed():
get_tree().paused = true

func _on_pause_popup_close_pressed():
get_tree().paused = false

Wouldn't this just lead to the same problem if the buttons had the same keys assigned to them? I think I might be over complicating this. >.<

Thanks for the help, the code worked, though I'm gonna test a bit, mess around and learn some more, after all I got time to learn. :)

Hope you have a nice day/night.

:Oh and I must've pressed enter too many times on here, I'll fix that quick so people/and me/ won't get eyesore from that coding. Eeek:

is_action_just_pressed("action") would probably be preferable to creating a global variable, just my 2 cents

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 Frequently asked questions and 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 [email protected] with your username.