Graphics Reference
In-Depth Information
the name of this script now, however, you will find that Blender will unceremoniously delete your entry from
the field. You must enter the name of an existing text file in the text editor. So open a text editor window and
select Add New from the header menu. Name the new text file move.py . Now go back and enter this name in
the Script field on the Python controller.
After these preliminary steps have been completed and the necessary sensors and actuators have been
plugged into the script controller, it is time to turn to the code itself.
The GameLogic Module
The module that distinguishes Blender's in-game Python functionality is the GameLogic module. When a
Python script is executed from within the BGE, GameLogic is automatically imported. Most of the game-
engine-specific functionality you will need to use will be accessed via methods defined in this module. The
GameLogic module is distinct from the main Python-Blender API. The game engine modules' API document-
ation can be found here:
http://www.blender.org/documentation/blender_python_api_2_61_4/#game-engine-modules
It is also convenient to take advantage of the OOP paradigm and rely on Python's built in dir() command
to find out what functions are available for each object, as this section describes.
“Catchy” Class Names
As you browse the BGE Python API, you'll no doubt notice that the classes' names are all prefixed by the letters
KX and an underscore. This is a remnant of the history of the BGE. When it was first implemented, it was given a
catchy name—literally. The name given to the game engine at that time by its original creator, Erwin Coumans,
was Ketsji, pronounced catchy . Erwin used the KX in the class names as a way to indicate that the classes be-
longed to the Ketsji game engine. The name Ketsji has since fallen out of use, but the KX prefix remains on the
class names.
To set up the character controls, the first thing to do in the controller script is to import the logic module and
assign it to a shorthand variable called GL . This is not so important in simple cases like this one, but it's good
practice for longer, more-complicated scripts, because this module gets called a lot. So the script begins with
this line:
from bge import logic as GL
It's important to note that the BGE Python module is only visible while Blender is actively in Game
mode, when you hit P. Because of this, trying to test this module by typing import bge or
print(bge.__doc__) in Blender's Python Console will not give you any info; it will just give you errors.
Instead, one of this script's early goals will be to use the System Console for feedback.
The next step is to access the logic brick that the script is being called from and assign it to a variable. This
is done with the getCurrentController() method. This line is always required in BGE scripts that use
sensors or actuators. The logic brick returned by this method is passed to the variable cont in the line shown
here:
cont = GL.getCurrentController()
Every code block has an owner, which is the object to which it is associated. The object that is active when
youcreateanewcodeblockwillbethatblock'sowner.Inthiscase,becausethearmatureistheobjectthatisaf-
fectedbythelogic,thecodeblocksmustbeassociated withthearmature,asyousawpreviouslyin Figure16-1 .
So, how do you return the owner of a controller in the Python code? There are two ways to find the answer to
Search WWH ::




Custom Search