3
Name: 'GameLogic Example'
5
Group: 'ScriptTemplate'
6
Tooltip: 'Script template with examples of how to use game logic'
9
from Blender import Window
14
# This script must be assigned to a python controller
15
# where it can access the object that owns it and the sensors/actuators that it connects to.
17
# GameLogic has been added to the global namespace no need to import
19
# for keyboard event comparison
22
# support for Vector(), Matrix() types and advanced functions like AngleBetweenVecs(v1,v2) and RotationMatrix(...)
25
# for functions like getWindowWidth(), getWindowHeight()
29
cont = GameLogic.getCurrentController()
31
# The KX_GameObject that owns this controller.
34
# for scripts that deal with spacial logic
35
own_pos = own.worldPosition
38
# Some example functions, remove to write your own script.
39
# check for a positive sensor, will run on any object without errors.
40
print 'Logic info for KX_GameObject', own.name
43
for sens in cont.sensors:
44
# The sensor can be on another object, we may want to use it
46
print ' sensor:', sens.name,
53
for actu in cont.actuators:
54
# The actuator can be on another object, we may want to use it
56
print ' actuator:', actu.name
58
# This runs the actuator or turns it off
59
# note that actuators will continue to run unless explicitly turned off.
65
# Its also good practice to get sensors and actuators by name
66
# rather then index so any changes to their order wont break the script.
68
# sens_key = cont.sensors['key_sensor']
69
# actu_motion = cont.actuators['motion']
72
# Loop through all other objects in the scene
73
sce = GameLogic.getCurrentScene()
74
print 'Scene Objects:', sce.name
75
for ob in sce.objects:
76
print ' ', ob.name, ob.worldPosition
79
# Example where collision objects are checked for their properties
80
# adding to our objects "life" property
82
actu_collide = cont.sensors['collision_sens']
83
for ob in actu_collide.objectHitList:
84
# Check to see the object has this property
85
if ob.has_key('life'):
86
own['life'] += ob['life']
94
new_text = bpy.data.texts.new('gamelogic_example.py')
95
new_text.write(script_data)
96
bpy.data.texts.active = new_text