How to toggle torch on and off

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

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_action_just_pressed(“flashlight”)
$SpotLight.show()
Else:
$SpotLight.hide()

:bust_in_silhouette: Reply From: johnygames

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.

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

Ogeeice | 2019-07-24 07:54

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()

johnygames | 2019-07-24 11:11

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

Ogeeice | 2019-07-24 22:16

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.

johnygames | 2019-07-24 22:19