Search Unity

Time slot for an update?

Discussion in 'Scripting' started by Tomas, Oct 29, 2007.

  1. Tomas

    Tomas

    Joined:
    Mar 30, 2007
    Posts:
    21
    Hello,
    Im wondering if there is a fixed time slot for the execution of an update. Is there preemption? If so, what does it happen when an update is stopped in the middle of a loop? Does the next update start again the loop or does continue fron the iteration it was stopped at?

    Im trying to test this with the following two scripts attached to two different objects.

    Code (csharp):
    1.  
    2. function Update() {
    3.     while (i < 12000.0) {
    4.     //nonsense inefficient math calculations
    5.       res = res + Mathf.Exp(i);
    6.       var aux = Mathf.Exp(i);
    7.           var aux2 = Mathf.Exp( 0.01 * (Mathf.Exp(i)*      Mathf.Exp(i+1)));
    8.       aux2 = Mathf.Exp(aux * (Mathf.Exp(aux2) *  Mathf.Exp(aux2 + res)));
    9.       i = i + 0.025;
    10.     }
    11. }
    12.  
    13.  
    14.  
    15. function Update () {
    16.     print("time CHECKED");
    17.     Debug.Log(Time.deltaTime);
    18.    
    19. }      

    And it happens that Unity begin to run slower till it reaches a point where is kind of blocked, after some minutes it comes back (the first scrip it is actually more complex and only performs the loop a few non-consecutive times). Checking the log I observe that the Time.deltaTime values printed are not excesively high, varying between 0.02 and 0.08 s.

    Thanks a lot
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, it runs every frame, and it can't be interrupted. If you do heavy calculations every Update, then the framerate will suffer and there will be longer times between Updates. (FixedUpdate, on the other hand, runs every physics frame, which is 60fps by default. You should normally only use it for doing physics stuff, though.) If you want to do just some stuff every frame, like part of a huge loop and not the whole thing, then you can use coroutines and the yield statement. I'd recommend looking those up in the docs.

    --Eric
     
  3. Tomas

    Tomas

    Joined:
    Mar 30, 2007
    Posts:
    21
    Thanks a lot!
    Ill look up for the coroutines stuff.

    What u explained bring me other questions:
    If an Update cannot be interrupted how is possible that a FixedUpdate run at a fixed framerate?
    I mean, if I have an Update that lasts a lot (much more than 1 / physics_fps ) then if the FixedUpdate has to be launched in the middle of the Update execution, what would happened? the FixedUpdate would be delayed? or the Update preempted?

    Another thing, if the FixedUpdate always runs at a fixed framerate, what would it happen if I actually put my huge loop with calculations in it? Would the FixedUpdate then be preempted?

    Again thanks a lot :)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I don't know the ins and outs of how Unity is programmed, but I imagine that internally it schedules stuff as appropriate.

    Dunno...not a good idea, I imagine. :) Probably it attempts to run at 60fps (or whatever you have it set to), but if it can't, then obviously it won't be able to do 60fps.

    The thing with Unity is that it's all high-level stuff, so you don't have to worry about the low-level handling because Unity takes care of that for you. Just try not to do too much in one frame, like massive loops. Keep Update functions as lean and mean as you can, and use yield and coroutines as appropriate. You can't put yield in an Update, FixedUpdate, or Awake.

    --Eric
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Then FixedUpdate will be executed after Update. And possibly executed multiple times to "catch up" (i.e. there can be zero, one or more FixedUpdate calls for each Update call).

    Then your game will play really bad :)