Aego Playground

Coroutines

A coroutine is a script method that can pause and continue on a later frame.

script Player {
	@export var HitTint Color
	var base Color
	var blink Coroutine

	func Hurt() {
		if !blink.Running() {
			blink = start Blink(6)
		}
	}

	func Blink(times int) coroutine {
		sr := node.Get<SpriteRenderer>()
		for i := 0; i < times; i++ {
			if i%2 == 0 {
				sr.Tint = HitTint
			} else {
				sr.Tint = base
			}
			yield wait(0.1)
		}
		sr.Tint = base
	}
}
Form Resumes
yield on the next frame
yield wait(s) after s seconds (float) of game time, at the earliest on the next frame; paused Play does not advance it
yield frames(n) after n frames (int)
yield fixed(n) after n fixed steps (int), before physics
yield c when coroutine c ends; it resumes in the same frame; a nil, finished or cancelled c resumes on the next frame

A wrong argument type is AES2057.

Run it:

package demo

import "aego/log"

script Blink {
    var blink Coroutine

    func Ready() {
        blink = start Run(3)
    }

    func Run(times int) coroutine {
        for i := 0; i < times; i++ {
            log.Info("blink " + string(i))
            yield wait(0.1)
        }
        log.Info("done")
    }
}