Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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

    Joined:
    Jun 12, 2013
    Posts:
    3,046
  3. abeisgreat

    abeisgreat

    Joined:
    Nov 21, 2016
    Posts:
    25
    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.
     
  5. alamac123

    alamac123

    Joined:
    Mar 15, 2017
    Posts:
    10
    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