Studio GuideWorld SDK Guide
Log In

Coroutine

A coroutine is a non-preemptive multitasking program component that works by pausing execution and then continuing where it left off. It is mainly used to execute such features such asynchronous operations, exceptions, event loops, iterators, infinite lists, and pipes.

Click here to learn more about Unity’s coroutine.

📘

Coroutine

https://docs.unity3d.com/kr/current/Manual/Coroutines.html


ZEPETOScript implementation of Unity coroutines as follows.

import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { WaitForSeconds } from 'UnityEngine';

export default class Coroutine extends ZepetoScriptBehaviour {
    
    private current: number;
    
    Start() {
        this.current = 0;
        console.log(`start routine`);
        this.StartCoroutine(this.DoRoutine());
    }

    *DoRoutine() {
        while(true) {
            yield null;
            console.log(`[${this.current++}] Wait to next routine..`);
            yield new WaitForSeconds(1);
        }
    }
}

  • The WaitForSeconds is used within a coroutine to implement a delay for a certain amount of time.
    • It takes a positive integer as a parameter, which represents the number of seconds.
  • In the example script above, it will output [${this.current++}] Wait for the next routine....., then wait for 1 second.
    • After 1 second has passed, it will output [${this.current++}] Wait for the next routine... again and this will be repeated indefinitely.

Check out the test code output screen below.