Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Wait

Discussion in 'Project Tiny' started by nik_ai, Jan 1, 2019.

  1. nik_ai

    nik_ai

    Joined:
    Jun 14, 2017
    Posts:
    9
    How to set up wait, IEnumerator like functionality.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
  3. abeisgreat

    abeisgreat

    Joined:
    Nov 21, 2016
    Posts:
    44
    Hey Nik,

    Try something like this..

    Code (JavaScript):
    1. // Set up a wrapper around setTimeout to act like a blocking yield. This could be a method on your class or in a separate utility class.
    2. async function delay(timeInSeconds: number) {
    3.   return new Promise((resolve) => {
    4.     setTimeout(resolve, timeInSeconds * 1000);
    5.   });
    6. }
    7.  
    8. // Then in your method Behavior / System define a new async method
    9. async function DoSomething() {
    10.   console.log("Hello world!")
    11.   await delay(3);
    12.   console.log("I'm waiting...?")
    13.   await delay(3);
    14.   console.log("Oh c'mon now!")
    15. }
    16.  
    17. // Then invoke DoSomething like a normal method (this.DoSomething) like you would StartCoroutine when invoking an IEnumerator in C#.
    18.  
    For more information about async / await in Typescript, check out this Typescript post.
     
    nik_ai likes this.
  4. nik_ai

    nik_ai

    Joined:
    Jun 14, 2017
    Posts:
    9
    Thanks @abeisgreat
    I tried it and its showing error because this functionality is only supported in latest version of typescript.
    I also added tsconfig.overrid.json file to use specific lib and target, but error still persist.
     
    osamansr2o1oo likes this.
  5. alamac123

    alamac123

    Joined:
    Mar 15, 2017
    Posts:
    22
    Code (CSharp):
    1. WaitOneShot(ms) : void{
    2.             var start = new Date().getTime();
    3.             var end = start;
    4.             while(end < start + ms*1000) {
    5.                 end = new Date().getTime();
    6.             }
    7.         }
    i tried and it's OK.
     
    nik_ai likes this.
  6. nik_ai

    nik_ai

    Joined:
    Jun 14, 2017
    Posts:
    9