Blender Game Engine Overview | Page 9

Blender org
for Python in the BGE can be found here.

Script Mode
In Script mode, the controller is linked to a script, the whole script will be executed before the logic
moves on. Everything in the script will happen at the same fr ame, if the same value or attribute are
changed more than once, only the latest will be visible in the g ame because of this. If for example the
position of an object is changed first to (100.0,100.0,100.0) but later i n the same script changed to
(0.0,0.0,0.0), then only the (0.0,0.0,0.0) will be visible to the player becaus e the move to
(100.0,100.0,100.0) happened on at the same frame.

Module Mode
Since Blender 2.49 the Python module controller was added. Simply choose Module instead of Script on
your Pythoncontroller drop-down menu. Then you define a function on that module and c all that function
from the controller. Instead of writing “myScript.py” on the script edit box, you’ll write
“myModule.myFunc” on the module edit box. This function will run every ti me the controller is called.
But other functions and variables outside of myFunc’s scope will only run once. This is good for
optimizing your code when you want to initiate variables only once then use t hem later.
The Python module controller supports any number of attributes, this means packages are supported
automatically. As well as “myModule.myFunc” you can do “myPackag e.myModule.myFunc”, nested
packages work too, as does method calls on class instances li ke:
“myPackage.myModule.myInstance.myMethod”. The python controller is passed to the python function
as an argument for functions that take one arg.
This allows live editing of scripts, learn more about the Python module controller at:
*Thread http://blenderartists.org/forum/showthread.php?t=156672
*Video http://download.blender.org/apricot/live_bge_edit.ogv



Game Expressions
A controller can evaluate an expression in order to send a positive or negative pulse to the connected
actuators:

 if the expression evaluates to True, the controller sends a positive pulse to the connected
actuators.
 if the expression evaluates to False, the controller sends a negative pulse to the connected
actuators.
Variables
You can use:
 sensors names ,
 properties : assign a game property to an object and use it in a controller exp ression.
These cannot contain blank spaces.
Operations
Mathematical operations
Operators: *, /, +, -
Returns: a number
Examples: 3 + 2, 35 / 5
Logical operations
 Comparison operators: <, >, >=, <=, ==, !=
 Booleans operators: AND, OR, NOT
Returns: True or False.
Examples: 3 > 2 (True), 1 AND 0 (False)
Conditional statement (if)
Use:
if( expression, pulse_if_expression_is_true, pulse_if_express ion_is_false )
If the controller evaluates expression to True:
 if pulse_if_expression_is_true is True, the controller sends a positive pulse to the connected
actuators.
 if pulse_if_expression_is_true is False, the controller sends a negative pulse to the connected
actuators.
If the controller evaluates expression to False:

 if pulse_if_expression_is_false is True, the controller sends a positive pulse to the connected
actuators.
 if pulse_if_expression_is_false is False, the controller sends a negative pulse to the connected
actuators.
Examples
Given the object has a property coins equal to 30:
coins > 20
returns True (the controller sends a positive pulse to the connected a ctuators).

Given the object has:
 a sensor called Key_Inserted equal to True,
 a property named Fuel equal to False,
Key_Inserted AND Fuel
returns False (the controller sends a negative pulse to the connect ed actuators).
Continue reading on your phone by scaning this QR Code

 / 23
Tip: The current page has been bookmarked automatically. If you wish to continue reading later, just open the Dertz Homepage, and click on the 'continue reading' link at the bottom of the page.