Search Unity

How to insert a delay without using WaitForSeconds() ?

Discussion in 'Scripting' started by ale870, Apr 30, 2011.

  1. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    Hello,

    I work in javascript, and sometimes I need to insert a delay for some milliseconds/seconds in the game, but I cannot use yield WaitForSeconds() since, in classes, there are many problems related to yield instruction.
    So, is there any other method to pause the game for, for example, 100 milliseconds without using yield?

    Thank you for your help!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    To pause the whole game (as in freeze) use the thread.sleep command to sleep unity itself.

    but if its just meant to make one function wait, the way is to use yield. (be it with waitforseconds or the others or null to wait for next frame)
    also I'm not aware of any problems in relation to it, if you have them then your code is just buggy
     
    Last edited: Apr 30, 2011
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Agreed with dreamora. There are no problems with yield waitforseconds, only problems with people's understanding and usage of it.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no problem with yield in classes; the only gotcha is that in Javascript you have to explicitly use StartCoroutine for classes that don't extend MonoBehaviour.

    --Eric
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    And specify an instance of monobehavior as "caller" as its a monobehavior only functionality
     
  6. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    Thank you,
    basically the problem with yield is inside classes. Yes I can use yield in classes also, but with several annoying constraints: my class must be inherited by MonoBehavior, else there is no way to use yield, and no way to use StartCoroutine.
    Furthermore, if you inherit from MonoBehavior, one has other limitations,
    I think Unity should contain a real "sleep" function not dependent from yield.
    This is the reason I look for a function alternative (@dreamora, my code is not buggy, you can be sure of this fact :cool: !;) )
     
    RiverExplorer likes this.
  7. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    In javascript you cannot use StartCoroutine if you don't inherit from MonoBehavior.
    See this thread: http://forum.unity3d.com/threads/16472-Yield-and-javascript-classes
     
  8. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    It seems like you might have some missing knowledge on how to utilize coroutines and asyncronous programming in .NET in general from your description.

    Yes the class on which the coroutine runs must inherit from MonoBehavior and that makes sense cause all code you write for unity and not for .NET outside of unity commonly inherits from MonoBehavior.
    For code you write outside of Unitys existance, you would commonly use BeginInvoke

    As for real sleep: You can use sleep but keep in mind, real sleep sleeps the process, not an async function, which is likely never an option as nothing happens any longer if you use thread.sleep.
    Async functions sleep with yield, be it BeginInvoke or StartCoroutine, this is not unity specific but a simple .NET fundamental


    As for buggy code: Well you design around problems that don't exist if the code is written right (usage of possibility that are available), which I consider a "bug" as the code does no longer fullfill its specification: Wait for an event to happen before it continues instead of stupidly poll.
    You would potentially call it "complicated" or "not as you want it" as you don't know what paths there are.
     
  9. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    Ok, I really appreciate an answer like this, instead of simply saying "your code is buggy".
    I cannot manage well .NET code since I'm not a .NET programmer (I currently use many other languages, but not .net programming).

    I use javascript in Unity since I wanted to avoid to study my 18th programming language (C#), but I often find some "limitations" in javascript implementation in unity. Furthermore I have the idea I will have to learn C# since javascript is missing of several important features: interfaces, abstract classes, etc... (even if I found some ways to "emulate" them).
    I have no idea how to use "BeginInvoke" since in Unity documentation there is no reference to it in scripting guide.
    I noticed several threads talk about YIELD in CLASSES, but there is no clear documentation in Unity how to achieve it. So I definitively found some nasty workarounds to avoid it but, sometimes, I face sync problems.

    How in Javascript/Unity can be achived? Which "tools" Unity gives me to do that?

    Thank you for your help!
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Actually the "missing features" list is much shorter on U3. Interfaces are present for example, generics too. Abstract classes are still missing right and const fields too but aside of multi-generics and event handlers are the only "base C# feature" thats missing at the time I think.
    But Eric5h5 could offer a deeper comparision as I use JS only upon customer wish for the layer on which they interact with my work

    As for yield in classes:

    someClassDerivingMonoBehavior.StartCoroutine(TheFunctionToCoroutine());

    for example.
    The only requirement coroutines have is that they run on an instance of Behaviour / MonoBehaviour, but nothing forces you to be a monobehaviour. That being said its significantly easier to be one.

    Also I wouldn't use coroutine all over the place it gets ugly to maintain the code if different things wait in different things. Use an observer pattern with C# event handlers so you can "listen" for something to happen while a monobehaviour based singleton responsible for this something will yield and fire the event once there for example
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. class Test {
    2.     function Blah () {
    3.         yield WaitForSeconds(3);
    4.         Debug.Log ("You just wasted 3 seconds waiting for this!");
    5.     }
    6. }
    7.  
    8. function Start () {
    9.     var test = new Test();
    10.     StartCoroutine(test.Blah());
    11. }
    --Eric
     
  12. ale870

    ale870

    Joined:
    Apr 21, 2009
    Posts:
    149
    Thank you, I will use these info to refine my game, and improve internal "workflow" :)