[jitter] settimeout in lua?
Wesley Smith
wesley.hoke at gmail.com
Sat Dec 1 08:12:30 MST 2007
- Next message: [jitter] Re: settimeout in lua?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi,
In Lua, coroutines extend very nicely into time-based parameter
modeling. A coroutine is kind of like a lightweight thread that runs
a function in another process but doesn't actually spawn another
thread, that is just the model. It can be a bit confusing at first
but is a very useful concept for all kinds of collaborative processes.
In the example below, I haven't made nice wrapper functions for the
coroutine code, so it's all flat. One could design a cleaner
interface by grouping together certain operations in higher-level
functions.
wes
function fade_in(start, done, incr)
while(start < done) do
coroutine.yield(start) --pass value to resume
start = start + incr
end
end
local coros = {}
local num_lines = 100
for i=1, num_lines do
--create a new function with fade params as upvalues
coros[i] = coroutine.create(function() fade_in(math.random()*-5,
math.random()*5, math.random()*0.2+0.1) end)
end
function draw()
gl.Enable("BLEND")
gl.BlendFunc("SRC_ALPHA", "ONE")
for i, c in pairs(coros) do
local status, val = coroutine.resume(c) --get the current value
if(val) then
gl.Color(1, 0, 0, 0.4)
gl.Begin("LINES")
gl.Vertex(val, 1, 0)
gl.Vertex(val, -1, 0)
gl.End()
else
table.remove(coros, i)
end
end
end
- Next message: [jitter] Re: settimeout in lua?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
