- Home /
How to Invoke() .. one frame
Unityscript ...
I am in routine BB and I want routine CC to run, in the next frame. The elegant solution would look something like..
 Invoke( "CC", one frame );
Is there such a thing? If you try Invoke( "CC", 0.00001 ) does that in fact safely mean next frame -- investigation says NO, that does not work. Perhaps some undocumented parameter?
PS I am fully aware you can do this by calling DD where DD is
 function DD() { yield; CC(); }
but then that's a coroutine, blah blah. (Come to think of it, I guess you could make a InvokeNextFrame(F:Function){yield;F();} But it's not elegant or builtin.)
Anyway does anyone know the elegant way to "Invoke next frame" ? Thanks a million.
Exactly as my copain below says, in unityscriot, you can test like this:
 private var index:int = 0;
 function Update()
 {
 if ( ++index > 10) return;
 Debug.Log("runloop "+index);
 Invoke("Print",0.00001);
 }
 function Print()
 { Debug.Log("printer "+index); }
You will see it does not work, not reliably anyway.
By the way, here's a demo that the inelegant "call a caller" function works as far as it goes.
 private var index:int = 0;
 function Update()
 {
 if ( ++index>10) return;
 Debug.Log("in loop "+index);
 INFPrint();
 }
 function Print()
 {Debug.Log("printer "+index);}
 function INFPrint() { yield; Print(); }
 // INF means Invoke Next Frame
same deal for this InvokeNextFrame(F:Function){yield;F();
Maybe it's really better just to have your own runloop going :-/
Answer by fafase · Nov 13, 2012 at 08:59 AM
OK I run a little test, maybe that will help you out:
 public class Test : MonoBehaviour {
     int index = 0;
     void Update () {
         index++;
         if(Input.GetKeyDown(KeyCode.Space)){
             print (index);
             Invoke ("Print",0.0001f);
         }
     }
     void Print(){
         print (index);
     }
 }
As you can see the index is incremented each frame. Running this I can see the printing value are the same inside the Update and in the function invoked.
Now if I change the value from 0.0001f to 1 I see total different values.
Now if I use:
 Invoke ("Print",Time.deltaTime);
I see a +1 meaning next frame. Problem is that deltaTime is not really reliable as a constant.
Is that what you needed?
Thanks for spending all the time on this --- right, that's exactly how you would test it. Interestingly in your test you report the "small value trick" DOES NOT work (I've had the opposite result -- actually I just tested it now and like you I found it does not work).
I think Invoking on Time.deltaTime just seems like a horrific idea, I'm sure you agree - it can't possibly work every time.
We just need someone who absolutely knows if there is an undocumented way to do this :-/
Thanks again ...
the deltaTime thingy would fix it in the FixedUpdate but in Update I would not risk it...
no really good solution on this, I've been using this
 InvokeNextFrame(F:Function)
 {
 yield;
 F();
 }
around. Sometimes you need to StartCoroutine on the F() of course. It's a mess. I can't believe youc an't just schedule next frame. Thanks for the time.
Os see $$anonymous$$ichael's tidy version below
Answer by MichaelTaylor3d · Nov 30, 2012 at 05:02 PM
Here is a solution that wont require you to use a coroutine in your current class, just put this in your base class.
     public delegate void Function();
         
     public void InvokeNextFrame(Function function)
     {
         try
         {
             StartCoroutine(_InvokeNextFrame(function));    
         }
         catch
         {
             Debug.Log ("Trying to invoke " + function.ToString() + " but it doesnt seem to exist");    
         }            
     }
     
     private IEnumerator _InvokeNextFrame(Function function)
     {
         yield return null;
         function();
     }
Your answer
 
 
             Follow this Question
Related Questions
Is it possible to invoke a button Press With a Coroutine? 1 Answer
Invoke Repeating Rate 1 Answer
Coroutine not running 1 Answer
Yield return inside a loop slowdown problem 2 Answers
WaitUntil doesn't work and the coroutine starts anyways 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                