#Coroutines what are they
1 messages · Page 1 of 1 (latest)
test
Okay good
So, let's go out of Unity for a moment, and explore what they are and how they're used outside of Unity, in standard C# apps for example.
What Unity names "coroutines" is more commonly referred as "iterator methods". They're methods that, when ran multiple times, return a different value.
I thought you did the C# basics
Returning means passing back a value to the code that executed (called) the method
Using the return instruction
void Method1() {
int number;
number = Method2();
}
int Method2() {
return 42;
}
Take this example
Method2 returns a number, 42, when called (executed)
And in Method1, we actually call the Method2.
So Method2 executes, and returns 42, and in Method1 we intercept that return value and store it into a variable, number
oh okay
but what would happen if there wasnt void method 1
the returned 42 wouldnt do anything?
It would not return anything, as Method2 is never executed
A method only returns when called
so basically, when doing coroutines
the StartCoroutine calls the IEnumerator and gets the value that is returned from it?
That's a bit more complicated.
If you understand what a return value is now, we can go back to coroutines, also named iterators
ok
So, back up here.
A normal method only returns once, otherwise you get an error.
But iterators can yield multiple values.
Example coming up...
private IEnumerator GetNumbers()
{
yield return 1;
yield return 2;
yield return 4;
yield return 8;
}
Here's an iterator, that, when called multiple times, will return a number.
An iterator can be spotted as:
- It uses the
IEnumeratorreturn type, - It contains at least one
yieldinstruction.
so when you call it one time it will return 1
if you call it the second time it will return 2
if you call it the third time it will return 3
does this work like that?
Let's execute it now. Calling GetNumbers() once, execution reaches the first yield instruction, and returns 1. Execution bails out of the iterator, so it can continue later on
Yeah exactly
ok now the 2nd question: what will happen to whats returned
ok i think i understand that now
however in my case i dont want to return anything
so why do i use this
Hang, on, still not in Unity, and the explanation is not finished
ok
Taking the example iterator above, you could do the following
foreach (int number in GetNumbers())
{
print(number);
}
And that will execute GetNumbers as long as it returns a value, so it will in term print out all the values to whatever console you're using
Okay, so now let's go back to Unity
Unity uses a loop like the one above to handle coroutines, but you don't see it as it's part of the inner workings of Unity
So let's take the simplest coroutine possible
private IEnumerator Coroutine()
{
Debug.Log("before yield");
yield return new WaitForSeconds(2);
Debug.Log("after yield");
}
nothing is returned
if you were to do something like this, there would be nothing you could return in this example
And now, you start it with StartCoroutine(Coroutine());.
StartCoroutine is a Unity method that will do all the looping work for us.
So, execution starts
The first Debug.Log is hit, and it's printed to the console.
Then, it hits the first yield instruction. And it tells to yield a new WaitForSeconds(2), which is a one-liner to say "create a new WaitForSeconds object, and yield it back"
Means "give way", ie. let other things do whatever they need, and pick back what you were doing later on
ohhh ok
So that (the WaitForSeconds) gets picked up by the inner working of Unity. It gets processed in some way (can't explain further, we don't have access to the code), and a Timer is started because we want to wait for two seconds.
Once the timer is elapsed, Unity calls our Coroutine() one more time, and execution picks back up where it left off, right after the first yield instruction. The second Debug.Log call is executed, and printed to the console.
The method finishes, and the coroutine is stopped.
And that's it. That's literally how they do it
i kinda understand it, but still its really complicated for me
Practice will make that simpler to understand
however while you were explaining me stuff i got to almost fix my problem using coroutines
this seems to be working
not entirely but its working quite well
the only problem is that its very not smooth
I have to go unfortunately, so I'll close the thread and you can repost your question back to the main channel.
I would say the first issue is the integer division you're doing (13/60), that will not behave how you want it to.
You need to do 13f / 60 to forcefully convert one to a float, giving back a decimal number.