[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16452] trunk/blender/release/scripts: 2 gamelogic templates, one with example functions and comments, another minimal template for those who know the api.

Campbell Barton ideasman42 at gmail.com
Wed Sep 10 05:34:09 CEST 2008


Revision: 16452
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16452
Author:   campbellbarton
Date:     2008-09-10 05:34:08 +0200 (Wed, 10 Sep 2008)

Log Message:
-----------
2 gamelogic templates, one with example functions and comments, another minimal template for those who know the api.

Added Paths:
-----------
    trunk/blender/release/scripts/scripttemplate_gamelogic.py
    trunk/blender/release/scripts/scripttemplate_gamelogic_basic.py

Added: trunk/blender/release/scripts/scripttemplate_gamelogic.py
===================================================================
--- trunk/blender/release/scripts/scripttemplate_gamelogic.py	                        (rev 0)
+++ trunk/blender/release/scripts/scripttemplate_gamelogic.py	2008-09-10 03:34:08 UTC (rev 16452)
@@ -0,0 +1,94 @@
+#!BPY
+"""
+Name: 'GameLogic Example'
+Blender: 245
+Group: 'ScriptTemplate'
+Tooltip: 'Script template with examples of how to use game logic'
+"""
+
+from Blender import Window
+import bpy
+
+script_data = \
+'''
+# GameLogic has been added to the global namespace no need to import
+
+# for keyboard event comparison
+# import GameKeys 
+
+# support for Vector(), Matrix() types and advanced functions like AngleBetweenVecs(v1,v2) and RotationMatrix(...)
+# import Mathutils 
+
+# for functions like getWindowWidth(), getWindowHeight()
+# import Rasterizer
+
+def main():
+	cont = GameLogic.getCurrentController()
+	
+	# The KX_GameObject that owns this controller.
+	own = cont.getOwner()
+	
+	# for scripts that deal with spacial logic
+	own_pos = own.getPosition() 
+	
+	
+	# Some example functions, remove to write your own script.
+	# check for a positive sensor, will run on any object without errors.
+	print 'Logic info for KX_GameObject', own.getName()
+	input = False
+	
+	for sens in cont.getSensors():
+		# The sensor can be on another object, we may want to use it
+		own_sens = sens.getOwner()
+		print '    sensor:', sens.getName(),
+		if sens.isPositive():
+			print '(true)'
+			input = True
+		else:
+			print '(false)'
+	
+	for actu in cont.getActuators():
+		# The actuator can be on another object, we may want to use it
+		own_actu = actu.getOwner()
+		print '    actuator:', sens.getName()
+		
+		# This runs the actuator or turns it off
+		# note that actuators will continue to run unless explicitly turned off.
+		if input:
+			GameLogic.addActiveActuator(actu, True)
+		else:
+			GameLogic.addActiveActuator(actu, False)
+	
+	# Its also good practice to get sensors and actuators by names
+	# so any changes to their order wont break the script.
+	
+	# sens_key = cont.getSensor('key_sensor')
+	# actu_motion = cont.getActuator('motion')
+	
+	
+	# Loop through all other objects in the scene
+	sce = GameLogic.getCurrentScene()
+	print 'Scene Objects:', sce.getName()
+	for ob in sce.getObjectList():
+		print '   ', ob.getName(), ob.getPosition()
+	
+	
+	# Example where collision objects are checked for their properties
+	# adding to our objects "life" property
+	"""
+	actu_collide = cont.getSensor('collision_sens')
+	for ob in actu_collide.getHitObjectList():
+		# Check to see the object has this property
+		if hasattr(ob, 'life'):
+			own.life += ob.life
+			ob.life = 0
+	print own.life
+	"""
+
+main()
+'''
+
+new_text = bpy.data.texts.new('gamelogic_example.py')
+new_text.write(script_data)
+bpy.data.texts.active = new_text
+Window.RedrawAll()

Added: trunk/blender/release/scripts/scripttemplate_gamelogic_basic.py
===================================================================
--- trunk/blender/release/scripts/scripttemplate_gamelogic_basic.py	                        (rev 0)
+++ trunk/blender/release/scripts/scripttemplate_gamelogic_basic.py	2008-09-10 03:34:08 UTC (rev 16452)
@@ -0,0 +1,33 @@
+#!BPY
+"""
+Name: 'GameLogic Template'
+Blender: 245
+Group: 'ScriptTemplate'
+Tooltip: 'Basic template for new game logic scripts'
+"""
+
+from Blender import Window
+import bpy
+
+script_data = \
+'''
+def main():
+
+	cont = GameLogic.getCurrentController()
+	own = cont.getOwner()
+	
+	sens = cont.getSensor('mySensor')
+	actu = cont.getActuator('myActuator')
+	
+	if sens.isPositive():
+		GameLogic.addActiveActuator(actu, True)
+	else:
+		GameLogic.addActiveActuator(actu, False)
+
+main()
+'''
+
+new_text = bpy.data.texts.new('gamelogic_example.py')
+new_text.write(script_data)
+bpy.data.texts.active = new_text
+Window.RedrawAll()





More information about the Bf-blender-cvs mailing list