Synchronize actions

from bge import logicfrom mathutils import Vector




sce = logic.getCurrentScene()
cont = logic.getCurrentController()
own = cont.owner




hlight1=sce.objects['highlightshine']
hlight2=sce.objects['highlightshine.001']
hlight3=sce.objects['highlightshine.002']
hlight4=sce.objects['highlightshine.003']




lightspeed = 1.97


thing = sce.objects['Charactertrack']
own.worldPosition = Vector((thing.worldPosition.x, thing.worldPosition.y, own.worldPosition.z))




if own['PLAY'] == True:
    


    own.playAction('highlightcol', 1, 241, layer=0, priority=0, blendin=0, play_mode=2, layer_weight=0.0, ipo_flags=0, speed = lightspeed)
    
    hlight1.playAction('highlightcol', 1, 241, layer=0, priority=0, blendin=0, play_mode=2, layer_weight=0.0, ipo_flags=0, speed = lightspeed)


    hlight2.playAction('highlightcol', 1, 241, layer=0, priority=0, blendin=0, play_mode=2, layer_weight=0.0, ipo_flags=0, speed = lightspeed)
    
    hlight3.playAction('highlightcol', 1, 241, layer=0, priority=0, blendin=0, play_mode=2, layer_weight=0.0, ipo_flags=0, speed = lightspeed)
    
    hlight4.playAction('highlightcol', 1, 241, layer=0, priority=0, blendin=0, play_mode=2, layer_weight=0.0, ipo_flags=0, speed = lightspeed)


    
elif own['PLAY'] == False:
    


    own.playAction('highlightcol', 0, 0, layer=0, priority=0, blendin=0, play_mode=1, layer_weight=0.0, ipo_flags=0, speed = 1)
    own['frame'] = own.getActionFrame(0)
    own.stopAction(0)


    



Hey there, as you can see in this script., i want to apply highlight 1 2 3 4 to have the to run action ‘highlightcol’ once prop 'Play" is true. I would like to ask if there is a way to rewrite these statements into another/ shorten form so that they can syncrhonize together better? kinda like… hlight1/2/3/4/.playAction … I have no idea please help!

Something like this?

from bge import logicfrom mathutils import Vector

sce = logic.getCurrentScene()
cont = logic.getCurrentController()
own = cont.owner

lightspeed = 1.97

#all things you want to affect, needs property hlight
for obj in sce.objects if ‘hlight’ in obj:

#if prop play is true, play the action
if obj['PLAY'] == True:
    
    obj.playAction('highlightcol', 1, 241, layer=0, priority=0, blendin=0, play_mode=2, layer_weight=0.0, ipo_flags=0, speed = lightspeed)

#if play is false,    
elif obj['PLAY'] == False:
    obj.playAction('highlightcol', 0, 0, layer=0, priority=0, blendin=0, play_mode=1, layer_weight=0.0, ipo_flags=0, speed = 1)


thanks cotax, i tried the script. it gives something like this. It doesnt run the animation yet

the first line needs to be the iterator,

for obj in own.scene.objects:

Then from there you can make checks and apply changes.

You can do a list comprehension to get all the right objects first, that one can be all one line, as long as it’s inside a set of square brackets.

color_objects = [ob for ob in own.scene.objects if ob.get("highlight")]

for obj in color_objects:
    obj.playAction()

You can also manually write a list and do the same:

color_objects = ["head","shoulders","knees","toes"]

for obj in color_objects:
    obj.playAction()

I often use this technique when I need to do a lot of the same code, like if I want to place footprints for left and right foot, rather than writing separate code for each foot, I make a list of two feet and iterate over it.

for obj in sce.objects if ‘hlight’ in obj:

change this into

for obj in sce.objects:

Now it will search for every object that has a property play in it.

you can change this line aswell for a double check to get the right items

if obj[‘PLAY’] == True:

into

if obj[‘PLAY’] == True AND ‘hilight’ in obj:

thought it was like this, dont forget to change the play = false aswell

thanks smoking mirror

i can only imagine how you build the logic but with my little knowledge of python, i cant really build these lines together, could you please write this for me to a comfortable form?

thanks cotax. hm i tried to rewrite the text. it looks now like this. Please fix it for me

There are 4 highlight lamps above the character and these should blink when PLAY is true. The round specular on the ground contains the script and will control the 4 lamps above it with (all of them will blink together)

I already put hilight as property for all these objects


I think i found a way to fix it! Thank you all

import GameLogicfrom mathutils import Vector

sce = GameLogic.getCurrentScene()
cont = GameLogic.getCurrentController()
own = cont.owner

lightspeed = 1.97

all things you want to affect, needs property hlight

if own[‘HL’] == 1:
for obj in sce.objects:

    if 'hilight' in obj:
        obj.playAction('highlightcol', 1, 241, layer = 0, priority = 0, blendin =
            0, play_mode = 2, layer_weight = 0.0, ipo_flags = 0, speed =
            lightspeed)

Don’t forget to indent the line that starts “if own[‘play’] == True…”

for blah in blah works like an if statement, in that it needs to be indented one level.

thanks smoking mirror, ya i thought about that too. I used this tool, its good to reformat the script
http://www.cleancss.com/python-beautify/