game engine templates (in trunk, edits welcome)

Hi, for yo-frankie release Id like to have templates so users can easy start their own scripts. These can be added from the text window (File->Script Templates->…)

Just committed these to blender now but would also be interested in feedback or additions.

Simple - for people who alredy know how to script in the bge

def main():
    cont = GameLogic.getCurrentController() 
    own = cont.getOwner() 
    sens = cont.getSensor('mySensor') 
    actu = cont.getActuator('myActuator') 
    if sens.isPositive():
        GameLogic.addActiveActuator(actu, True)
        GameLogic.addActiveActuator(actu, False)

Commented with examples

# 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 
            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) 
            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'): 
   = 0 

Why do you define all in a function (main())? I think it’s writing 2 lines for nothing.
Nice examples.

How do you add script templates?
Because I want one because alot of my scripts start off the same.

Why do you define all in a function (main())?

I guess this way it makes it neater, its a good idea, I might start doing it. Except with initialize .

wow. .very usefull…thanks for the post. Feed back… hmm.
Well scripts for showing people how to do the following would be nice :wink:
-move objects,
-rotate objects,
-Use global variables…
-use the new dict thing you guys made so you can copy info between blends/levels
-Showing how to change states.
-how to stop objects by removing their linear and angular momentum.
-mouse script…
-some kind of score setup that uses text…
-some kind of text entering script?
-how to do stuff to objects in other scenes?

Dont forget a save&load script, that is wanted by Blender users almost every month :).

What about creating breakable joints between objects at enough force, or break joints made by the constraint GUI.

@Cyborg… that was next on my list… heh heh…

@B3D00 - having code inside a function is useful when you want to do error checking an return when that functionality dosnt need to run.
@andrew-101, look in the scripts directory, its very easy. just copy any file starting with scripttimplate_ and add your own script and title.

@kirado, most of the things your suggesting are code examples. not really a template, further, most of these examples would not work with arbitrary objects, so I think this is better done by having example blend files.

It would be possible to have a script template also append in objects from a library that contain game logic but that would mean distributing blend files with script templates and thats probably not going to be accepted in svn.
could also apply the gamelogic python api patch and maintain it, scripts could make objects and link up logic bricks, but this is a fair bit of work too.

PS - for yo-frankie Im making example blendfiles that show some useful stuff like this, it wont be long!

thanks for the reply… either way the stuff you guys have made is awesome.

just copy any file starting with scripttimplate_ and add your own script and title.

Wow, that is pretty simple. I looked in the folder but I wasn’t looking for anything like that. Thanks, this is going to help.