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 :-/

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();
	}

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?