Toggle Buttons

In this tutorial website, we'll be creating the toggle mechanism using python. We will turn on and off a light.

Scene Setup

Create a dark environment and add a spot lamp, creating a dark environment is simple as pie! Delete everything in the scene, set the horizon and zenith colors to some dark color, add a plane and scale it to 10, add a spot lamp and bring it on Z axis to something around 10. Should be looking something like this.

Dark Environment

Coding

Let's import our module, define the current controller, get the owner and create a function that runs in a loop named as main().

try:
    import Range
except:
    import bge as Range
    
cont = Range.logic.getCurrentController()
own = cont.owner

def main():
    pass

We need to get our object's name which is a lamp and in python, its type will be KX_LightObject which is a subclass of KX_GameObject.

def init():
    if "init" not in own or own["init"] == False:
        own["init"] = True
        own["toggle"] = True

We create a function that runs once even though its wrapped with a sensor that runs in a loop. We also created a property that controls the light.
Now let's code the toggle mechanism, let's work on the looping function. We check for the user's input and if that certain input was triggered, we turn on the light.
Let's add an always and a keyboard sensor to the controllers. It looks something like this.

Logic Bricks Setup
def main():
    if own.sensors["Keyboard"].positive and own["toggle"] == False:
        own["toggle"] = True
    elif own.sensors["Keyboard"].positive and own["toggle"] == True:
        own["toggle"] = False

Here, we check that if the keyboard sensor is triggered and if the property toggle was False, we set it to True and vice versa.

def main():
    if own.sensors["Keyboard"].positive and own["toggle"] == False:
        own["toggle"] = True
    elif own.sensors["Keyboard"].positive and own["toggle"] == True:
        own["toggle"] = False

    # Light toggle part goes below
    elif own["toggle"] == True:
        own.scene.objects["Spot"].energy = 1.0
    elif own["toggle"] == False:
        own.scene.objects["Spot"].energy = 0.0
    else:
        pass

Here we check that if the property was True we set the light's energy to 1.0, light attribute needs a float when pass the value to it. Then on the third elif it happens vice versa. Finally if none of them takes place, we do nothing.
Here is the full code.

try:
    import Range
except:
    import bge as Range
    
cont = Range.logic.getCurrentController()
own = cont.owner

def init():
    if "init" not in own or own["init"] == False:
        own["init"] = True
        own["toggle"] = True

def main():
    if own.sensors["Keyboard"].positive and own["toggle"] == False:
        own["toggle"] = True
    elif own.sensors["Keyboard"].positive and own["toggle"] == True:
        own["toggle"] = False
    elif own["toggle"] == True:
        own.scene.objects["Spot"].energy = 1.0
    elif own["toggle"] == False:
        own.scene.objects["Spot"].energy = 0.0
    else:
        pass

The final result should look something like this.

Final Result

Download the project file.