Module GameLogic

Module GameLogic

source code

Documentation for the GameLogic Module.

There are only three importable modules in the game engine:

All the other modules are accessible through the methods in GameLogic.

See WhatsNew for updates, changes and new functionality in the Game Engine Python API.

Examples:
       # To get a controller:
       import GameLogic
       co = GameLogic.getCurrentController()
       
       # To get the game object associated with this controller:
       obj = co.getOwner()
KX_GameObject and KX_Camera or KX_Light methods are available depending on the type of object:
       # To get a sensor linked to this controller.
       # "sensorname" is the name of the sensor as defined in the Blender interface.
       # +---------------------+  +--------+
       # | Sensor "sensorname" +--+ Python +
       # +---------------------+  +--------+
       sens = co.getSensor("sensorname")

       # To get a list of all sensors:
       sensors = co.getSensors()
See the sensor's reference for available methods: You can also access actuators linked to the controller:
       # To get an actuator attached to the controller:
       #                          +--------+  +-------------------------+
       #                          + Python +--+ Actuator "actuatorname" |
       #                          +--------+  +-------------------------+
       actuator = co.getActuator("actuatorname")
       
       # Activate an actuator
       GameLogic.addActiveActuator(actuator, True)
See the actuator's reference for available methods:

Most logic brick's methods are accessors for the properties available in the logic buttons. Consult the logic bricks documentation for more information on how each logic brick works.

There are also methods to access the current KX_Scene:
       # Get the current scene
       scene = GameLogic.getCurrentScene()
       
       # Get the current camera
       cam = scene.active_camera
Matricies as used by the game engine are row major:
       matrix[row][col] = blah
KX_Camera has some examples using matricies.

Functions
SCA_PythonController
getCurrentController()
Gets the Python controller associated with this Python script.
source code
KX_Scene
getCurrentScene()
Gets the current Scene.
source code
 
addActiveActuator(actuator, activate)
Activates the given actuator.
source code
 
getRandomFloat()
Returns a random floating point value in the range [0...1)
source code
 
setGravity(gravity)
Sets the world gravity.
source code
list [float], len(getSpectrum()) == 512
getSpectrum()
Returns a 512 point list from the sound card.
source code
 
stopDSP()
Stops the sound driver using DSP effects.
source code
float
getLogicTicRate()
Gets the logic update frequency.
source code
 
setLogicTicRate(ticrate)
Sets the logic update frequency.
source code
float
getPhysicsTicRate()
Gets the physics update frequency
source code
 
setPhysicsTicRate(ticrate)
Sets the physics update frequency
source code
string
expandPath(path)
Converts a blender internal path into a proper file system path.
source code
Variables
    Constants
  KX_FALSE
False value used by some modules.
  KX_TRUE
True value used by some modules.
    Property Sensor
  KX_PROPSENSOR_CHANGED
Activate when the property changes
  KX_PROPSENSOR_EQUAL
Activate when the property is equal to the sensor value.
  KX_PROPSENSOR_EXPRESSION
Activate when the expression matches
  KX_PROPSENSOR_INTERVAL
Activate when the property is between the specified limits.
  KX_PROPSENSOR_NOTEQUAL
Activate when the property is not equal to the sensor value.
    Constraint Actuator
  KX_CONSTRAINTACT_LOCX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_LOCY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_LOCZ
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ROTX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ROTY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ROTZ
See KX_ConstraintActuator
    IPO Actuator
  KX_IPOACT_FLIPPER
See KX_IpoActuator
  KX_IPOACT_LOOPEND
See KX_IpoActuator
  KX_IPOACT_LOOPSTOP
See KX_IpoActuator
  KX_IPOACT_PINGPONG
See KX_IpoActuator
  KX_IPOACT_PLAY
See KX_IpoActuator
    Random Distributions
  KX_RANDOMACT_BOOL_BERNOUILLI
See SCA_RandomActuator
  KX_RANDOMACT_BOOL_CONST
See SCA_RandomActuator
  KX_RANDOMACT_BOOL_UNIFORM
See SCA_RandomActuator
  KX_RANDOMACT_FLOAT_CONST
See SCA_RandomActuator
  KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL
See SCA_RandomActuator
  KX_RANDOMACT_FLOAT_NORMAL
See SCA_RandomActuator
  KX_RANDOMACT_FLOAT_UNIFORM
See SCA_RandomActuator
  KX_RANDOMACT_INT_CONST
See SCA_RandomActuator
  KX_RANDOMACT_INT_POISSON
See SCA_RandomActuator
  KX_RANDOMACT_INT_UNIFORM
See SCA_RandomActuator
    Action Actuator
  KX_ACTIONACT_FLIPPER
See BL_ActionActuator
  KX_ACTIONACT_LOOPEND
See BL_ActionActuator
  KX_ACTIONACT_LOOPSTOP
See BL_ActionActuator
  KX_ACTIONACT_PLAY
See BL_ActionActuator
  KX_ACTIONACT_PROPERTY
See BL_ActionActuator
    Sound Actuator
  KX_SOUNDACT_LOOPBIDIRECTIONAL
See KX_SoundActuator
  KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP
See KX_SoundActuator
  KX_SOUNDACT_LOOPEND
See KX_SoundActuator
  KX_SOUNDACT_LOOPSTOP
See KX_SoundActuator
  KX_SOUNDACT_PLAYEND
See KX_SoundActuator
  KX_SOUNDACT_PLAYSTOP
See KX_SoundActuator
Function Details

addActiveActuator(actuator, activate)

source code 
Activates the given actuator.
Parameters:
  • activate (boolean) - whether to activate or deactivate the given actuator.

setGravity(gravity)

source code 
Sets the world gravity.

getSpectrum()

source code 
Returns a 512 point list from the sound card. This only works if the fmod sound driver is being used.
Returns: list [float], len(getSpectrum()) == 512

stopDSP()

source code 

Stops the sound driver using DSP effects.

Only the fmod sound driver supports this. DSP can be computationally expensive.

getLogicTicRate()

source code 
Gets the logic update frequency.
Returns: float
The logic frequency in Hz

setLogicTicRate(ticrate)

source code 

Sets the logic update frequency.

The logic update frequency is the number of times logic bricks are executed every second. The default is 30 Hz.
Parameters:
  • ticrate (float) - The new logic update frequency (in Hz).

getPhysicsTicRate()

source code 
Gets the physics update frequency
Returns: float
The physics update frequency in Hz

setPhysicsTicRate(ticrate)

source code 

Sets the physics update frequency

The physics update frequency is the number of times the physics system is executed every second. The default is 60 Hz.
Parameters:
  • ticrate (float) - The new update frequency (in Hz).

expandPath(path)

source code 

Converts a blender internal path into a proper file system path.

Use / as directory separator in path You can use '//' at the start of the string to define a relative path; Blender replaces that string by the directory of the startup .blend or runtime file to make a full path name (doesn't change during the game, even if you load other .blend). The function also converts the directory separator to the local file system format.
Parameters:
  • path (string) - The path string to be converted/expanded.
Returns: string
The converted string