Module GameLogic
[hide private]
[frames] | no frames]

Module GameLogic

source code

Documentation for the GameLogic Module.

Module to access logic functions, imported automatically into the python controllers namespace.

Examples:
       # To get the controller thats running this python script:
       cont = GameLogic.getCurrentController() # GameLogic is automatically imported
       
       # To get the game object this controller is on:
       obj = cont.owner
KX_GameObject and KX_Camera or KX_LightObject 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 = cont.sensors["sensorname"]

       # To get a sequence of all sensors:
       sensors = co.sensors
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.actuators["actuatorname"]
       
       # Activate an actuator
       controller.activate(actuator)
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] = float
KX_Camera has some examples using matricies.

Functions [hide private]
SCA_PythonController
getCurrentController()
Gets the Python controller associated with this Python script.
source code
KX_Scene
getCurrentScene()
Gets the current Scene.
source code
list of KX_Scene
getSceneList()
Gets a list of the current scenes loaded in the game engine.
source code
 
sendMessage(subject, body='', to='', message_from='')
Sends a message to sensors in any active scene.
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
interger
getMaxLogicFrame()
Gets the maximum number of logic frame per render frame.
source code
 
setMaxLogicFrame(maxlogic)
Sets the maximum number of logic frame that are executed per render frame.
source code
interger
getMaxPhysicsFrame()
Gets the maximum number of physics frame per render frame.
source code
 
setMaxPhysicsFrame(maxphysics)
Sets the maximum number of physics timestep that are executed per render frame.
source code
float
getLogicTicRate()
Gets the logic update frequency.
source code
 
setLogicTicRate(ticrate)
Sets the logic update frequency.
source code
float
getPhysicsTicRate()
NOT IMPLEMENTED Gets the physics update frequency
source code
 
setPhysicsTicRate(ticrate)
NOT IMPLEMENTED Sets the physics update frequency
source code
    Utility functions
float
getAverageFrameRate()
Gets the estimated average framerate
source code
string
expandPath(path)
Converts a blender internal path into a proper file system path.
source code
list
getBlendFileList(path='//')
Returns a list of blend files in the same directory as the open blend file, or from using the option argument.
source code
 
PrintGLInfo()
Prints GL Extension Info into the console
source code
 
getRandomFloat()
Returns a random floating point value in the range [0...1)
source code
    Deprecated
 
addActiveActuator(actuator, activate)
Activates the given actuator.
source code
Variables [hide private]
  globalDict
A dictionary that is saved between loading blend files so you can use it to store inventory and other variables you want to store between scenes and blend files.
    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_DIRNX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_DIRNY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_DIRPX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_DIRPY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_LOCX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_LOCY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_LOCZ
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ORIX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ORIY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ORIZ
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ROTX
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ROTY
See KX_ConstraintActuator
  KX_CONSTRAINTACT_ROTZ
See KX_ConstraintActuator
  KX_ACT_CONSTRAINT_DISTANCE
  KX_ACT_CONSTRAINT_DOROTFH
  KX_ACT_CONSTRAINT_FHNX
  KX_ACT_CONSTRAINT_FHNY
  KX_ACT_CONSTRAINT_FHNZ
  KX_ACT_CONSTRAINT_FHPX
  KX_ACT_CONSTRAINT_FHPY
  KX_ACT_CONSTRAINT_FHPZ
  KX_ACT_CONSTRAINT_LOCAL
  KX_ACT_CONSTRAINT_MATERIAL
  KX_ACT_CONSTRAINT_NORMAL
  KX_ACT_CONSTRAINT_PERMANENT
    IPO Actuator
  KX_IPOACT_FLIPPER
See KX_IpoActuator
  KX_IPOACT_FROM_PROP
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
    Radar Sensor
  KX_RADAR_AXIS_NEG_X
See KX_RadarSensor
  KX_RADAR_AXIS_NEG_Y
See KX_RadarSensor
  KX_RADAR_AXIS_NEG_Z
See KX_RadarSensor
  KX_RADAR_AXIS_POS_X
See KX_RadarSensor
  KX_RADAR_AXIS_POS_Y
See KX_RadarSensor
  KX_RADAR_AXIS_POS_Z
See KX_RadarSensor
    Ray Sensor
  KX_RAY_AXIS_NEG_X
See KX_RaySensor
  KX_RAY_AXIS_NEG_Y
See KX_RaySensor
  KX_RAY_AXIS_NEG_Z
See KX_RaySensor
  KX_RAY_AXIS_POS_X
See KX_RaySensor
  KX_RAY_AXIS_POS_Y
See KX_RaySensor
  KX_RAY_AXIS_POS_Z
See KX_RaySensor
    Dynamic Actuator
  KX_DYN_DISABLE_DYNAMICS
See KX_SCA_DynamicActuator
  KX_DYN_DISABLE_RIGID_BODY
See KX_SCA_DynamicActuator
  KX_DYN_ENABLE_RIGID_BODY
See KX_SCA_DynamicActuator
  KX_DYN_RESTORE_DYNAMICS
See KX_SCA_DynamicActuator
  KX_DYN_SET_MASS
See KX_SCA_DynamicActuator
    Game Actuator
  KX_GAME_LOAD
See KX_GameActuator
  KX_GAME_LOADCFG
See KX_GameActuator
  KX_GAME_QUIT
See KX_GameActuator
  KX_GAME_RESTART
See KX_GameActuator
  KX_GAME_SAVECFG
See KX_GameActuator
  KX_GAME_START
See KX_GameActuator
    Scene Actuator
  KX_SCENE_ADD_BACK_SCENE
See KX_SceneActuator
  KX_SCENE_ADD_FRONT_SCENE
See KX_SceneActuator
  KX_SCENE_REMOVE_SCENE
See KX_SceneActuator
  KX_SCENE_RESTART
See KX_SceneActuator
  KX_SCENE_RESUME
See KX_SceneActuator
  KX_SCENE_SET_CAMERA
See KX_SceneActuator
  KX_SCENE_SET_SCENE
See KX_SceneActuator
  KX_SCENE_SUSPEND
See KX_SceneActuator
    Input Status
  KX_INPUT_ACTIVE
See SCA_MouseSensor
  KX_INPUT_JUST_ACTIVATED
See SCA_MouseSensor
  KX_INPUT_JUST_RELEASED
See SCA_MouseSensor
  KX_INPUT_NONE
See SCA_MouseSensor
    Mouse Buttons
  KX_MOUSE_BUT_LEFT
See SCA_MouseSensor
  KX_MOUSE_BUT_MIDDLE
See SCA_MouseSensor
  KX_MOUSE_BUT_RIGHT
See SCA_MouseSensor
    States
  KX_STATE1
  KX_STATE10
  KX_STATE11
  KX_STATE12
  KX_STATE13
  KX_STATE14
  KX_STATE15
  KX_STATE16
  KX_STATE17
  KX_STATE18
  KX_STATE19
  KX_STATE2
  KX_STATE20
  KX_STATE21
  KX_STATE22
  KX_STATE23
  KX_STATE24
  KX_STATE25
  KX_STATE26
  KX_STATE27
  KX_STATE28
  KX_STATE29
  KX_STATE3
  KX_STATE30
  KX_STATE4
  KX_STATE5
  KX_STATE6
  KX_STATE7
  KX_STATE8
  KX_STATE9
  KX_STATE_OP_CLR
  KX_STATE_OP_CPY
  KX_STATE_OP_NEG
  KX_STATE_OP_SET
    2D Filter
  RAS_2DFILTER_BLUR
  RAS_2DFILTER_CUSTOMFILTER
  RAS_2DFILTER_DILATION
  RAS_2DFILTER_DISABLED
  RAS_2DFILTER_ENABLED
  RAS_2DFILTER_EROSION
  RAS_2DFILTER_GRAYSCALE
  RAS_2DFILTER_INVERT
  RAS_2DFILTER_LAPLACIAN
  RAS_2DFILTER_MOTIONBLUR
  RAS_2DFILTER_NOFILTER
  RAS_2DFILTER_PREWITT
  RAS_2DFILTER_SEPIA
  RAS_2DFILTER_SHARPEN
  RAS_2DFILTER_SOBEL
    Parent Actuator
  KX_PARENT_REMOVE
  KX_PARENT_SET
    Shader
  CAM_POS
Current camera position
  CONSTANT_TIMER
User a timer for the uniform value.
  MODELMATRIX
  MODELMATRIX_INVERSE
  MODELMATRIX_INVERSETRANSPOSE
  MODELMATRIX_TRANSPOSE
  MODELVIEWMATRIX
  MODELVIEWMATRIX_INVERSE
  MODELVIEWMATRIX_INVERSETRANSPOSE
  MODELVIEWMATRIX_TRANSPOSE
  SHD_TANGENT
Not yet documented.
  VIEWMATRIX
  VIEWMATRIX_INVERSE
  VIEWMATRIX_INVERSETRANSPOSE
  VIEWMATRIX_TRANSPOSE
    Blender Material
  BL_DST_ALPHA
  BL_DST_COLOR
  BL_ONE
  BL_ONE_MINUS_DST_ALPHA
  BL_ONE_MINUS_DST_COLOR
  BL_ONE_MINUS_SRC_ALPHA
  BL_ONE_MINUS_SRC_COLOR
  BL_SRC_ALPHA
  BL_SRC_ALPHA_SATURATE
  BL_SRC_COLOR
  BL_ZERO
Function Details [hide private]

getSceneList()

source code 
Gets a list of the current scenes loaded in the game engine.
Returns: list of KX_Scene

Note: Scenes in your blend file that have not been converted wont be in this list. This list will only contain scenes such as overlays scenes.

addActiveActuator(actuator, activate)

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

sendMessage(subject, body='', to='', message_from='')

source code 
Sends a message to sensors in any active scene.
Parameters:
  • subject (string) - The subject of the message
  • body (string) - The body of the message (optional)
  • to (string) - The name of the object to send the message to (optional)
  • message_from (string) - The name of the object that the message is coming from (optional)

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.

getMaxLogicFrame()

source code 
Gets the maximum number of logic frame per render frame.
Returns: interger
The maximum number of logic frame per render frame

setMaxLogicFrame(maxlogic)

source code 
Sets the maximum number of logic frame that are executed per render frame. This does not affect the physic system that still runs at full frame rate.
Parameters:
  • maxlogic (integer) - The new maximum number of logic frame per render frame. Valid values: 1..5

getMaxPhysicsFrame()

source code 
Gets the maximum number of physics frame per render frame.
Returns: interger
The maximum number of physics frame per render frame

setMaxPhysicsFrame(maxphysics)

source code 
Sets the maximum number of physics timestep that are executed per render frame. Higher value allows physics to keep up with realtime even if graphics slows down the game. Physics timestep is fixed and equal to 1/tickrate (see setLogicTicRate) maxphysics/ticrate is the maximum delay of the renderer that physics can compensate.
Parameters:
  • maxphysics (integer) - The new maximum number of physics timestep per render frame. Valid values: 1..5.

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 60 Hz.
Parameters:
  • ticrate (float) - The new logic update frequency (in Hz).

getPhysicsTicRate()

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

setPhysicsTicRate(ticrate)

source code 

NOT IMPLEMENTED 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).

getAverageFrameRate()

source code 
Gets the estimated average framerate
Returns: float
The estimed average framerate in frames per second

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

getBlendFileList(path='//')

source code 
Returns a list of blend files in the same directory as the open blend file, or from using the option argument.
Parameters:
  • path (string) - Optional directory argument, will be expanded (like expandPath) into the full path.
Returns: list
A list of filenames, with no directory prefix

Variables Details [hide private]

globalDict

A dictionary that is saved between loading blend files so you can use it to store inventory and other variables you want to store between scenes and blend files. It can also be written to a file and loaded later on with the game load/save actuators. note: only python built in types such as int/string/bool/float/tuples/lists can be saved, GameObjects, Actuators etc will not work as expectred.