0 votes

Hi,
following the question from here: https://godotengine.org/qa/48956/how-to-toggle-torch-on-and-off?show=48956#q48956
I am trying to use scancode to make a flashlight and I wonder if it is even possible. the code looks like this:

if event is InputEventKey:
    if event.scancode == KEY_F and flashlight.visible == false:
        flashlight.show()
    elif event.scancode == KEY_F and flashlight.visible == true:
        flashlight.hide()

the problem as you guessed is that the light doesn't stay visible and flickers. Is there something I can do while using scancodes?

thanks

Godot version 3.5
in Engine by (20 points)

1 Answer

0 votes

First, the above code can be simplified to something like this:

if event is InputEventKey:
    if event.scancode == KEY_F:
        flashlight.visible = !flashlight.visible

However, that'll have the same problem as your original code. That is, it'll continue to toggle the light on/off as that code will fire as long as the key is pressed...

Is there a reason you're trying to do this via a scancode? Why not just define an input action for your flashlight and then use something more standard, like:

if Input.is_action_just_pressed("flashlight_toggle"):
    flashlight.visible = !flashlight.visible
by (14,902 points)
edited by

HI, the reason for using scancode is that when I migrate projects I have to define the input actions all over again therefore I try to use it as little as possible. I was just wondering if there was a way but it seems there is none. Thanks a lot for your answer! Also your simplification is very welcomed.

The input actions are stored in project.godot, so you can copy and paste them from one project to another.

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.