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


cont.sensors['sensor_name']
cont.actuators['actuator_name']

Determine if a sensor is active through

sensor.positive

Activate/deactivate actuators though

cont.activate(actuator)
cont.deactivate(actuator)

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

own['property_name']

You can get other objects through

GameLogic.getCurrentScene().objects['object_name']

Basically thats a run down of the most common/basic functions of the API. For more info on the API consult the docs:
http://www.blender.org/documentation/249PythonDoc/GE/

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
            continue
        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
else:
    # space is not pressed so we wont track
    cont.deactivate(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:
http://codesyntax.netfirms.com/lang-python.htm

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

http://codesyntax.netfirms.com/lang-python.htm

nice catch , thanks .

Nice tutorial!
gill

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.

http://www.blender.org/documentation/249PythonDoc/GE/GameTypes.KX_GameObject-class.html#position

Just adding on:

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

To get an object’s position:
own.worldPosition

Great intro OTO!
-Sunjay03

Congratulations should go to andrew-101, not me!

Congratulations Andrew-101!!!

;D

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…

Hello

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