x


Chaining actions over time and in order

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

more ▼

asked Aug 21 '10 at 05:33 AM

airt gravatar image

airt
14 2 2 3

Upvoted because I think this is a great question, but not certain of an answer. I'll have a think/experiment. I don't think Unity natively supports events/callbacks, or perhaps I just haven't come across them yet.

Aug 21 '10 at 11:36 AM Marowi

You are right, I am looking for something similar to callbacks/events. But I believe no such thing is possible currently, this is why I want to see what people might suggest instead.

Aug 21 '10 at 06:51 PM airt
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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
      }
   }
}
more ▼

answered Aug 21 '10 at 01:39 PM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Hmmm... I think you overcomplicated things. I only wanted an easy way to do things in sequence. What you have done can be done using a while loop for each action where you check if the action finished or not instead of using multiple GameObjects and messaging. I was seeking for a way to do it without continous checking especially when I want to do lots of chained actions. I really appreciate your examples, but I will see if I can have a better solution.

Aug 21 '10 at 06:49 PM airt

Lol, I designed the script to be hassle free and flexible and you wanted a while loop. The call back method is the best if you want asynchronous sequencing.

Aug 21 '10 at 09:48 PM spinaljack

I've upvoted this because it's the answer I would be looking for. Cheers Spinaljack (and sorry airt!)

Aug 22 '10 at 12:04 AM Marowi
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x572
x55
x36
x14

asked: Aug 21 '10 at 05:33 AM

Seen: 1172 times

Last Updated: Aug 21 '10 at 05:33 AM