Compound Actions

What Are Compound Actions?

This is where PRAction really shines. Creating a single action and having an object play it is fine for something simple. But when you need an object to perform several actions in concert, akin to a scripted action sequence in a movie, then you'll want to create compound actions. A compound action consists of multiple actions enclosed in up to 3 type of containers: groups, sequences and repeaters.

An action group is a collection of actions that are executed simultaneously. An action group is considered fully played out when each of the actions within the group has played out. To create a grouped action, you can use the PRActionCreateGroup() or PRActionCreateGroupFromArray() function. Here is an example of creating a grouped action:

// Create a typical action for a floating spinning coin.
var spinningCoinAction = PRActionCreateGroup(floatUpAndDownAction, xFlipAction)

The above example groups two other actions together into a compound action that, when played by an object, will play both contained actions simultaneously.

An action sequence is a collection of actions that are executed one after the other. The next action in a sequence will only begin playing once the previous action has completed. As with action groups, a sequence is considered fully played out when each of the actions within the sequence has played out. To create a sequence action, you can use the PRActionCreateSequence() or PRActionCreateSequenceFromArray() functions. Here is an example of creating a sequence action:

// Create a typical square patrol pattern for enemy soldier.
var enemyPatrolSeq = PRActionCreateSequence(moveNorth, moveEast, moveSouth, moveWest)

The above example creates an action sequence consisting of 4 actions that are played out one after the other, when played by an object.

A repeater action is a collection of actions that play out as normal, but when they have finished playing, they go back and play again and again the number of time sepecified. They can be repeated a fixed number of times, or they can be made to repeat indefinitely. A repeater action is considered fully played out once all iterations have played out. Be careful when constructing compound actions with repeaters that repeat forever. These never finish playing and can only be stopped with a call to PRStopAction() or PRStopAllActions(). To create a repeater action, you can use the PRActionCreateRepeat() or PRActionCreateRepeatForever() functions. Here is an example of creating a repeater action:

// Create an action that flashes the object forever.
var flashForever = PRActionCreateRepeatForever(flashingAction)

Note that each of these 3 container action types can be nested within each other as deep as you need. That means, for example, that you can define a repeater action that consists of a sequence action that contains several groups of actions within it. Some pretty complex scenes can be created using compound actions.

Another action type that is very useful in compound actions is the "wait" action. This action will simply do nothing for a given length of time before proceeding to the next action in the sequence. Can be used to add dramatic effect to your animations. Use the PRActionCreateWait() function to create this type of action. For example:

// Create a left/right enemy patrol pattern.
var waitAction = PRActionCreateWait(3) // Wait 3 seconds action.
var enemyPatternSeq = PRActionCreateSequence(moveLeft, waitAction, moveRight, waitAction)
var repeatEnemyPattern = PRActionCreateRepeatForever(enemyPatternSeq)

One thing to keep in mind when creating compound actions, is that you can nest the function calls into one to avoid creating variables you don't need. The above example can be re-written like so:

// Nesting action creation functions together.
var repeatEnemyPattern = PRActionCreateRepeatForever(
    
PRActionCreateSequence(
        
moveLeft,
        
PRActionCreateWait(3),
        
moveRight,
        
PRActionCreateWait(3)
    )
)

This module contains several pre-built compound action creation functions ready to use in your projects. You may use them as is or you can examine their code for tips on how to build your own. These functions are located in the cPRActionCompoundActions script asset.


© 2021 Prismatic Realms, Inc. Contact

Green Hosting Badge