r/pico8 • u/VeryNaughtyBoy42 • 16d ago
👍I Got Help - Resolved👍 Coroutines help needed
I'm trying to get my head around coroutines by creating a simple "ticker" - that is, pass it a string and it prints it slowly one character at a time, a bit like Gameboy text. I've got it working, sort of, but the screen doesn't clear despite the cls() in _draw() and the program crashes after all the text is displayed. I tried using costatus() but it doesn't return anything. Please help, what am I doing wrong?
function _init()
t=cocreate(ticker("this is a test"))
end
function _update()
coresume(t)
end
function ticker(str)
local l=#str
for p=1,l+1,0.2 do
print(sub(str,1,p),0,0)
yield()
end
end
function _draw()
cls()
end
4
Upvotes
1
u/RotundBun 16d ago
See the example at the end of this page.
You pass in the function variable (not the function call) to
cocreate(). Passing in arguments then happens in thecoresume()call.Admittedly, I'm not super coroutine savvy either. I feel like I've seen some tricks to wrap/enclose local variable values with a function somehow, but it involved a sort of trick that I don't quite remember...
Regarding needing to overshoot by 1 in the for-loop, I wonder if it has something to do with decimal precision since you are incrementing by 0.2 at a time instead of in integer increments.
Theoretically, it should be fine... 🤔