roblox wait()

I stumbled across the roblox thread scheduler while looking up tutorials for coroutines. at the bottom of the page for the scheduler, it had an implementation in Lua with some syntax I wasn’t familiar with. perhaps someone could explain the enqueue_task{ }. shouldn’t it be enqueue_task( {} )?

function wait(time)
    enqueue_task{coro=coroutine.wrap(fn), at=tick() + time}
    return coroutine.yield()
end
 
function spawn(fn)
    enqueue_task{coro=coroutine.wrap(fn), at=tick()}
end
 
function delay(time, fn)
    enqueue_task{coro=coroutine.wrap(fn), at=tick() + time}
end
 
local tasks = {}
function enqueue_task(t)
    -- there are more efficient ways to insert and sort, but oh well...
    tasks[#tasks + 1] = t
    table.sort(tasks, function(a, b) return a.at < b.at end)
end
 
-- The main scheduler
while true do
    local now = tick()
    if #tasks ~= 0 and tasks[1].at <= now then
        local t = table.remove(tasks, 1)
        coroutine.resume(t.coro, now - t.at, now)
    end
end

http://wiki.roblox.com/index.php?title=Thread_scheduler

I think you’re right, there’s some missing braces (). I made a simple coroutine scheduler, but eventually realised, with input from the good people of Codea Talk, that there’s nothing you can do with a coroutine that you can’t achieve with another very powerful and underused Lua feature, closures. i.e. a closure will remember it’s upvalues, and is an instantiation with a sense of self, in other words it is a kind of “thread”. Putting that closure in a coroutine is an extra step that IMO isn’t necessary except for big tasks.

FYI, in Lua, if the only argument to a function call is a table literal or string literal, the parentheses may be omitted.

Thanks, I did not know that. I can definitely use that a lot, as most of my functions just take a table of parameters.

Wait, did you find this code in ROBLOX the video game? Or in a Codea tutorial? I happen to be a ROBLOX scripted myself, ROBLOX uses Lua 5.3 with some extra commands. Codea uses Lua 5.2, it different

@code_maker check the link in the first post at the bottom

@code_maker, Codea uses Lua 5.3

@JackAttak I think code_maker is on Codea 1.2, in another post they said that they are on an iPad 1 with iOS 5.1 and Codea 1.2 is the most it can handle, is that correct @code_maker?

100% correct, @TheSoldierKing . I’m looking into getting an iPad gen 3. Then, I can make multiplayer games. Maby even I’ll make my 2D minecraft game a multiplayer once I get an iPad 3.

So, I have to deal with Lua 5.1 let me just say, I really wish Codea 2.1 could handle IOS 5.1. Code is really outdated

@code_maker It’s not that Codea 2.1 can’t handle iOS 5, Apple made it so you can only submit apps to the App Store targeted at iOS 7 or later (no new apps are allowed to support iOS 6 and lower), to try and force users with old iPads into buying a new one.