Play all animations in sequence

Sorry for the delay!

In the new system, there’s no “animation that contains all other animations” as it didn’t seem very useful.

However you can implement a feature “play all available animations in a sequence” still, like this:

  1. Get all possible animation names by Scene.AnimationsList. It is just a TStringList with all animation names.

  2. Use the TPlayAnimationParameters.StopNotification mechanism to play one animation, and when it ends play the next one, and when it ends play the next again etc.

See my post Sprite control with the new system - #2 by michalis where I mention this.

See examples/animations/play_animation/code/gamestatemain.pas for an example usage of PlayAnimation with TPlayAnimationParameters.

In your case, it would look like this (warning: untested quick draft code):

type
  TSomeClass = class // place this in any class you have handy
  private
    AnimList: TStringList;
    NextAnim: Integer;
    procedure RunNextAnimation(const Scene: TCastleSceneCore; const Animation: TTimeSensorNode);
  public
    procedure RunAllAnimations(const Scene: TCastleSceneCore);
  end;
     
procedure TSomeClass.RunAllAnimations(const Scene: TCastleSceneCore);
begin
  AnimList := Scene.AnimationsList;
  NextAnim := 0;
  RunNextAnimation(Scene, nil); // our RunNextAnimation implementation ignores 2nd param, ok to pass nil
end;

procedure TSomeClass.RunNextAnimation(const Scene: TCastleSceneCore; const Animation: TTimeSensorNode);
var
  NextAnimationName: String;
  Params: TPlayAnimationParameters;
begin
  NextAnimationName := AnimList[NextAnim];
  NextAnim := (NextAnim + 1) mod AnimList.Count;

  // play NextAnimationName, such that RunNextAnimation will be called again when it ends
  Params := TPlayAnimationParameters.Create;
  try
    Params.Name := NextAnimationName;
    Params.StopNotification := @RunNextAnimation;
    Scene.PlayAnimation(Params);
  finally FreeAndNil(Params) end;
end;

You may want to add here some “cancel” condition to TSomeClass.RunNextAnimation, otherwise it will just go on forever, until TCastleScene instance is finished.

1 Like