Doc:/Tutorials/Game Engine/ScriptMode vs ModuleMode

提供: wiki
移動先: 案内検索

This page describes the differences between the python controller's modes.

Script Mode Module Mode
  • Uses Python code from any text block in the Blender text editor.
  • Uses Python code from an external file or if not present from the text block in the Blender text editor.
  • The text block can have any name
  • The file/textblock name has to end with ".py".
  • The code gets interpreted within each trigger of the controller.
  • The code gets compiled within the first access to the module. This provides a little better performance.
  • The code is recreated with each access. Therefore it is not possible to store any data that survives the code execution. Data needs to be stored at other elements e.g. game objects or other modules.
  • The code is created once. The module can store module specific attribute. They survive until the module is unloaded (usually at the game's end).
  • The main level code (indentation 0) gets executed each time the Python controller gets triggered.
  • The main level code (indentation 0) gets executed the first time the module is accessed. Which makes this code a perfect candidate for any module specific initialization.
  • One entry point only: the first statement of the Python code.
  • Multiple entry points: Any BGE callable function (none or one argument)
  • The code has to deal with all possible situations at the main level. These easily leads to unnecessary complex code and unnecessary dependencies.
  • The main code needs to deal with module initialization only. Each entry point can deal with a separate situation. These provides clean architecture and supports re-usability.
if not initialized():
  initialize()

def doA(controller):
  ...
def doB(controller):
  ...

if situation==a:
  doA()
elif situation==b:
  doB()
# module initialization
...
def doA(controller):
  ...
def doB(controller):
  ...
  • The access to the current controller must be handled via a separate API call.
  • The access to the current controller is implicit provided as it is the one and only parameter to a BGE callable function.
controller = GameLogic.getCurrentController()
gameObject = controller.owner
def myFunction(controller):
  gameObject = controller.owner
  • Script functions or script data can not not be called by other scripts/modules. This means functions or data of a script can't be shared with other scripts.
  • Modules can be imported by other scripts and modules. The public attributes of a module can be accessed by other scripts and modules.
  • New requirements require a new script. A lot of scripts are hard to maintain.
  • New requirements require a new entry point in a new or existing module. A lot of entry points can be collected in a few modules.
  • Text blocks referred by a Python controller in script mode are linked together with the game object within groups and links.
  • Python controllers in module mode do not refer to the text block with the python module. If you link the game object they text block will not be linked. In case of external file the Python search path must match.
  • The code can be changed on the fly by assigning new python code to the attribute "script". The code must be complete!
  • The entry point to a module can be changed by assigning a new value to the attribute "script".
  • The module can be updated when the controller is in debug mode and the external file changes (For performance reasons this should be restricted to debugging purposes only.).