- Home /
Executing coroutines consecutively
So I have a coroutine which rotates and moves an object. First it should rotate and after it finished doing that, then it should move to its target position. I have two coroutines for handling the rotating and moving, which looks like this:
public IEnumerator CoRotateAndMove(Vector3 targetPosition, Quaternion newRotation) {
yield return StartCoroutine(Rotate(newRotation));
yield return StartCoroutine(Move(targetPosition));
}
public IEnumerator Move(Vector3 targetPosition) {
while(Vector3.Distance(transform.position, targetPosition) > 0.05f) {
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * moveSpeed);
yield return null;
}
}
public IEnumerator Rotate(Quaternion newRotation) {
for(float t = 0.0f; t < 1; t += Time.deltaTime) {
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, t);
yield return null;
}
}
Additionally I've built a system for queueing commands which uses delegates to store the function to call and a custom class for handling the parameters. I add these commands to my queue List during the course of a player's turn and when it's time to execute I call the functions in a loop like this:
void EndTurn() {
foreach(Tuple t in actionQueue) {
t.T1.Invoke(t.T2);
}
actionQueue.Clear();
}
So to finally get to the problem: When I queue two move commands with a unit, let's say first to point A and then to point B, the unit should first move to point A and then to point B. What it actually seems to do though is, it tries to execute both commands simultaneously which results in it moving what looks to be somewhere in the middle of A and B where it gets stuck (doesn't take any more commands and visibly shakes around like crazy).
Does anyone have any idea what might cause this behaviour?
Am I right in thinking t.T1 is your monobehaviour instance, and t.T2 is your coroutine then? Don't you need to be yielding in the foreach loop, I would have expected a rotation and movement to also occur simultaneously with how they look to be being called. I'm calling this pseudo code as I have no way of testing it, but it might work:
IEnumerator EndTurn() {
foreach(Tuple t in actionQueue){
yield t.T1.Invoke(t.T2);
}
actionQueue.Clear();
}
Sorry, I forgot to include this bit for the custom Tuple class:
public class Tuple {
public Func T1;
public DataObject T2;
public Tuple(Func T1, DataObject T2) {
this.T1 = T1;
this.T2 = T2;
}
}
Func refers here to the delegate which references the function, the DataObject holds the parameters for this function. I then want to call each function in my List with its respective parameter. How can I yield in the EndTurn function/coroutine? Because the functions I call in the loop aren't coroutines, they are just normal methods that themselves start the coroutines. Do I have to make these coroutines as well then?
Edit: I found a solution! Just as I said, by transfor$$anonymous$$g all methods to coroutines and then yielding in the foreach loop, the coroutines will wait until the previous one has finished, so I can chain function calls after each other. Thanks for the tip!
Good job on getting it solved. Alternatively you could have had some other callback to know when you were finished on one and could start the next, but the coroutine idea sounds simpler!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
function behaves unexpectedly inside a coroutine. 2 Answers
make events occur only at specific times 1 Answer
Loop Coroutine For A Demo Mode 1 Answer
Code working on wrong axis. 1 Answer