Python Scripting - Introduction to

Basic/Fast introduction to BGE Scripting by andrew-101

You call a script through a python controller logic brick.

You can get that controller though

cont = GameLogic.getCurrentController()

You can get any sensors or actuators connected to the controller through


Determine if a sensor is active through


Activate/deactivate actuators though


You can get the object which has the controller on it through

own = cont.owner

You can then change the objects position through

own.position = [x,y,z]

Access properties through


You can get other objects through


Basically thats a run down of the most common/basic functions of the API. For more info on the API consult the docs:

So by stringing those together you can create scripts. Its also good to note that each sensor and actuator have their own attributes. Here is an example that will make an object look at the closest object to it. The following script assumes that an always sensor, a keyboard sensor set to trigger on SPACE (called ‘space’) and an actuator set to edit object > track to (Called ‘track to’). Connect all this to a python controller.

cont = GameLogic.getCurrentController()
own = cont.owner
track = cont.actuators['track to'] # track to actuator
space = cont.sensors['space'] # keyboard sensor

if space.positive:
    object_list = GameLogic.getCurrentScene()
    closest = object_list[0] # set a default
    distance = own.getDistanceTo(closest)
    for object in object_list: # iterate over all the objects in a scene
        if object == own:
            # we dont the object to track to itself
        tentative_distance = own.getDistanceTo(object)
        if tentative_distance < distance:
            # this object is closer than our previous distance, so we have to change out distance and closest variables
            distance = tentative_distance
            closest = object
    track.object = closest # the object attribute determines which object will be tracked to
    cont.activate(track) # activate the track to actuator
    # space is not pressed so we wont track

I hope that gives you a run down of the useful functions and how to use them.

If you don’t understand some of the syntax browse over this helpful guide:

Nice intro andrew-101. Maybe someone could add to this with the differences between module/script python controllers too.

nice catch , thanks .

Nice tutorial!

This is a nice run-down of what makes up a typical BGE script. One thing I noticed is the use of KX_GameObject.position. This has been deprecated in favor of KX_GameObject.localPosition and KX_GameObject.worldPosition.

Just adding on:

To set an objec’s position:
own.localPosition = [x, y, z]

To get an object’s position:

Great intro OTO!

Congratulations should go to andrew-101, not me!

Congratulations Andrew-101!!!


OK, guys, that’s pretty nice, so lets track back a bit (bear with me please I’m really a noob at python, but must learn it).
Before going into the cool scripts, how about showing how I get a motion actuator to activate? I need examples on servo, Loc, Force, Rot,etc.
I believe it’s nice to have some movement first…


All of this has been very useful to me, thank you.
I’m having trouble with the last piece of code. I’m using it as shown, which I hope is correct.

spaceship = GameLogic.getCurrentScene().objects['Cube.003']

And I am returned with:

Python script error from controller “cont2#CONTR#3”:
Traceback <most recent call last>:
File “health1”, line 10, in <module>
KeyError: "CList[key]: ‘‘Cube.003’’ key not in list

Any suggestions?

spaceship = GameLogic.getCurrentScene().objects['OBCube.003']

I think that should work, and yes, it is different to yours. :wink:

thanks it worked lol, thats code for you

lol, glad to help :yes:

Watch out though, in Blender 2.5, there will be no prefix.

Just in case anyone cares, I’ve switched to the bge module. so instead of GameLogic, bge.logic. instead of Rasterizer, bge.render, etc.

reference: Blender Documentation contents — Blender v2.53 - UNSTABLE API documentation
NOTE: only works in 2.5x