BGE Python scriptingEdit

Use of python scriptsEdit


When I write a script do I just select an object then paste the code for that object I wrote in the script or python window? See thread scripting


You write the script in the text window, then you put a sensor on the object and a python controller that links to the name of the script.


Show mouse cursorEdit


How to show the mouse in the GE? See thread mouse


Simply activate the default mouse cursor, you use this Python script:

import Rasterizer


Add new objectsEdit


How to add new objects in the GE? See thread How to create new objects with an Actuator


The "template" object must be placed on an hidden layer. It will be inactive when running the GE. From the template instance objects can be created by copying. There are multiple ways:

  1. By Edit Object actuator in Add Object mode. Add the name of the template object in the OB: field. The new object will get the position and orientation of the object that added it.
  2. By the Python function instantAddObject() in conjunction with the above Add Object actuator. See API.
  3. By the Python function KX_Scene.addObject(object, other, time=0). See API

Keep in mind:

  • The template object should be on an hidden layer.
  • The template object is inactive and will not execute any logic.
  • Actuators and sensors that have the template assigned as parameter during desing time will keep this reference at runtime. They will NOT reference to new instances!
  • The new objects are copies of the inactive template. The instances are active and execute their logic.