PlayAnimationSet ( entity,clip$[,restart] )

Parameters

entity - a model from CreateAnimationSet, or one set up with ApplyAnimationSet

clip$ - the clip's name in the set, such as "walk"; names are case-sensitive

restart (optional) - True to start the clip from the beginning even if it is already playing; False to leave a clip that is already playing alone (default)

Description

Plays an animation set's clip by name, cross-fading from the current one.

The set saves each clip's mode, speed and blend time, so your game can say PlayAnimationSet player,"run" and get the run the way the animator set it up, instead of repeating Animate's numbers all through the code. The clip's events arrive through PendingAnimEvents and PollAnimEvent.

Calling it every frame with the same clip is fine: a clip that is already playing is left alone unless restart is True. As with Animate and CrossFadeAnim, speed and blend time count UpdateWorld steps, not seconds.

Returns True, or False when the model has no animation set or the set has no clip with that name; AnimationSetError() says why, and playback carries on unchanged. A copy made with CopyEntity keeps the set's settings and events, with its own playback.

For the full story - clip modes, speeds, events and root motion - see the Animation sets guide and its complete AnimationSets.bb sample.


See also: CreateAnimationSet, ApplyAnimationSet, AnimationSetError, CrossFadeAnim, PollAnimEvent.

Example

; CreateAnimationSet Example
; --------------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,2,-7

light=CreateLight()
RotateEntity light,45,45,0

; The robot's animation set: clip names, speeds, blends and step events
document=LoadResourceDocument("../../../samples/animation-sets/robot.animset")
If document=0 Then RuntimeError ResourceDocumentError()
library=LoadResourceDocumentAsync(document)
FreeResourceDocument document
While Not WorldReady(library)
    If WorldLoadState(library)=WORLD_LOAD_FAILED Then RuntimeError WorldLoadError(library)
    UpdateAsyncLoads 2
Wend
asset=FindWorldAsset(library,"resource")

; A robot made from the set, ready to play its clips by name
robot=CreateAnimationSet(asset)
If robot=0 Then RuntimeError AnimationSetError()
PositionEntity robot,-1.5,0,0

; An ordinary animated model, given the same settings
twin=LoadAnimMesh("../../../samples/animation-sets/robot.gltf")
If Not ApplyAnimationSet(twin,asset) Then RuntimeError AnimationSetError()
PositionEntity twin,1.5,0,0

; Both robots keep their settings when the loader world goes
FreeWorld library

PlayAnimationSet robot,"walk"
PlayAnimationSet twin,"walk"
report$="PlayAnimationSet robot,"+Chr(34)+"walk"+Chr(34)+" returned True"
problem$=""
step_event$=""

While Not KeyDown(1)

    ; 1 / 2 cross-fade both robots, 3 asks for a clip the set lacks
    clip$=""
    If KeyHit(2) Then clip="walk"
    If KeyHit(3) Then clip="run"
    If KeyHit(4) Then clip="fly"
    If clip<>"" Then
        If PlayAnimationSet(robot,clip) Then
            PlayAnimationSet twin,clip
            report="PlayAnimationSet robot,"+Chr(34)+clip+Chr(34)+" returned True"
            problem=""
        Else
            report="PlayAnimationSet robot,"+Chr(34)+clip+Chr(34)+" returned False"
            problem=AnimationSetError()
        EndIf
    EndIf

    ; The set's step events arrive like any animation event
    While PendingAnimEvents(robot)>0
        step_event=PollAnimEvent(robot)
    Wend

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    UpdateWorld
    RenderWorld

    Text 0,0,"Arrow keys: move camera   1 / 2: walk / run   3: unknown clip   Esc: exit"
    Text 0,20,"Left: CreateAnimationSet   Right: LoadAnimMesh + ApplyAnimationSet"
    Text 0,40,report
    Text 0,60,problem
    Text 0,80,"Latest step event: "+step_event

    Flip

Wend

End

Index