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

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:

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

by (391 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:

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 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.