x


Confused about Coroutines

I'm almost convinced I'm seeing a bug, but this is an entirely new concept for me, so I need some help to understand what's going on. Here's a somewhat simplified version:

static function Initialize() 
{
    Debug.Log("EventManager.Initialize() called");
    if (initialized) return;
    Debug.Log("EventManager.Initialize() 1");       
    eventQueue = new PriorityQueue();
    initialized = true;
    Debug.Log("EventManager.Initialize() 2");
    DoLoop();
    Debug.Log("EventManager.Initialize() 3");
 }

static private function DoLoop()
{
    Debug.Log("Enter Loop()");
    while (true) {
        Debug.Log("Top of Loop() loop");
        while (eventQueue.Count) {
            // do something useful
        }
        Debug.Log("Before Loop() wait");
        yield WaitForSeconds(1.0);
    }
    Debug.Log("Exit Loop()");
}

What's weird is I never see "Enter Loop()" in the console (or anything from DoLoop()), but I see the "Initialize 1", 2, and 3. If I comment out the while (true) loop, I'll see both the "Enter Loop()" and "Exit Loop()", but otherwise neither. What's going on?

more ▼

asked Apr 06 '10 at 04:52 AM

ZowPac gravatar image

ZowPac
106 2 3 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

When a coroutine is called, it does not automatically run through to completion. It returns the body of the routine embedded in an object implementing the IEnumerator interface. Each time MoveNext is called on the object, the coroutine continues until the next yield, and returns the yield argument (in your case an instance of WaitForSeconds).

When writing scripts in C#, this is explicit. You have to start a corutine with StartCoroutine(coroutine());, where the IEnumerator is passed to the unity engine, and called at the appropriate times depending on the [YieldInstructions][2] returned from the IEnumerator.

In Javascript, I believe the compiler determines which routines are coroutines, and automatically calls StartCoroutine automatically, however apparently not from static routines.

TLDR; do as Eric suggested, remove both instances of static.

more ▼

answered Apr 06 '10 at 07:06 AM

KvanteTore gravatar image

KvanteTore
1.5k 9 15 33

Re the scripting language name, it is consistently referred to as Javascript in the Unity manual and in other official written Unity materials.

Apr 06 '10 at 09:37 AM runevision ♦♦

@Rune ok. thx :)

Apr 06 '10 at 11:14 AM KvanteTore

Javascript automatically uses StartCoroutine from anywhere, not just Update etc.

Apr 06 '10 at 05:03 PM Eric5h5

@Eric5h5. Ok, that's kind of neat and kind of scary... updated the last statement to reflect this.

Apr 06 '10 at 11:11 PM KvanteTore
(comments are locked)
10|3000 characters needed characters left

Remove both instances of "static".

more ▼

answered Apr 06 '10 at 05:24 AM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x348
x336

asked: Apr 06 '10 at 04:52 AM

Seen: 3537 times

Last Updated: Apr 06 '10 at 04:52 AM