How to make a key togglable and then check if it it toggled

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

Can someone please help I’m trying to make the alt key togglable and then to check if toggle is true.

I have set the alt key to gun safety and the left mouse button to shoot in the project settings under input map.

I want to make the alt key(gun safety) togglable and then check to see if the alt key’s toggle is false and then only print(“shoot”) when the left mouse button(shoot) is pressed.

For example

func  _process(delta):
	if Input.is_action_just_pressed("gun safety"):
	     somehow make alt key togglable 		
	if Input.is_action_just_pressed("shoot"):
		somehow make it check if the alt key's toggle is true 
        and then only if toggle is false print("shoot")
:bust_in_silhouette: Reply From: Millard

add this variable…

var safety = false

then, make your code be like this.

if Input.is_action_just_pressed("gun safety") and safety = false:
    safety = true
elif Input.is_action_just_pressed("gun safety") and safety = true:
    safety = false
if Input.is_action_just_pressed("shoot"):
    if safety = false:

I hope it helps, and please comment if it doesn’t make sense. =)

Those equality checks should use ==, not =. Additionally, that can be cleaned up some. Untested, but something like this:

var safety = false

func  _process(delta):
    if Input.is_action_just_pressed("gun safety"):
        safety = !safety

    if Input.is_action_just_pressed("shoot") && !safety:

jgodfrey | 2020-11-28 18:12

yeah, forgot about the ==. never seen ! used before with booleans. does that just switch the boolean?

Millard | 2020-11-29 04:10

Correct. For a boolean, that ! just returns the opposite of the current value.

jgodfrey | 2020-11-29 04:13

thnx that will be helpful in the future.

Millard | 2020-11-29 17:15