Hello everyone,

I was wondering how to chain actions and preserve order using scripting one after the other. For example:

  • Do some transformation (position/rotation) smoothly for 2 seconds, after that

  • Play some audio, once it has finished

  • Play some animation, once it has finished

  • Call a function

Now I don't want the details of transformation/audio-playing/animation, as I already know how to do each of them. However, I don't know how to timely and orderly chain them one after the other so that once the first one finish, the second one start, and once it has finished the third one start etc.

Thanks

You can use a call back method when another action is done to tell you when to fire the next step in the sequence. You can do this all in one script using boolean flags or you can separate out each task into its own game object and then you can place any combination of tasks into a game object array where they can be triggered in the correct order. You can even queue up the same object more than once if you want repeated actions.

e.g.

var ActionList : GameObject[];

function TaskDone(taskID : int){
   if(taskID < ActionList.length){
      // not reached the end of the list yet
      ActionList[taskID].SendMessage("NextTask",taskID+1);
   }
}

You initialise the sequence by calling TaskDone(0);

If all the tasks were set as children of the game controller object you can simply use SendMessageUpwards to send the complete message to the controller.

If one of the tasks were to play an audio it'd look like this:

private var ID : int;

function NextTask(returnID : int){
   ID = returnID;
   audio.Play();
}

function Update(){
   if(ID>0){ // has the task been started?
      if(!audio.isPlaying){ // is the audio done playing?
         // send the call back message to the controller
         gameObject.SendMessageUpwards("TaskDone",ID);
         ID = 0; // stop sending messages
      }
   }
}