0 votes

Having problems making a working torch I can't seem to remember the toggle script
Extends staticbody
Onready var light = $SpotLight
Func input (event):
If input.is
actionjustpressed("flashlight")
$SpotLight.show()
Else:
$SpotLight.hide()

asked Jul 23 in Projects by Ogeeice (126 points)

1 Answer

0 votes

I do not know exactly what problems you encounter, but maybe try this:

extends OmniLight

func _ready():
    pass

func _input (event):
    if Input.is_action_just_released("ui_right"):
        show()
    elif Input.is_action_just_released("ui_left"):
        hide()

You made an onready variable and then didn't use it. Moreover, are you sure that this $SpotLight is a proper reference to the light in question? You might wanna check that this $SpotLight actually references something and that it's not null. The above code will work if you place it directly on the Light node in question.

answered Jul 24 by johnygames (750 points)

The problem I'm having is that the button does not switch it on and off it only comes on once and if pressed again switches it off for some second then it only makes it blink

Instead of writing an else statement, write the code I provided above, namely, an elif statement. Right now your code only switches the light on when a key has just been pressed, but in any other case (as indicated by your else statement), the light remains switched off. If you want the same button to trigger the light on/off, then try the following:

extends RigidBody
onready var light = $SpotLight
func _ready():
    pass

func _input (event):
    if Input.is_action_just_pressed("ui_right") and light.visible == false:
        light.show()
    elif Input.is_action_just_pressed("ui_right") and light.visible == true:
        light.hide()

I noticed you using two different keys to toggle its visibility I'm trying to make a single key do that instead

Read my last comment carefully. I write: "If you want the same button to trigger the light on/off, then try the following:" It is meant to toggle the light on and off using the same bbutton.

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.