settimeout in lua?
would anyone be able to suggest an efficient way of doing timeouts in jit.gl.lua?
say i want to make a fadein that lasts x seconds running separately from the main loop. e.g. i'm looking for the equivalent of this javascript:
var fadeval = 0
function fade() {
if (fadeval < 100) {
fadeval=fadeval+10
}
myobject.alpha = i/100
setTimeout("fade()",100)
thanks
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
thanks wes, this is great.
just noticed, i think this may be a bug in the jit.gl.lua implementation but the GL constant "ONE" in the code above is not recognized (GL Error: Invalid Enumeration). "ONE_MINUS_SRC_ALPHA" and the other blend modes work fine.
i have 1beta4 on macbook pro, max 4.6.3/jitter 1.6.3
uh yeah, finally someone points this out. I was wondering. Anyway,
what happened is I scripted a bunch of GL enums and apparently GL_ONE
occurs more than once in the GL spec and extensions so it took the
last one which is not the same as is used in the blendfunc arguments
for example, thus the error. I'll post a new version tommorrow to fix
this and add some new things.
wes
On Dec 1, 2007 7:01 PM, liubo wrote:
>
> thanks wes, this is great.
>
> just noticed, i think this may be a bug in the jit.gl.lua implementation but the GL constant "ONE" in the code above is not recognized (GL Error: Invalid Enumeration). "ONE_MINUS_SRC_ALPHA" and the other blend modes work fine.
>
> i have 1beta4 on macbook pro, max 4.6.3/jitter 1.6.3
>
>
I've uploaded a new version of osx jit.gl.lua with a fix for the "ONE" error.
wes
On Dec 2, 2007 1:19 AM, Wesley Smith wrote:
> uh yeah, finally someone points this out. I was wondering. Anyway,
> what happened is I scripted a bunch of GL enums and apparently GL_ONE
> occurs more than once in the GL spec and extensions so it took the
> last one which is not the same as is used in the blendfunc arguments
> for example, thus the error. I'll post a new version tommorrow to fix
> this and add some new things.
>
> wes
>
>
> On Dec 1, 2007 7:01 PM, liubo wrote:
> >
> > thanks wes, this is great.
> >
> > just noticed, i think this may be a bug in the jit.gl.lua implementation but the GL constant "ONE" in the code above is not recognized (GL Error: Invalid Enumeration). "ONE_MINUS_SRC_ALPHA" and the other blend modes work fine.
> >
> > i have 1beta4 on macbook pro, max 4.6.3/jitter 1.6.3
> >
> >
>