Shuffle Script Timing

I have a script wich shuffels tiles on the field. That means It rotates tiles by an array of angles, rotate enums to that corresponding angles. The next step is reposition. After that i want to call another function.

Currently i have this and basicly it works, but the last called function is called to early. How can i put this all together to work properly?

void Update () 
{
	if (shuffeling)
	{

		chooseRandomRotation ();
		chooseRandomLocation ();
		GMS.InitColorCheck ();
	}
}

void chooseRandomRotation()
{
int num = Random.Range(0,degreesToRotate.Length);
float rot = degreesToRotate[num];
	for (int i = 0; i < allCards.Length;i++)
	{
		allCards*.transform.rotation = Quaternion.Euler(0,rot,0);*
  •   	//now rotate all enums according to the direction*
    

_ CC = allCards*.GetComponent();_
_
//RotateEnums for num amount*_
* for (int j = 0; j < num; j++) {*
* //Update the colors*
* CC.temp = CC.Top;//save top in temp*
* CC.Top = CC.Left;// set top to left*
* CC.Left = CC.Bottom;//set left to bottom*
* CC.Bottom = CC.Right;*
* CC.Right = CC.temp;//set top to the saved topleft(temp)*
* //Update Forms*
* CC.formTemp = CC.formTop;//save top in temp*
* CC.formTop = CC.formLeft;// set top to left*
* CC.formLeft = CC.formBottom;//set left to bottom*
* CC.formBottom = CC.formRight;*
* CC.formRight = CC.formTemp;//set top to the saved topleft(temp)*
* }*
* }*
* shuffeling = false;*
* }*
* void chooseRandomLocation()*
* {*
* while(Fields.Count > 0)*
* {*
* for(int k = 0; k < allCards.Length;k++)*
* {*
* int l = Random.Range(0,Fields.Count);*
* Transform newloc = Fields[l];*
* allCards[k].transform.position = new Vector3(newloc.transform.position.x,newloc.transform.position.y,newloc.transform.position.z);*
* allCards[k].GetComponent().oldPosition = newloc.transform.position;// set the shuffeled position as the old position*
* Fields.RemoveAt(l);*
* }*
* }*
* }*
}
I hope you understand my problem and have an idea how i could fix it. Thanks in advance.

but the last called function is called to early

chooseRandomLocation ();
GMS.InitColorCheck ();

Sorry if this sounds dumb to you, but which function is the “last” function? The last function defined in the script (chooseRandomLocation), or the last function called in the statement block (GMS.InitColorCheck)? And how soon is “too early”?

Anyway, if you have a method x, and you want to call method y after some unknown time t where t = f(x), either you must provide a way for the caller to know how long it will take, or let it know when it is done.

The former relies on the method x to make a promise for how long it will take (project the time required). The calling code then has to schedule the call to y (use a timer, when timer expires, call y).

The latter relies on the method x to signal when it is done, however long that may be. The calling code can either poll for a result (are we there yet?) or perhaps more conveniently, tell x to call y when x is done (you have arrived) through delegation.

Timing can be as simple as a float-callback pair.

class EventTimer
{
    public float remaining;
    public event System.Action onExpire;
    public void Tick()
    {
        remaining -= Time.deltaTime;
        if (remaining <= 0)
           onExpire();
    }
}

Polling can be as simple as a function-callback pair.

class EventPoller
{
    public System.Func<bool> poll;
    public event System.Action isDone;
    public void Tick()
    {
        if (poll())
           isDone();
    }
}

Timing, Polling and Blocking can also be done in a Coroutine.

IEnumerator Example()
{
    X();
    yield return new WaitForSeconds(timeX); // timing

    Y();
    while (!doneY) { yield return null; } // polling

    yield return StartCoroutine(Z()); // blocking

    print("Called X, Y and Z");
}

Delegation can be as simple as giving the function a reference to the method to be called on completion.

X(Y);

So you wanted to try polling based on our comments.

There’s a demo you can try to help you follow the example. Clicking somewhere will queue a command to go there, when the navigation is free to do so. Right clicking will queue a command to go to the next position in a list of waypoints. The text overlay will get pruned ever so often to remove async results that have been completed.

There are two ways the pending stuff is used. Internally, it uses it to format the text of a move order. The client uses pending to count how many calls have completed and for cleaning up the list when it grows too big.

UI Script

Navigation Script

Other building blocks

I made a UI script that request a navigation script to go to another location (when it is free). The result from the call is an IAsyncResult<Vector3> which is a type I made up that has a pending and result member. So you can wait until the async result no longer is pending, and then access the result.

The actual code that does something over time, well I made an order system for my navigation. Each move order is an AsyncCall<Vector3> , meaning, each takes time and at the end, you’ll get a Vector3. The main “Update” loop is the method “Process” which call Return() on the base type. AsyncCall is pretty much just an implementation of IAsyncResult. It allows the server to call Return() on the object, but that method is not exposed to the client.