- Home /
How to make your script wait until function finishes
Greetz, i need a bit of help
I m having a hard time for a few days allready figuring out how to execute multiple iTween.MoveTo functions.
Basically, i need my script to wait for the iTween.MoveTo to finish moving and then proceede to next stuff.
Simple example:
iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination1, "time", 2f));
//now here script has to wait until upper iTween.MoveTo finishes moving
iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination2, "time", 2f));
In this case, second iTween.MoveTo "kills" first one and only second one is executed.
I tryed to make it with delay property of iTween.MoveTo, but then other kind of problems occur, i.e. MoveTo waits but gets wrong destination point since destination is calculated too early. I tryed with yield but no results, i dont understand that concept quite well.
Anway, i need my script to wait (not completely freeze) until iTween.MoveTo finishes, how could i do it, maybe make a separate thread and then stop one until other finishes or.... ?
Plz help.
Answer by kurifu · Dec 18, 2012 at 07:51 PM
The easiest way to do this is use launch the movement in a Coroutine, say something like this
void StartMoving()
{
StartCoroutine(DoMoving());
}
IEnumerator DoMoving()
{
iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination1, "time", 2f));
yield return new WaitForSeconds(2f);
iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination2, "time", 2f));
yield return new WaitForSeconds(2f);
// ... other sequential actions here?
}
Now there are ways you can improve this, for instance don't wait on discrete periods of time, I'm not sure offhand if iTween provides some object you can check to see when an action is done but this might be best, or you could even check the distance between the current location and the target location and continuing once you get to approximately 0.
Answer by justin_iSO · Dec 19, 2012 at 12:13 AM
If you look at the iTween documentation there is a field in the Hash values called "oncomplete". This is a string that is the name of a function to call when the iTween finishes. So just move your code to finish and next iTween call to a separate method and place that method name as the value to "oncomplete". Saves all the extra steps with setting up a coroutine.
~Justin
Justin, i think i could not do it like that, i have multiple functions with iTween calls, and those functions have multiple loops with iTween calls in them, + some logic to execute before and after the $$anonymous$$oveTo call. I just cant imagine how could i syncronize it all with onComplete.
Now i made every animation function IEnumerator + i made new IEnumerator function to call those IEnumerator animation functions and i can yield this animation functions (script waits for anim. fun. finish, before proceeding to another animation funciton), and i put a "yield wait for seconds" after every iTween.$$anonymous$$oveTo call... Yeah its not very nice but it works, i could use better solution, probably i should redesign my whole code, but i m fine for now, i m learning : ).
thx
Joakim
Answer by Piflik · Dec 18, 2012 at 07:29 PM
Have a look at coroutines. I haven't used them myself enough to have more specific advice, but you can make coroutines pause for a certain amount of time, or until another coroutine has completed.
Answer by TheCheese · Dec 18, 2012 at 07:40 PM
What you're looking for is a coroutine. In c# coroutines look like this:
void FunctionThatCallsCoroutine(){
StartCoroutine(ExampleFunction());
}
IEnumerator ExampleFunction(){
iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination1, "time", 2f));
yield return new WaitForSeconds(2f);
iTween.MoveTo(tile.gameObject, iTween.Hash("position", destination2, "time", 2f));
}
Answer by Joakim · Dec 18, 2012 at 09:31 PM
Thx guys! i did try coroutines last night and could not get it work.
Now i got it better and it workz!! Thx a lot !
Peace out
Joakim
Glad you got it figured out! It would be appreciated if you could check the most useful response as the answer. Thanks.
I did not know i should do that i m new here xD. Ok i did it now, i would like to check 3 answers as correct cos they all are. Thx one more time
Your answer
![](https://koobas.hobune.stream/wayback/20220613085619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iTween: change parameters during the path 1 Answer
Multiple iTween MoveTo's 1 Answer
iTween Help Please MoveTo not working 1 Answer
iTween movetopath 1 Answer
Unknown Identifer 'iTween' 1 Answer