True, but coroutines are much more low-cost than script files. And they don’t have to be associated with game objects (although you do need at least one script file to start the coroutine).
And I love the way in which you can make long running and complex tasks look synchronous. With coroutines you can write code that looks like this:
moveTo(position, 200) -- move to position, 200 pixels per second
wait(2) -- wait 2 seconds
fireRocket(target) -- fire rocket at target
wait(1) -- wait 1 second
selfDestruct() -- boom!
A sequence of events that happen over a longer period of time, written one instruction after the other, with no notion of callbacks, delays or anything like that.
The moveTo() and wait() functions looks like this:
local function moveTo(target, speed)
local co = coroutine.running()
local pos = go.get_position()
local distance = vmath.length(target-pos)
local duration = distance / speed
go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target, go.EASING_LINEAR, duration, 0, function()
coroutine.resume(co)
end)
coroutine.yield()
end
local function wait(delay)
local co = coroutine.running()
timer.delay(delay, false, function()
coroutine.resume(co)
end)
coroutine.yield()
end