Make a keyboard key toggle (on/off states)

I have a flashlight in my game.
I would like to turn it’s lamp on and off with one key (F).

I can picture the logic but don’t know how to write it yet! :o
I have an animation for the lamp that has two key frames.
Key Frame 1 = lamp power 0.000
Key Frame 2 = lamp power 2.000

Here is what I envision:

default state is lamp power 0.000 (key frame 0 for start of the game)
the animation for the light is called Flashlight_On_Off

if FKEY = True, and animation frame = less than 2, play animation frame 2
if FKEY = True, and animation frame = 2, play animation frame 1

right now I have it working fine with two keys…would much rather have only one.

Okay…i figured it out with the help of a tutorial on youtube.

import bge

def main():

    cont = bge.logic.getCurrentController()
    light = cont.owner = light ["energy"]
    if light ["lamp"] == True:
        light ["energy"] = 2.000
        light ["energy"] = 0.000


this is working with:
two game properties on the “lamp” : one called lamp set to boolean and one called “energy” set to float with a value of 2.000.
Always sensor --> Python controller
Keyboard sensor --> And controller --> Property actuator with the property “lamp”

Now I have a flashlight that toggles on and off when I push F.
Now what I need is a timer of some sort so that you can’t just click the light on and off by pushing the F key repeatedly. I want to make it so that the key press won’t work for a full second or two. (this will keep my arm animation and sound that is also set to the F key from getting out of whack)
Basically what happens is: Hit F and a sound plays, the light goes on, and an arm with the flashlight pops up into view of the camera. Hit F again and the sound plays, the light goes off, and the arm goes out of view. I want it so that once it comes up or down, if you hit the F key too quick then nothing happens for a full second or two. Whats’ happening now is the sound and light always work like they should but the animation of the arm is about a second long, so if you hit F while the arm is still moving the light goes on or off and gets out of whack with the animation. No good…


you can controll the energy of the lamp via python too, so you dont need an extra property “energy”: = 10.0

Of course only if the lamp is calling the function itself, else you have to get the lamp over the scene.

Next to the timer. I will start from scratch and you can addapt how you need it:

Properties on the lamp:

on [Boolean] - TRUE #the state of the lamp, so either on or off :smiley:
timer [Integer] - 0 #will be later to the time you need for the animation to finish. This is for how long you wont be able to switch the state of the lamp. You could also use a [Timer] Property but i hate them as they are ALWAYS counting up and you have to make them stop and what not…
timer_go [Boolean] - FALSE #if the timer counts up
timer_length [Integer] - 10 #this determines how long you cant switch the lamp again. Using an extra property here because there are multiple places it is used and it is easier to just change one number instead of searching everywhere

Logic Bricks:

Keyboard Sensor [F Key] — AND — Property [Mode: Toggle; Property: on]
Property Sensor [Eval. Type: Not Equal to; Prop: timer_go; Value: TRUE] — #CONNECT TO SAME AND AS ABOVE#

Property Sensor Name it “switched” [Eval.Type: Changed; Prop: on] — PYTHON [Module: lamp.switch]

Property Sensor -Activated TRUE level triggering- [Eval. Type: Equal to; Prop: timer_go; Value: TRUE] — AND — Property Actuator [Mode: Add; Prop: timer; Value: -1]

Property Sensor [Eval. Type: Equal; Prop: timer; Value: 0] — AND — Property Actuator[Mode: Assign; Prop: timer; Value: timer_length #set to same time as before; how much the animation needs to finish]
#CONNECT TO SAME AND AS ABOVE# — Property Actuator[Mode: Assign; Prop: timer_go; Value: FALSE]

Python Script

import bge

def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    sens = cont.sensors["switched"]

    if own["timer"] == own["timer_length"] and sens.positive:
        if own["on"] == True:
   = 10.0 #set to how much energy you want
            own["timer_go"] = True
   = 0.0 #0 = off
            own["timer_go"] = True

Hope this is helpfull.