0 votes

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")
in Engine by (26 points)

1 Answer

0 votes
Best answer

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:
        print("shoot)

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

by (379 points)
selected by

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:
        print("shoot")

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

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

thnx that will be helpful in the future.

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 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 webmaster@godotengine.org with your username.