Skip to content

Commit

Permalink
add coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
borbware committed Mar 26, 2024
1 parent 71ed7ff commit 27aa8a5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tic80-advanced/coroutines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
marp: true
paginate: true
theme: borbtheme
math: mathjax
---
<!-- headingDivider: 3 -->
<!-- class: invert -->

# Coroutines

## What are coroutines

* Couroutines are a nifty Lua feature
* Special functions that we can jump out of
* and then later continue execution
* from the same line we last stopped at
* Coroutines can be used to
* Loop through large structures one subset per frame
* Construct cutscenes
* Delay things

## Example: Large loops

```lua
t=0
x=96
y=24

corout = coroutine.create(function()
for i=1,10000 do
if i%100==0 then
x=i/100*8
coroutine.yield()
end
end
end)

function TIC()
cls(13)
if t%60==0 then
coroutine.resume(corout)
end
spr(1+t%60//30*2,x,y,14,3,0,0,2,2)
t=t+1
end
```

0 comments on commit 27aa8a5

Please sign in to comment.