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

Coroutines or an equivalent ?

Discussion in 'Project Tiny' started by SorneBachse, Jan 24, 2019.

  1. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Hey!

    So, I'm new to web development and typescript. Is there anything like a coroutine in Tiny mode? I'm trying to basically call a function after a x amount seconds. I know I could make an Update timer, but wanted to ask if this was possible as well.

    Thanks!
     
  2. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    For anyone that's interested, it seems to work with the following:

    Code (JavaScript):
    1. setTimeout(function () {
    2.   //do something once
    3. }, 1000);
     
  3. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Code (CSharp):
    1. AfterDelayAction(ms: number, callback: () => any) : void{
    2.             var start = new Date().getTime();
    3.             var end = start;
    4.             while(end < start + ms*1000) {
    5.                 end = new Date().getTime();
    6.             }
    7.  
    8.             callback();
    9.         }
    Action:
    AfterDelayAction(1.5, function() { console.log("It Works after .1.5 second!"); })
     
  4. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Since this freezes everything up until that while loop is done, it isn't really feasible unfortunately.
     
  5. TheFordeD

    TheFordeD

    Joined:
    Jul 9, 2018
    Posts:
    32
    Okay, alternative :).
    May be this code help you with trouble.

    Code (JavaScript):
    1. AfterDelayAction(ms: number, callback: () => any) : void{
    2.             setTimeout(function () {
    3.                 callback();
    4.             }, ms*1000);
    5.         }
     
    Last edited: Jan 24, 2019
  6. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Thanks! After a slight modification it worked as expected :) In your example the callback you pass as a reference never gets called.

    Code (JavaScript):
    1.     AfterDelayAction(seconds: number, callback: () => any): void
    2.     {
    3.         setTimeout(() =>
    4.         {
    5.             callback();
    6.         }, seconds * 1000);
    7.     }
     
  7. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    I made a little "wanna-be" coroutine logic, if anybody wanna use that:

    Code (JavaScript):
    1. module Utils
    2. {
    3.     export function StartCoroutine(seconds: number, coroutine: () => any) : number
    4.     {
    5.         return setTimeout(() =>
    6.         {
    7.             coroutine();
    8.         }, seconds * 1000);
    9.     }
    10.  
    11.     export function StopCoroutine(id: number) : void
    12.     {
    13.         clearTimeout(id);
    14.     }
    15. }
    Usability:
    Code (JavaScript):
    1. let routine = Utils.StartCoroutine(1, this.TestCallback);
    2.  
    3. ...
    4.  
    5. Utils.StopCoroutine(routine);
     
    frankprogrammer, alamac123 and tomny like this.