Coroutines
I’ve been scripting for a while now, but I’ve literally never had to use coroutines ever,
l-- Function that runs in a coroutine
local function asyncTask()
for i = 1, 5 do
print("Task running: " .. i)
task.wait(1) -- Simulate a delay
end
print("Task completed")
end
-- Create a coroutine
local taskCoroutine = coroutine.create(asyncTask)
-- Start the coroutine
coroutine.resume(taskCoroutine)
print("Now I can run some other code down here aswell!")
Why not just do things in order the way you’re suppose to rather than doing multiple tasks at once?
I struggle to find any use cases of this, believe me I have made many systems and have been scripting for almost a year now but i’ve never had to use coroutines at ALL.
From what i’ve seen and done, it seems to me that basically the whole coroutine library is useless.
Task library
Now the task library is more useful, I definitely see the use of task.wait/task.delay and have used them alot. Those two are very useful.
Now the rest, I haven’t used at all really.
task.spawn starts a coroutine/function immediately through the engine scheduler, but is this really necessary? Again, haven’t had any need to start something THAT quickly. Usually code executes quite fast already in scripts, not sure why you would need to do that.
I guess some people I’ve seen want to make their while loops for example really fast? And the fastest they can do without crashing I guess would be to add task.spawn? Not sure why they’d want their loops to execute that quick though.
task.defer has a similiar behaviour to task.spawn so I won’t really get into that.
task.synchronize/task.desynchronize COULD be useful, I can’t really comment on those ones as I dont fully grasp the idea of parallel luau yet. But I will say that I haven’t had to use these two either and my games that I have worked on ran well and are fairly optimized.
I think the main one I see alot of people use is task.spawn though, which again don’t really see much use cases.