Module to access logic functions, imported automatically into the python controllers namespace.
..module:: bge.logic
..code-block:: python
# To get the controller thats running this python script:
cont = bge.logic.getCurrentController() # bge.logic is automatically imported
# To get the game object this controller is on:
obj = cont.owner
: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
..code-block:: python
# 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:
*:class:`bge.types.SCA_DelaySensor`
*:class:`bge.types.SCA_JoystickSensor`
*:class:`bge.types.SCA_KeyboardSensor`
*:class:`bge.types.KX_MouseFocusSensor`
*:class:`bge.types.SCA_MouseSensor`
*:class:`bge.types.KX_NearSensor`
*:class:`bge.types.KX_NetworkMessageSensor`
*:class:`bge.types.SCA_PropertySensor`
*:class:`bge.types.KX_RadarSensor`
*:class:`bge.types.SCA_RandomSensor`
*:class:`bge.types.KX_RaySensor`
*:class:`bge.types.KX_TouchSensor`
You can also access actuators linked to the controller
..code-block:: python
# 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
*:class:`bge.types.SCA_2DFilterActuator`
*:class:`bge.types.BL_ActionActuator`
*:class:`bge.types.KX_SCA_AddObjectActuator`
*:class:`bge.types.KX_CameraActuator`
*:class:`bge.types.KX_ConstraintActuator`
*:class:`bge.types.KX_SCA_DynamicActuator`
*:class:`bge.types.KX_SCA_EndObjectActuator`
*:class:`bge.types.KX_GameActuator`
*:class:`bge.types.KX_IpoActuator`
*:class:`bge.types.KX_NetworkMessageActuator`
*:class:`bge.types.KX_ObjectActuator`
*:class:`bge.types.KX_ParentActuator`
*:class:`bge.types.SCA_PropertyActuator`
*:class:`bge.types.SCA_RandomActuator`
*:class:`bge.types.KX_SCA_ReplaceMeshActuator`
*:class:`bge.types.KX_SceneActuator`
*:class:`bge.types.BL_ShapeActionActuator`
*:class:`bge.types.KX_SoundActuator`
*:class:`bge.types.KX_StateActuator`
*:class:`bge.types.KX_TrackToActuator`
*:class:`bge.types.KX_VisibilityActuator`
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 :class:`bge.types.KX_Scene`
..code-block:: python
# Get the current scene
scene = bge.logic.getCurrentScene()
# Get the current camera
cam = scene.active_camera
Matricies as used by the game engine are **row major**
``matrix[row][col] = float``
:class:`bge.types.KX_Camera` has some examples using matricies.
..data:: 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.
..data:: keyboard: The current keyboard wrapped in an SCA_PythonKeyboard object.
..data:: mouse: The current mouse wrapped in an SCA_PythonMouse object.
..function:: getCurrentController()
Gets the Python controller associated with this Python script.
:rtype::class:`bge.types.SCA_PythonController`
..function:: getCurrentScene()
Gets the current Scene.
:rtype::class:`bge.types.KX_Scene`
..function:: getSceneList()
Gets a list of the current scenes loaded in the game engine.
:rtype:list of :class:`bge.types.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.