BGE basic scripting?

Hello, I am generally new to scripting, as in I just started looking into it about. . 3 hours ago. I know I should keep studying, reading, and etc. But it all seems too. . tedious?:confused:
But I’m here asking for the plain basics for BGE scripting. Or is what I’m asking too much of a broad subject? Any help is appreciated. Thanks!

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:

Wow, nice introduction to python in BGE.

That post should be copied over to the resources forum.

I second this.

Can I third this? I use those functions several times every script.

Hi Dylan Ringstad,

The BGE scripting/programming involves the python language and the Blender Game Engine (BGE) Application Programming Interface (API).

If you are new with the python scripting/programming, I highly recommend to look some pure python tutorials to get some basic idea about given language:

Once you get basic grip, it will be MUCH easier to start to working with the BGE API in BGE.

My 2 cents