GameLogic Module (2.48)


Methods
addActiveActuator getRandomFloat
expandPath  getSpectrum
getAverageFrameRate globalDict
getBlendFileList setGravity
getCurrentController setLogicTicRate
getCurrentScene setPhysicsTicRate
getLogicTicRate stopDSP
getPhysicsTicRate ----------
 
Variables
---------- ----------
 
Constants
---------- ----------


Methods



addActiveActuator

addActiveActuator(actuator, activate)

Activates the chosen actuator

actuator:
   Type:  SCA_IActuator
(actuator linked to the controller logic brick)

activate:
   Type:  Bool

True or 1 = activate the actuator
False or 0 = deactivate the actuator

Sample Code

# get the current controller
controller = GameLogic.getCurrentController()

# get the actuator named Jump attached to the controller
jump = controller.getActuator("Jump")

# Use the actuator
GameLogic.addActiveActuator(jump, True)

expandPath

expandPath(include)

Expands the path to include the directory of the blend that was opened.

include:
Type:  string
  
"//" = expands path to include the directory of the blend opened.

Note:
You can include subdirectories by using ("//" + "subdir_name\\").

Sample Code

# get the path to the blend directory
mainDir = GameLogic.expandPath("//")

# get path to subdirectory named Sound
soundDirectory = mainDir + "Sound\\"

# get path to scream.wav
screamPath = (soundDirectory + "scream.wav")

getAverageFrameRate

getAverageFrameRate()

Returns the estimated frames per second

Return type:   float

Sample Code

# get the FPS
fps = GameLogic.getAverageFrameRate()

getBlendFileList

getBlendFileList(dirPath)

Returns a list of the blend files in a directory.

Return type:  List [ string ]

dirPath:
Type:  string

Note:
Works the same as expandPath.

Note:  
Leaving dirPath blank -- ie. GameLogic.getBlendFileList() -- returns a list of the blend files in the same directory as the open blend.

Sample Code

# get a list of the blends in the same directory as open blend
blendList = GameLogic.getBlendFileList("//")

# get a list of the blends in subDirectory Data
dataBlendList = GameLogic.getBlendFileList("//" + "\\Data")

# get list of the blends in C:\MyGames\Blender
gameBlends = GameLogic.getBlendFileList("C:\\MyGames\\Blender")

getCurrentController

getCurrentController()

Returns the controller logic brick that this python script is attached to.

Return type:
SCA_PythonController

Sample Code

# get the controller
controller = GameLogic.getCurrentController()

getCurrentScene

getCurrentScene()

Returns the current scene

Return type:
KX_Scene

Sample Code

# get current scene
scene = GameLogic.getCurrentScene()

getLogicTicRate

getLogicTicRate()

Returns how many times per second the logic bricks sensors are being checked/fired. 

The default is 60 times per second (60 Hz)

Return type:
float number

Sample Code

# get the logic tic rate
ticRate = GameLogic.getLogicTicRate()

getPhysicsTicRate

getPhysicsTicRate()

Returns how many times per second the scene's physics are being updated. 

The default is 60 times per second (60 Hz)

Return type:  float 

Note:
Always returns 0.0

Sample Code

# get the physics tic rate
ticRate = GameLogic.getPhysicsTicRate()

getRandomFloat

getRandomFloat()

Returns a random float number between 0.0 and 1.0

Return type:  float

Sample Code

# get a random number to use as a seed
seed = GameLogic.getRandomFloat()

getSpectrum

getSpectrum()

Returns a 512 point list from the sound card

This is from the official documentation

Returns: list[float]

len(getSpectrum()) == 512

Note:
This only works if the fmod sound driver is being used.

Sample Code

None

globalDict

globalDict

Stores/saves the name and values of variables so they can be passed between scenes.

Note:
This is the Blender version of a python dictionary.  

Note:
A dictionary is a python data structure.  Different types (float, integer, string, dictionary, etc) of data can be stored in the same dictionary.

Sample Code

#create a dictionary for player 1 stats
player1_Stats = {  "Health" : 80,
"Armor" : 30,
"Class" : "Cleric"  }
               
#create a dictionary for player 2 stats
player2_Stats = {  "Health" : 60,
"Armor" : 90,
"Class" : "Warrior"  }

# save player 1 and player 2 dictionarys to the globalDict
GameLogic.globalDict["player1"] = player1_Stats
GameLogic.globalDict["player2"] = player2_Stats

# get player 1 stats from the globalDict
player1_stats = GameLogic.globalDict.get("player1")

# get player 2 stats from the globalDict
player2_stats = GameLogic.globalDict.get("player2")

setGravity

setGravity(gravity)

Sets the world gravity using the world x, y and z axis.

gravity:
Type:  list [ x, y, z]

example: gravity = [ 0.0, 0.0, -10.0]

Note:
KX_PyConstraintBinding  also has a setGravity function.

Sample Code

# set the gravity to 10
GameLogic.setGravity([ 0.0, 0.0, -10.0])


setLogicTicRate

setLogicTicRate(ticRate)

Sets how many times per second the logic brick sensors are checked/fired. 

The default is 60 times per second (60 Hz)

ticRate:
Type:  float number

Sample Code

# set the logic tic rate to 100
GameLogic.setLogicTicRate(100.0)

setPhysicsTicRate

setPhysicsTicRate(ticRate)

Sets how many times per second the scene's physics are being updated. 

The default is 60 times per second (60 Hz)

ticRate:
Type:  float number

Note:
Using this function doesn't seem to change anything.

Sample Code

# set the physics tic rate to 120
GameLogic.setPhysicsTicRate(120.0)

stopDSP

stopDSP()

Stops the sound driver using DSP effects

Note:
This only works if the fmod sound driver is being used.

Sample Code

None



Blender 3D
Game Engine