9 Module to access logic functions, imported automatically into the python controllers namespace.
13 .. code-block:: python
15 # To get the controller thats running this python script:
16 cont = bge.logic.getCurrentController() # bge.logic is automatically imported
18 # To get the game object this controller is on:
21 :class:`~bge.types.KX_GameObject` and :class:`~bge.types.KX_Camera` or :class:`~bge.types.KX_LightObject` methods are available depending on the type of object
23 .. code-block:: python
25 # To get a sensor linked to this controller.
26 # "sensorname" is the name of the sensor as defined in the Blender interface.
27 # +---------------------+ +--------+
28 # | Sensor "sensorname" +--+ Python +
29 # +---------------------+ +--------+
30 sens = cont.sensors["sensorname"]
32 # To get a sequence of all sensors:
35 See the sensor's reference for available methods:
40 * :class:`~bge.types.KX_MouseFocusSensor`
41 * :class:`~bge.types.KX_NearSensor`
42 * :class:`~bge.types.KX_NetworkMessageSensor`
43 * :class:`~bge.types.KX_RadarSensor`
44 * :class:`~bge.types.KX_RaySensor`
45 * :class:`~bge.types.KX_TouchSensor`
46 * :class:`~bge.types.SCA_DelaySensor`
47 * :class:`~bge.types.SCA_JoystickSensor`
48 * :class:`~bge.types.SCA_KeyboardSensor`
49 * :class:`~bge.types.SCA_MouseSensor`
50 * :class:`~bge.types.SCA_PropertySensor`
51 * :class:`~bge.types.SCA_RandomSensor`
53 You can also access actuators linked to the controller
55 .. code-block:: python
57 # To get an actuator attached to the controller:
58 # +--------+ +-------------------------+
59 # + Python +--+ Actuator "actuatorname" |
60 # +--------+ +-------------------------+
61 actuator = co.actuators["actuatorname"]
63 # Activate an actuator
64 controller.activate(actuator)
66 See the actuator's reference for available methods
71 * :class:`~bge.types.BL_ActionActuator`
72 * :class:`~bge.types.KX_CameraActuator`
73 * :class:`~bge.types.KX_ConstraintActuator`
74 * :class:`~bge.types.KX_GameActuator`
75 * :class:`~bge.types.KX_NetworkMessageActuator`
76 * :class:`~bge.types.KX_ObjectActuator`
77 * :class:`~bge.types.KX_ParentActuator`
78 * :class:`~bge.types.KX_SCA_AddObjectActuator`
79 * :class:`~bge.types.KX_SCA_DynamicActuator`
80 * :class:`~bge.types.KX_SCA_EndObjectActuator`
81 * :class:`~bge.types.KX_SCA_ReplaceMeshActuator`
82 * :class:`~bge.types.KX_SceneActuator`
83 * :class:`~bge.types.KX_SoundActuator`
84 * :class:`~bge.types.KX_StateActuator`
85 * :class:`~bge.types.KX_TrackToActuator`
86 * :class:`~bge.types.KX_VisibilityActuator`
87 * :class:`~bge.types.SCA_2DFilterActuator`
88 * :class:`~bge.types.SCA_PropertyActuator`
89 * :class:`~bge.types.SCA_RandomActuator`
91 Most logic brick's methods are accessors for the properties available in the logic buttons.
92 Consult the logic bricks documentation for more information on how each logic brick works.
94 There are also methods to access the current :class:`bge.types.KX_Scene`
96 .. code-block:: python
98 # Get the current scene
99 scene = bge.logic.getCurrentScene()
101 # Get the current camera
102 cam = scene.active_camera
104 Matricies as used by the game engine are **row major**
105 ``matrix[row][col] = float``
107 :class:`bge.types.KX_Camera` has some examples using matrices.
115 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.
116 It can also be written to a file and loaded later on with the game load/save actuators.
118 .. note:: only python built in types such as int/string/bool/float/tuples/lists can be saved, GameObjects, Actuators etc will not work as expected.
122 The current keyboard wrapped in an :class:`~bge.types.SCA_PythonKeyboard` object.
126 The current mouse wrapped in an :class:`~bge.types.SCA_PythonMouse` object.
132 .. function:: getCurrentController()
134 Gets the Python controller associated with this Python script.
136 :rtype: :class:`bge.types.SCA_PythonController`
138 .. function:: getCurrentScene()
140 Gets the current Scene.
142 :rtype: :class:`bge.types.KX_Scene`
144 .. function:: getSceneList()
146 Gets a list of the current scenes loaded in the game engine.
148 :rtype: list of :class:`bge.types.KX_Scene`
150 .. 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.
152 .. function:: loadGlobalDict()
154 Loads bge.logic.globalDict from a file.
156 .. function:: saveGlobalDict()
158 Saves bge.logic.globalDict to a file.
160 .. function:: startGame(blend)
162 Loads the blend file.
164 :arg blend: The name of the blend file
167 .. function:: endGame()
169 Ends the current game.
171 .. function:: restartGame()
173 Restarts the current game by reloading the .blend file (the last saved version, not what is currently running).
175 .. function:: LibLoad(blend, type, data, load_actions=False, verbose=False, load_scripts=True)
177 Converts the all of the datablocks of the given type from the given blend.
179 :arg blend: The path to the blend file (or the name to use for the library if data is supplied)
181 :arg type: The datablock type (currently only "Action", "Mesh" and "Scene" are supported)
183 :arg data: Binary data from a blend file (optional)
185 :arg load_actions: Search for and load all actions in a given Scene and not just the "active" actions (Scene type only)
186 :type load_actions: bool
187 :arg verbose: Whether or not to print debugging information (e.g., "SceneName: Scene")
189 :arg load_scripts: Whether or not to load text datablocks as well (can be disabled for some extra security)
190 :type load_scripts: bool
192 .. function:: LibNew(name, type, data)
194 Uses existing datablock data and loads in as a new library.
196 :arg name: A unique library name used for removal later
198 :arg type: The datablock type (currently only "Mesh" is supported)
200 :arg data: A list of names of the datablocks to load
201 :type data: list of strings
203 .. function:: LibFree(name)
205 Frees a library, removing all objects and meshes from the currently active scenes.
207 :arg name: The name of the library to free (the name used in LibNew)
210 .. function:: LibList()
212 Returns a list of currently loaded libraries.
216 .. function:: addScene(name, overlay=1)
218 Loads a scene into the game engine.
222 This function is not effective immediately, the scene is queued
223 and added on the next logic cycle where it will be available
226 :arg name: The name of the scene
228 :arg overlay: Overlay or underlay (optional)
229 :type overlay: integer
231 .. function:: sendMessage(subject, body="", to="", message_from="")
233 Sends a message to sensors in any active scene.
235 :arg subject: The subject of the message
236 :type subject: string
237 :arg body: The body of the message (optional)
239 :arg to: The name of the object to send the message to (optional)
241 :arg message_from: The name of the object that the message is coming from (optional)
242 :type message_from: string
244 .. function:: setGravity(gravity)
246 Sets the world gravity.
248 :type gravity: list [fx, fy, fz]
250 .. function:: getSpectrum()
252 Returns a 512 point list from the sound card.
253 This only works if the fmod sound driver is being used.
255 :rtype: list [float], len(getSpectrum()) == 512
257 .. function:: getMaxLogicFrame()
259 Gets the maximum number of logic frames per render frame.
261 :return: The maximum number of logic frames per render frame
264 .. function:: setMaxLogicFrame(maxlogic)
266 Sets the maximum number of logic frames that are executed per render frame.
267 This does not affect the physic system that still runs at full frame rate.
269 :arg maxlogic: The new maximum number of logic frames per render frame. Valid values: 1..5
270 :type maxlogic: integer
272 .. function:: getMaxPhysicsFrame()
274 Gets the maximum number of physics frames per render frame.
276 :return: The maximum number of physics frames per render frame
279 .. function:: setMaxPhysicsFrame(maxphysics)
281 Sets the maximum number of physics timestep that are executed per render frame.
282 Higher value allows physics to keep up with realtime even if graphics slows down the game.
283 Physics timestep is fixed and equal to 1/tickrate (see setLogicTicRate)
284 maxphysics/ticrate is the maximum delay of the renderer that physics can compensate.
286 :arg maxphysics: The new maximum number of physics timestep per render frame. Valid values: 1..5.
287 :type maxphysics: integer
289 .. function:: getLogicTicRate()
291 Gets the logic update frequency.
293 :return: The logic frequency in Hz
296 .. function:: setLogicTicRate(ticrate)
298 Sets the logic update frequency.
300 The logic update frequency is the number of times logic bricks are executed every second.
301 The default is 60 Hz.
303 :arg ticrate: The new logic update frequency (in Hz).
306 .. function:: getPhysicsTicRate()
308 Gets the physics update frequency
310 :return: The physics update frequency in Hz
313 .. warning: Not implimented yet
315 .. function:: setPhysicsTicRate(ticrate)
317 Sets the physics update frequency
319 The physics update frequency is the number of times the physics system is executed every second.
320 The default is 60 Hz.
322 :arg ticrate: The new update frequency (in Hz).
325 .. warning: Not implimented yet
327 .. function:: getExitKey()
329 Gets the key used to exit the game engine
331 :return: The key (defaults to :mod:`bge.events.ESCKEY`)
334 .. function:: setExitKey(key)
336 Sets the key used to exit the game engine
338 :arg key: A key constant from :mod:`bge.events`
346 .. function:: expandPath(path)
348 Converts a blender internal path into a proper file system path.
350 Use / as directory separator in path
351 You can use '//' at the start of the string to define a relative path;
352 Blender replaces that string by the directory of the startup .blend or runtime file
353 to make a full path name (doesn't change during the game, even if you load other .blend).
354 The function also converts the directory separator to the local file system format.
356 :arg path: The path string to be converted/expanded.
358 :return: The converted string
361 .. function:: getAverageFrameRate()
363 Gets the estimated/average framerate for all the active scenes, not only the current scene.
365 :return: The estimated average framerate in frames per second
368 .. function:: getBlendFileList(path = "//")
370 Returns a list of blend files in the same directory as the open blend file, or from using the option argument.
372 :arg path: Optional directory argument, will be expanded (like expandPath) into the full path.
374 :return: A list of filenames, with no directory prefix
377 .. function:: getRandomFloat()
379 Returns a random floating point value in the range [0 - 1)
381 .. function:: PrintGLInfo()
383 Prints GL Extension Info into the console
391 True value used by some modules.
395 False value used by some modules.
407 .. data:: KX_SENSOR_INACTIVE
408 .. data:: KX_SENSOR_JUST_ACTIVATED
409 .. data:: KX_SENSOR_ACTIVE
410 .. data:: KX_SENSOR_JUST_DEACTIVATED
412 .. _logic-property-sensor:
418 .. data:: KX_PROPSENSOR_EQUAL
420 Activate when the property is equal to the sensor value.
424 .. data:: KX_PROPSENSOR_NOTEQUAL
426 Activate when the property is not equal to the sensor value.
430 .. data:: KX_PROPSENSOR_INTERVAL
432 Activate when the property is between the specified limits.
436 .. data:: KX_PROPSENSOR_CHANGED
438 Activate when the property changes
442 .. data:: KX_PROPSENSOR_EXPRESSION
444 Activate when the expression matches
452 See :class:`bge.types.KX_RadarSensor`
454 .. data:: KX_RADAR_AXIS_POS_X
455 .. data:: KX_RADAR_AXIS_POS_Y
456 .. data:: KX_RADAR_AXIS_POS_Z
457 .. data:: KX_RADAR_AXIS_NEG_X
458 .. data:: KX_RADAR_AXIS_NEG_Y
459 .. data:: KX_RADAR_AXIS_NEG_Z
465 See :class:`bge.types.KX_RaySensor`
467 .. data:: KX_RAY_AXIS_POS_X
468 .. data:: KX_RAY_AXIS_POS_Y
469 .. data:: KX_RAY_AXIS_POS_Z
470 .. data:: KX_RAY_AXIS_NEG_X
471 .. data:: KX_RAY_AXIS_NEG_Y
472 .. data:: KX_RAY_AXIS_NEG_Z
485 See :class:`bge.types.BL_ActionActuator`
487 .. data:: KX_ACTIONACT_PLAY
488 .. data:: KX_ACTIONACT_PINGPONG
489 .. data:: KX_ACTIONACT_FLIPPER
490 .. data:: KX_ACTIONACT_LOOPSTOP
491 .. data:: KX_ACTIONACT_LOOPEND
492 .. data:: KX_ACTIONACT_PROPERTY
498 .. _constraint-actuator-option:
500 See :class:`bge.types.KX_ConstraintActuator.option`
502 * Applicable to Distance constraint:
504 .. data:: KX_CONSTRAINTACT_NORMAL
506 Activate alignment to surface
508 .. data:: KX_CONSTRAINTACT_DISTANCE
510 Activate distance control
512 .. data:: KX_CONSTRAINTACT_LOCAL
514 Direction of the ray is along the local axis
516 * Applicable to Force field constraint:
518 .. data:: KX_CONSTRAINTACT_DOROTFH
520 Force field act on rotation as well
522 * Applicable to both:
524 .. data:: KX_CONSTRAINTACT_MATERIAL
526 Detect material rather than property
528 .. data:: KX_CONSTRAINTACT_PERMANENT
530 No deactivation if ray does not hit target
532 .. _constraint-actuator-limit:
534 See :class:`bge.types.KX_ConstraintActuator.limit`
536 .. data:: KX_CONSTRAINTACT_LOCX
540 .. data:: KX_CONSTRAINTACT_LOCY
544 .. data:: KX_CONSTRAINTACT_LOCZ
548 .. data:: KX_CONSTRAINTACT_ROTX
552 .. data:: KX_CONSTRAINTACT_ROTY
556 .. data:: KX_CONSTRAINTACT_ROTZ
560 .. data:: KX_CONSTRAINTACT_DIRNX
562 Set distance along negative X axis
564 .. data:: KX_CONSTRAINTACT_DIRNY
566 Set distance along negative Y axis
568 .. data:: KX_CONSTRAINTACT_DIRNZ
570 Set distance along negative Z axis
572 .. data:: KX_CONSTRAINTACT_DIRPX
574 Set distance along positive X axis
576 .. data:: KX_CONSTRAINTACT_DIRPY
578 Set distance along positive Y axis
580 .. data:: KX_CONSTRAINTACT_DIRPZ
582 Set distance along positive Z axis
584 .. data:: KX_CONSTRAINTACT_ORIX
586 Set orientation of X axis
588 .. data:: KX_CONSTRAINTACT_ORIY
590 Set orientation of Y axis
592 .. data:: KX_CONSTRAINTACT_ORIZ
594 Set orientation of Z axis
596 .. data:: KX_CONSTRAINTACT_FHNX
598 Set force field along negative X axis
600 .. data:: KX_CONSTRAINTACT_FHNY
602 Set force field along negative Y axis
604 .. data:: KX_CONSTRAINTACT_FHNZ
606 Set force field along negative Z axis
608 .. data:: KX_CONSTRAINTACT_FHPX
610 Set force field along positive X axis
612 .. data:: KX_CONSTRAINTACT_FHPY
614 Set force field along positive Y axis
616 .. data:: KX_CONSTRAINTACT_FHPZ
618 Set force field along positive Z axis
624 See :class:`bge.types.KX_SCA_DynamicActuator`
626 .. data:: KX_DYN_RESTORE_DYNAMICS
627 .. data:: KX_DYN_DISABLE_DYNAMICS
628 .. data:: KX_DYN_ENABLE_RIGID_BODY
629 .. data:: KX_DYN_DISABLE_RIGID_BODY
630 .. data:: KX_DYN_SET_MASS
638 See :class:`bge.types.KX_GameActuator`
640 .. data:: KX_GAME_LOAD
641 .. data:: KX_GAME_START
642 .. data:: KX_GAME_RESTART
643 .. data:: KX_GAME_QUIT
644 .. data:: KX_GAME_SAVECFG
645 .. data:: KX_GAME_LOADCFG
651 .. data:: KX_PARENT_REMOVE
652 .. data:: KX_PARENT_SET
654 .. _logic-random-distributions:
660 See :class:`bge.types.SCA_RandomActuator`
662 .. data:: KX_RANDOMACT_BOOL_CONST
663 .. data:: KX_RANDOMACT_BOOL_UNIFORM
664 .. data:: KX_RANDOMACT_BOOL_BERNOUILLI
665 .. data:: KX_RANDOMACT_INT_CONST
666 .. data:: KX_RANDOMACT_INT_UNIFORM
667 .. data:: KX_RANDOMACT_INT_POISSON
668 .. data:: KX_RANDOMACT_FLOAT_CONST
669 .. data:: KX_RANDOMACT_FLOAT_UNIFORM
670 .. data:: KX_RANDOMACT_FLOAT_NORMAL
671 .. data:: KX_RANDOMACT_FLOAT_NEGATIVE_EXPONENTIAL
677 See :class:`bge.types.KX_SceneActuator`
679 .. data:: KX_SCENE_RESTART
680 .. data:: KX_SCENE_SET_SCENE
681 .. data:: KX_SCENE_SET_CAMERA
682 .. data:: KX_SCENE_ADD_FRONT_SCENE
683 .. data:: KX_SCENE_ADD_BACK_SCENE
684 .. data:: KX_SCENE_REMOVE_SCENE
685 .. data:: KX_SCENE_SUSPEND
686 .. data:: KX_SCENE_RESUME
688 .. _logic-sound-actuator:
694 See :class:`bge.types.KX_SoundActuator`
696 .. data:: KX_SOUNDACT_PLAYSTOP
700 .. data:: KX_SOUNDACT_PLAYEND
704 .. data:: KX_SOUNDACT_LOOPSTOP
708 .. data:: KX_SOUNDACT_LOOPEND
712 .. data:: KX_SOUNDACT_LOOPBIDIRECTIONAL
716 .. data:: KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP
731 See :class:`bge.types.SCA_PythonKeyboard`, :class:`bge.types.SCA_PythonMouse`, :class:`bge.types.SCA_MouseSensor`, :class:`bge.types.SCA_KeyboardSensor`
733 .. data:: KX_INPUT_NONE
734 .. data:: KX_INPUT_JUST_ACTIVATED
735 .. data:: KX_INPUT_ACTIVE
736 .. data:: KX_INPUT_JUST_RELEASED
742 See :class:`bge.types.SCA_MouseSensor`
744 .. data:: KX_MOUSE_BUT_LEFT
745 .. data:: KX_MOUSE_BUT_MIDDLE
746 .. data:: KX_MOUSE_BUT_RIGHT
752 See :class:`bge.types.KX_StateActuator`
785 .. _state-actuator-operation:
787 See :class:`bge.types.KX_StateActuator.operation`
789 .. data:: KX_STATE_OP_CLR
791 Substract bits to state mask
795 .. data:: KX_STATE_OP_CPY
801 .. data:: KX_STATE_OP_NEG
803 Invert bits to state mask
807 .. data:: KX_STATE_OP_SET
809 Add bits to state mask
813 .. _Two-D-FilterActuator-mode:
819 .. data:: RAS_2DFILTER_BLUR
823 .. data:: RAS_2DFILTER_CUSTOMFILTER
825 Customer filter, the code code is set via shaderText property.
829 .. data:: RAS_2DFILTER_DILATION
833 .. data:: RAS_2DFILTER_DISABLED
835 Disable the filter that is currently active
839 .. data:: RAS_2DFILTER_ENABLED
841 Enable the filter that was previously disabled
845 .. data:: RAS_2DFILTER_EROSION
849 .. data:: RAS_2DFILTER_GRAYSCALE
853 .. data:: RAS_2DFILTER_INVERT
857 .. data:: RAS_2DFILTER_LAPLACIAN
861 .. data:: RAS_2DFILTER_MOTIONBLUR
863 Create and enable preset filters
867 .. data:: RAS_2DFILTER_NOFILTER
869 Disable and destroy the filter that is currently active
873 .. data:: RAS_2DFILTER_PREWITT
877 .. data:: RAS_2DFILTER_SEPIA
881 .. data:: RAS_2DFILTER_SHARPEN
885 .. data:: RAS_2DFILTER_SOBEL
894 .. data:: VIEWMATRIX_INVERSE
895 .. data:: VIEWMATRIX_INVERSETRANSPOSE
896 .. data:: VIEWMATRIX_TRANSPOSE
897 .. data:: MODELMATRIX
898 .. data:: MODELMATRIX_INVERSE
899 .. data:: MODELMATRIX_INVERSETRANSPOSE
900 .. data:: MODELMATRIX_TRANSPOSE
901 .. data:: MODELVIEWMATRIX
902 .. data:: MODELVIEWMATRIX_INVERSE
903 .. data:: MODELVIEWMATRIX_INVERSETRANSPOSE
904 .. data:: MODELVIEWMATRIX_TRANSPOSE
907 Current camera position
909 .. data:: CONSTANT_TIMER
911 User a timer for the uniform value.
913 .. data:: SHD_TANGENT
919 .. data:: BL_DST_ALPHA
920 .. data:: BL_DST_COLOR
922 .. data:: BL_ONE_MINUS_DST_ALPHA
923 .. data:: BL_ONE_MINUS_DST_COLOR
924 .. data:: BL_ONE_MINUS_SRC_ALPHA
925 .. data:: BL_ONE_MINUS_SRC_COLOR
926 .. data:: BL_SRC_ALPHA
927 .. data:: BL_SRC_ALPHA_SATURATE
928 .. data:: BL_SRC_COLOR