I need help with this toggle.

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

Im trying to make this input toggle sneaking in my 2D game, sneaking makes you invisible to enemies and that part seems to work but this isnt working so if anyone could help I would appreciate it.

if Input.is_action_pressed("sneak") and Game.sneak == false:
	Game.sneak = true
	print("sneak")
	await Input.is_action_pressed("sneak") and Game.sneak == true
	Game.sneak=false
	print("nosneak")
:bust_in_silhouette: Reply From: jgodfrey

If you just need a boolean toggle, something like this should work:

if Input.is_action_just_pressed("sneak"):
    Game.sneak = !Game.sneak

That’ll simply toggle the boolean value of the Game.sneak variable each time the sneak action is pressed…

Also, note the change from is_action_pressed() to is_action_just_pressed()

jgodfrey | 2023-06-28 17:36

thank you, helps a lot

powerplayerplus | 2023-06-28 17:54