#Coroutines what are they

1 messages · Page 1 of 1 (latest)

karmic basin
#

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.

shrewd ledge
#

what does returning a value mean

#

how do they return

karmic basin
#

I thought you did the C# basics

shrewd ledge
#

i only know code structure and usage

#

so yeah

karmic basin
#

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.

shrewd ledge
#

yes

#

but

#

what does the code do with the 42

karmic basin
#

So Method2 executes, and returns 42, and in Method1 we intercept that return value and store it into a variable, number

shrewd ledge
#

oh okay

#

but what would happen if there wasnt void method 1
the returned 42 wouldnt do anything?

karmic basin
#

It would not return anything, as Method2 is never executed

#

A method only returns when called

shrewd ledge
#

so basically, when doing coroutines
the StartCoroutine calls the IEnumerator and gets the value that is returned from it?

karmic basin
#

That's a bit more complicated.
If you understand what a return value is now, we can go back to coroutines, also named iterators

shrewd ledge
#

ok

karmic basin
#

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:

  1. It uses the IEnumerator return type,
  2. It contains at least one yield instruction.
shrewd ledge
#

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?

karmic basin
#

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

shrewd ledge
#

ok now the 2nd question: what will happen to whats returned

karmic basin
#

Well, that depends on your code

#

Reminder that we're still outside of Unity here

shrewd ledge
#

ok i think i understand that now

#

however in my case i dont want to return anything

#

so why do i use this

karmic basin
#

Hang, on, still not in Unity, and the explanation is not finished

shrewd ledge
#

ok

karmic basin
#

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

shrewd ledge
#

yeah i understand that

#

thats like shown in the first example

karmic basin
#

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");
}
shrewd ledge
#

nothing is returned

shrewd ledge
karmic basin
#

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"

shrewd ledge
#

what does yield mean

#

as a verb

karmic basin
#

Means "give way", ie. let other things do whatever they need, and pick back what you were doing later on

shrewd ledge
#

ohhh ok

karmic basin
#

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

shrewd ledge
#

i kinda understand it, but still its really complicated for me

karmic basin
#

Practice will make that simpler to understand

shrewd ledge
#

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

karmic basin
#

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.