Doubt about code's flow

Hello, i was reading the documentation and i am confused now.

It says:

function teste() {
a();
b();
}

The b function call could be finished before a ends first, how can it be possible? Does unity creates one thread for each function to run in parallel? I was reading that i can work around this using coroutines, but i would like to know how the stuffs works behind the curtains

Cheers.

It’s possible if it’s a coroutine.

No.

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

–Eric

Thanks for the replay, Eric :slight_smile:

Just one more question :slight_smile:

Do ();
print (“This is printed immediately”);

function Do ()
{
print(“Do now”);

yield WaitForSeconds (2);

print(“Do 2 seconds later”);
}

You told me that it’s possible if it is a coroutine, but in the example above
print (“This is printed immediately”);
Is printed before the
print(“Do 2 seconds later”);
executes, but Do() is not a coroutine. I did not understand yet, sorry for my ignorance;

Cheers

I read that in javascript every call to a function the compiler creates a coroutine, so Do() actually is a coroutine, so I think that I did not understand the coroutine itself. How the code is divided by the processor when a coroutine is called :slight_smile:

Thanks Eric

A function is only called as a coroutine if it actually is a coroutine. If it has “yield” in it somewhere, then it is. You can write out “StartCoroutine” if you want, but in Unityscript it’s not necessary.

It’s not divided, it’s all in one thread.

–Eric

Hello again, I was reading about the coroutines, so I understood the concept (i guess), coroutines give me a mechanism to order and save the state using first-class continuations. I already played with setjmp and longjmp in c++, but i can trace the execution going to a function and returning to it caller. In Unity calling Do(); the executing goes to Do() method, if i evaluate a yield, it will return and if i call Do() again the flow will continue from the yield I have called. So i can see the execution flow, but in the example Do() is called and print (“This is printed immediately”); is printed before Do() finished. I cannot figure out what happened, is the true flow behavior created during compilation to .Net dlls?

Cheers.

if you call the function Do() from the function Start() in Unity, it will only get called once.
It will start the coroutine DO() and execute it immediately

Sorry for my english Eric, I would like to know the flow that Unity does when calling the functions, when one is called and returned, started and finished, can you teach me that?

Thanks so much

Thanks Appels, I know Start is called only once and before an update call and it is usually used to innitialize some stuffs, my doubt is how the flow is managed by the unity, i cannot see the returning from one to call another and how the two can be managed in parallel without multhread, so if it’s sequential how unity organized the stack calls to each coroutine

through a discrete tasklet scheduler.

Whenever you call Startcoroutine (or just yield in unityscript) or yield, your function with a pointer to this point within the code is put back onto the future execution handling (likely priority queue as it needs to be sorted by the distance from now to the future to efficientely process the stuff in the right order for relationship management etc) where it will be executed once that point in the future is either reached or has passed (the execution has no millisecond precision granularity)

if you have some LUA background: It knows a similar concept of Coroutine. Going by that, you can think of “yield” as a call that splits your function internally at compile into two functions and that makes the part before the function “coroutine call” into the part with yield and after it which then directly goes to sleep with yield.

thats at least my understanding from experience of how it works and how such things are done even for real threads on OS level

No, if it did that then it would be impossible to test in the editor. If you want it to wait for the coroutine to finish, then use

yield Do();

instead of

Do();

–Eric

Thanks Dreamora, it is what i was looking for, cause everybody understand the concept but not the internal behavior.
Thanks Eric.

Cheers.

Studying unity to my university project, so i need to know some engine mechanisms. :slight_smile:

Hello again, if somebody would become interested in how the unity keep track of the coroutines created, cause nobody knows or can’t tell me what kind of architecture is used to managed the scheduler of the coroutines inside Unity.
I found this article that can give you an idea about that: (I said idea)

And how yield can works in C #:
http://www.ytechie.com/2009/02/using-c-yield-for-readability-and-performance.html

An Unity architect reading this could light my way or keep the secret locked by seven keys… :stuck_out_tongue:

var global = 0;

Example ();
print (global);

function Example ()
{
print (global);
yield WaitForSeconds (2);
global = 1;
print (global);
}

This wil print
#=> 0
#=> 0
#=> 1

When the flow reachs yield WaitForSeconds (2); the flow is back to the caller and the next time that Example() is called the flow goes to
global = 1; print (global);
I would like to know who called Example again, cause i don’t have this in the code, How the flow is managed inside Unity to organized the code cause it is not parallel, the wiki gives a illusion mentioning this word, but if there is only the main thread and I don’t have the knowlodge about the sequential instruction I will be tricked managing global variables as in my example where the values to print could change cause i don’t kow which code is done first.
Can someone give me an explanation?

Cheers.

PS: Again sorry for my English ><

The numbers on left represents the sequence of execution.

  1. var global = 0;

  2. Example ();

  3. print (global);

function Example ()
{
3. print (global);
4. yield WaitForSeconds (2);
6. global = 1;
7. print (global);
}

when yield WaitForSeconds(2) is called, the funtion halts its execution then the code continue running from where the function Exemple was called.
After 2 seconds have passed, the execution continues at number 6 (global = 1).

Probably some scheduling algorithm take care of looking when and from where any suspended functions need to continue its execution.

Obrigado Felipe, deve a ver alguma forma de organização de código que a Common Language Runtime utiliza para gerenciar a troca de contextos e execuções das funções. Estou procurando algum documento que me leve mais a fundo, agora nada que seja a respeito com a Unity.

Abraço.