From BlenderWiki

Jump to: navigation, search

[edit] Game Engine Python

Python inside blender game engine works differently than it does in other parts of blender. Scripts are activated by sensors, and rely on logic bricks to do a lot of the work.

Python scripts and modules are run from a Python Controller.

[edit] Basic Python

[edit] The Console

The console is an important part of python in BGE. Errors in scripts will be printed in the console. To help debugging it is also possible to write things in the console with the print statement.

print "Hello World"

What is important to remember when fixing bugs is that the error messages is just what the compiler thinks is the error. It can't know your intentions. An error can also cause other errors, starting from the top of the list can often be quicker if there are many errors, and some are causing other. An error starts with: "python error compiling script - controller" followed by the controller name, script name, and line on which the error was raised. Example of error messages:

  • SyntaxError: invalid syntax. The syntax of the code is wrong, can for example be a missing colon (:).
  • IndentationError: expected an indented block. This means there should be an indentation in the script, either with tab or with spaces.
  • IndentationError: unexpected indent. Means there shouldn't be an indent there, apart from that it could also be:
    • Accidentally starting a line with a space
    • A syntax mistake in a previous line making the compiler assume it shouldn't be indented.
  • AttributeError: " object name " object has no attribute attribute. This is caused by trying to access an attribute which isn't there.

[edit] GameLogic

The most important module that comes with blender is GameLogic, it is imported automatically when you run the game engine. It gives access to logic bricks. A script can only access logic bricks connected to the sensor that runs the script. The script will only run when at least one of the sensors connected to it sends a pulse. There is a hierarchy between the bricks and the object.

To get the controller, that runs the script:

GameLogic.getCurrentController()

This can be stored in a variable, cont, for reuse. For example one named cont.

cont = GameLogic.getCurrentController()

To get a list of sensors connected to the controller and store them in the variable sens:

cont = GameLogic.getCurrentController()
sens = cont.sensors

To access a specific sensor it can be done by either the sensor name ["SensorName"], or by its index in the list, starting with zero, [0]. The sensor name is the name entered in the sensors logic brick. Here the sensor named sensor is stored in the variable a, and the first sensor in the list stored in b.

cont = GameLogic.getCurrentController()
sens = cont.sensors
a = sens["sensor"]
b = sens[0]

Actuators works exactly the same, but with actuators instead of sensors.

cont = GameLogic.getCurrentController()
actu = cont.actuators
a = actu["actuator"]
b = actu[0]

To find the attributes in each sensor, either look it up in the API, or use the dir() function. For example if act is a motion actuator connected to the controller.

a = GameLogic.getCurrentController()
print dir(a.actuators["act"])

will print this in the console:

['__delattr__', '__doc__', '__getattribute__', '__repr__', '__setattr__', 'angV'
, 'dLoc', 'dRot', 'damping', 'executePriority', 'force', 'forceLimitX', 'forceLi
mitY', 'forceLimitZ', 'getAngularVelocity', 'getDLoc', 'getDRot', 'getDamping',
'getExecutePriority', 'getForce', 'getForceLimitX', 'getForceLimitY', 'getForceL
imitZ', 'getLinearVelocity', 'getName', 'getOwner', 'getPID', 'getTorque', 'inva
lid', 'isA', 'linV', 'name', 'owner', 'pid', 'reference', 'setAngularVelocity',
'setDLoc', 'setDRot', 'setDamping', 'setExecutePriority', 'setForce', 'setForceL
imitX', 'setForceLimitY', 'setForceLimitZ', 'setLinearVelocity', 'setPID', 'setT
orque', 'torque', 'useLocalAngV', 'useLocalDLoc', 'useLocalDRot', 'useLocalForce
', 'useLocalLinV', 'useLocalTorque'] 

Which is all functions and attribute that the motion actuator has.

[edit] BGL

[edit] GameKeys

[edit] Geometry

[edit] Mathutils

[edit] Rasterizer