how to make a flshlight with event.scancode

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

following the question from here:
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:
	elif event.scancode == KEY_F and flashlight.visible == true:

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


:bust_in_silhouette: Reply From: jgodfrey

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

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.

dango1 | 2022-09-22 09:15

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

stormreaver | 2022-09-23 14:43