Search Unity

Waiting for a condition before painting each frame

Discussion in 'Scripting' started by Inverse, Jul 13, 2009.

  1. Inverse

    Inverse

    Joined:
    Jul 13, 2009
    Posts:
    10
    Hi guys. I'm writing a game in which each frame must be painted at very specific moments (when a signal message is received from the net). That means that when the game is about to paint a frame it must pause, wait for the signal and then proceed with the painting.

    I figured the right moment for the wait to occur would be at the camera's OnPreCull() method, as some game objects could be affected (moved) when the signal is received and processed, so they would be still culled and rendered correctly. However, I can't manage to make the wait work well: if I use a 'while( !signalReceived ) {}' statement to pause the game until the signal is received, the game freezes forever (seems as if it's too tight so it hogs all processor cycles, not leaving time for the other threads to process). The painting must be done immediately after the signal is received, so I can't use any delay inside the while in order to make it use less CPU.

    Then I thought there must be a much better method in Unity than my noob approach. How would you do it? read: make the game wait for a certain condition before painting each frame, make the wait precise, and make it wait in a way that it uses few resources so other threads can execute well.

    Thanks for your time.
     
  2. phuzzy

    phuzzy

    Joined:
    Feb 12, 2009
    Posts:
    31
    What you're after is to receive an update about the game and react on the next draw call, yes? You can probably just do this in an Update() and let the stack naturally flow to any subroutines you have to determine what the reactions are before drawing the next frame.

    The thing to remember is that Unity is going to call Update() for every frame it draws, so just hitch your behavior to that wagon.

    You can check out the method descriptions on http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html to get a sense of the call order of the Monobehaviours.
     
  3. Inverse

    Inverse

    Joined:
    Jul 13, 2009
    Posts:
    10
    Yep, that's mostly what I'm after, with the particularity that there must be exactly one frame drawn for every update of the game that is received, so the game must halt until it receives the next update (i.e. no drawing must occur unti then). The halting is what I can't get my head around to do in an efficient manner. But thanks for taking the time to respond :)
     
  4. phuzzy

    phuzzy

    Joined:
    Feb 12, 2009
    Posts:
    31
    Ain't no monkey that can stop the show of those Update()s getting called each time a frame is drawn, and frame is drawn as quickly in sequence as the machine running the app will allow. I suspect what you really want to do is not have anything react or move until you want it to.

    Consider Update() as dual purpose. It can react to an event or behavior as-needed on a frame-by-frame basis. It is also what you can use as an idle behavior, as it gets called at the top of each frame no matter what.