Graphics Reference
In-Depth Information
Accessing Keyboard Information Directly in Python
The example here uses the sensor logic brick to set the keyboard key for the Keyboard sensor. It is also possible
to work with keyboard information directly in Python by using the GameKeys module. This is one of the three
modules that make up the BGE API, the other two being GameLogic and Rasterizer, which is discussed later in
this chapter. In the BGE API, you will find a list of the key codes for accessing individual keys with GameKeys.
Python Code for a Login Screen
As in the previous examples, the code begins by creating a shorthand for the GameLogic name. After that, the
Python hasattr() function iscalled toassess whether GameLogic hasanattribute called init .GameLogic
attributes are how values can be persisted and accessed throughout a game-play session. Upon the first iteration
of this script, GameLogic does not have any such attribute, so the code in the if clause will be executed. This
results in several initialization steps.
First,the showMouse() methodoftheRasterizermoduleisimportedandsetto 1 ,whichcausesthemouse
pointer to be visible within the game-play environment. Next, the GL.password and GL.name attributes are
created and given empty string values. These attributes will now persist throughout the game-play session. Fin-
ally, GL.init is created and given a value of None . This will prevent this code from being executed again,
because the hasattr() function will result in a True value the next time it is called.
from bge import logic as GL
if not hasattr(GL, "init"):
from Rasterizer import showMouse
showMouse(1)
GL.init = None
GL.password = ""
GL.name = ""
Once-off Execution and Persistent Variables in the BGE
BGE Python scripts are executed when the associated sensors are activated. Scripts associated to Always sensors
are executed over and over again in rapid succession. Of course, variable values do not generally persist beyond a
single execution of a normal Python script, which means that when scripting for the BGE, it is necessary to have
another way to ensure that values can be made to persist throughout an entire game session. The solution to this
problem is also the solution to the problem of how to execute a given piece of code only once, at the start of a
game-play session. The way to do this is to assign attributes to the logic object itself. You can create arbitrarily
named attributes for logic and assign them values, which will be accessible by any script run at any time during a
single game-play session. The syntax for this is logic.attributename = value . To execute a particular
section of code once only, use Python's hasattr() function to determine whether the logic object has a partic-
ular attribute associated with it. If hasattr() returns 0 ( False ), execute the once-off code, and then define
the attribute for GameLogic, so that the hasattr() function will not return 0 the next time it is called. An ex-
ample of this technique is the use of the init attribute in the login screen script in this chapter.
As in the previous examples, the current controller, its owner, and its associated sensors and actuators are
retrieved and sent to aptly named variables, which is done with the following code:
cont = GL.getCurrentController()
current_scene = GL.getCurrentScene()
Search WWH ::




Custom Search