Pause only works for a few seconds or when I hold it. Answered. <3

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

New godoter here. :slight_smile:

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
else:
	event. is_action_pressed("PauseButton") and (Is_pause_on == true)
	get_tree().paused = (false)
	Is_pause_on = false
:bust_in_silhouette: Reply From: UnRealCloud 1

Hi,
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 :slight_smile: :

Your problem is coming from your condition

if event. isactionpressed("PauseButton") and (Ispauseon == false):
gettree().paused = (true)
yield(gettree().createtimer(10000), "timeout")
Ispauseon = true
else:
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 → Input examples — Godot Engine (stable) documentation in English.
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 https://docs.godotengine.org/fr/stable/tutorials/misc/pausing_games.html , 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  #

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
$pause_popup.show()

func _on_pause_popup_close_pressed():
$pause_popup.hide()
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. :slight_smile:

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:

JadeWizard | 2020-08-05 20:09

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

DDoop | 2020-08-05 20:13