How to make flashlight turning on and turning off by one button?


#1

I want code flashlight. when player press f he will turn on flashlight. if he will press f while flashlight is on he will turn of flashlight. I can code it with 2 buttons but i don´t won´t do something like this. pls help.


#2

use a variable to store state of the flashlight
for example by default value 0 meanning turned off and if value is 1 turned on


#3

You can also do it by adding a variable that stores the state of the torch! You can set default to 1 or -1 and when the user clicks the button do -(YourVariable) to the value of your variable (trigger once) ! And add the events when the variable is -1 turn on the light and when it is one turn off or just switch the values according to your needs! (this will switch the values between 1 and -1 always) .It works perfectly for me while making mute button that mutes and unmutes aswell as while making pause button that pauses and unpauses the game!
But anyway you can probably do it with other different methods!


#4

i already done this. (0=off 1=on) But if you press q while variable is 0 it will turn variable 1 and i have same thing but opposite numbers when variable is 1. But if you will hold that button (in my case it´s q) you won´t see it.


#5

yes i forgot you need a second variable
when click left button set a scene variable to 0 then when you check light state with the oder variable check also that this variable is 0 d your work and set to 1. Jere is the pseudocode
light=0
if mouse click then pressed=0
if light=0 and pressed =0 then
turn on light
change button animation
pressed=1
light=1
end if
if light=1 and pressed =0 then
turn off light
change button animation
pressed=1
light=0
end if
sorry i’m a programmer :sweat_smile:


#6

Try this :stuck_out_tongue:

If Q is pressed do Once Variable + 1
If Variable > 1 then do Variable = 0
If Variable = 1 then turn light on
if Variable = 0 then turn light off


#7

it works. Thanks :slight_smile: