Graphics Reference
In-Depth Information
Chapter 16
Python Power in the Blender Game Engine
As you saw in Chapter 14, “Creating Assets for the Blender Game Engine,” the Blender Game Engine (BGE)
is a powerful tool for 3D interactive content creation and visualization. The logic brick system enables artists to
quickly set up interactive logic without the need for programming knowledge. Nevertheless, the logic brick sys-
temhasitslimitations. Whenthelogicbecomes moderately complex, thestacks ofconnected blockscanbecome
difficult to read and debug. External data cannot be accessed by logic bricks, and many commands do not have
logic brick equivalents. Fortunately, Python can also be incorporated into your game engine creations, greatly
expanding the possibilities of what you can do.
In this chapter, you will learn to
• Replace multiple logic brick setups with a few lines of Python code
• Use Python to create effects in the BGE that would not be possible using logic bricks alone
• Create unique game environments with sound effects and multiple viewports
From Logic Bricks to Python
Blender'slogicbricksareagreatwaytodefinegamelogicinmanycases.Amongtheadvantagesofworkingwith
logicbricksarethattheyarehighlyaccessiblefornonprogrammers,theycontainnumerousbuilt-infunctionsthat
make logic bricks fast to work with even for programmers, and they are an entirely self-contained system in the
BGE.However,manycasesrequireamorepowerfulapproachtocreatinggamelogic.Alreadyyouhaveseenthat
when the logic becomes moderately complex, logic bricks quickly become unwieldy and difficult to read. There
are many functions that do not exist in logic bricks and need to be written specially. Finally, the self-contained
nature of logic bricks also represents a limitation. If you want to work with certain kinds of external resources in
your game, you have to turn to Python.
The BGE's Python interpreter enables you to create game logic by programming directly in Python. Although
a simple way to think of Python scripting in the BGE is as a replacement for logic bricks, it is more true to say
that it is an enhancement of the logic brick system. You still must use logic bricks, but with Python you can use
far fewer of them and stretch their functionality much further.
Controlling a Character with Python
A Python script in the BGE plays the role of the controller from the standpoint of logic bricks. However, rather
than using simple Boolean logical operations such as AND , OR , and XOR , a script controller can take an arbitrary
numberofsensorinputsandarriveattheappropriateactuatorstotriggerbasedoninstructionsinthePythoncode.
Search WWH ::




Custom Search