I’m still struggling to understand use cases for task.defer. I understand how it works and how it fits into the library but under what circumstances would you want code to run at some predetermined point later in time that you have no control over? Is there any real benefit in doing so?
I also don’t understand this. How is it not a replacement for spawn? And wasn’t the original fastspawn method implemented with BindableEvents? With deferred events that would mean that it wouldn’t be an immediate resumption, unless of course I’m misinterpreting the meaning of “fastSpawn pattern”.
Will documentation for task happen soon? I’d expect it to be available on the developer hub for more info and on task scheduler article on availability of a class (aka today).
Updated my “Spawn is Evil” article to just point to the new task library. Glad that this problem finally has an easy solution that doesn’t require several paragraphs to explain!
Wanted to test the accuracy to see if task.wait has any benefits. Here’s the results of averaging the delta time of each function 20 times:
task.wait really is EXACTLY Heartbeat:Wait(), only much easier to type out. For anyone asking the differences between wait and task.wait, the answer is accuracy.
You should use defer in cases where you want to run some code after the current code has yielded, terminated, or you simply don’t care about the exact timing of it. Meanwhile the new spawn method will halt the calling thread until the thread it resumes has finished executing. So the main benefit is it won’t halt your code but will still execute almost immediately.
The new spawn method resumes a thread immediately while the old one waited a frame before resuming the callback and was subject to throttling. You can best replicate the old behavior by doing the following:
task.delay(0, function () ... end)
With deferred Lua enabled, events are deferred which is the reason for adding the new spawn method. It allows you to replicate the behavior of fastSpawn when deferred events are disabled.
Also, it kinda runs a bit faster than heartbeat < 0.0000001 nanosecond faster, and will you guys fix the problem thats its very inaccurate the first few seconds of starting the game?
This is a spectacular update!
A long awaited accuracy improvement for wait and an alternative to using coroutines and is speedier inside the call than out!
Really cool, excited for Parallel Lua!!
As someone who uses coroutines a lot to get around the shortcomings of spawn, are there practical differences between task.spawn(f) and coroutine.resume(coroutine.create(f))? (or even coroutine.wrap(f)()?
The time returned by Heartbeat:Wait() will be the time since the last frame while the time returned by task.wait() will be the total time elapsed since that call. In other words, this is to be expected.
You can really see the difference when calling task.wait() from one of our other engine events. For example, the following will result in a much shorter yield time.
Not gonna lie this scared me for a second. I thought by “finished executing” you meant the resumed thread had to be dead before it returned to the calling thread but that is definitely not the case.
Thanks for clearing up the rest of it though, although I still find it hard to believe that you wouldn’t care about execution order of your code but the part about running code after the current code yields or terminates makes perfect sense.