Help .. Playing Multiple Action using script

hello I need help about playing multiple action in for loop, I want to play multiple action to the armature using script,for example I play the action run and after the action finished the next one will automatically play after that the next one again and so on until no more action is to be played…heres my script when I use this it only plays the last one, how to play it from the very first to last,i’m thinking about timer or thread but I don’t know how to do it I’m not good in python,please help thank you very much :slight_smile:

import bge

co = bge.logic.getCurrentController()
own = co.owner

sensor = co.sensors[‘Keyboard1’]

string = own[‘Text’]
lenString = len(string)

for s in string:
if s.isdigit():
action = “num” + s
print (“d-” + action)
else:
action = “alpha” + s.upper()
print (“nd-” + action)

#what to add here??  
own.playAction(action, 0, 9, layer=0, play_mode=bge.logic.KX_ACTION_MODE_PLAY )

Check out the method: own.isPlayingAction(layer=0).
http://www.blender.org/documentation/blender_python_api_2_62_release/bge.types.html?highlight=isplayingaction#bge.types.KX_GameObject.isPlayingAction

What you have to do is maintain some state in your object about what you are currently doing and what you should do next.

I’ve attached a sample .blend made in 2.62. In the game engine, if you hit the up-arrow then it executes actions ‘num1’, ‘num2’, ‘num1’, ‘num2’. If you hit the down-arrow it executes animations ‘num1’, ‘num2’.

It does this by remember what animation it is currently playing. When the controller runs it checks to see if there are still animations to run and if the current animation has finished, then it starts the next animation.

You start animation by setting ‘currentAnimation’ to be somewhere in the range [0, len(string)-1] and the animations will execute from where you set it. The up-arrow just starts from index 0 and the down-arrow starts from index 2.

The relevant script is:


import bge




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner


    string = own['Text']
    
    # See if there are a list of actions that should be executed.
    if len(string) > 0 and own['currentAction'] >= 0:


        # See if the next action should be executed
        if own.isPlayingAction(0) == False:


            # The previous Action has finished, see if we should play the next one or if we are out of actions
            
            if own['currentAction'] + 1 > len(string):


                # The last action has finished.  Stop the animations
                own['currentAction'] = -1
                
            else:


                # Play the next action in the sequence.
                action = getActionName(string[own['currentAction']])
                
                own['currentAction'] += 1
                
                own.playAction(action, 1, 59, layer=0, play_mode=bge.logic.KX_ACTION_MODE_PLAY )
           


def getActionName(s):
    """Based on the digit provided, creates an action name."""
    
    action = ""
    
    if s.isdigit():
        action = "num" + s
        print ("d-" + action)
    else: 
        action = "alpha" + s.upper()
        print ("nd-" + action)


    return action




main()

Attachments

actions.blend (409 KB)

1 Like

Problem solved, thank you sir Kastoria, :salute: