Task Library - Now Available!

Yes, task.wait() is now equivalent to RunService.Heartbeat:Wait(), and a lot more convenient too. The amount of time spent typing that out saved is astonishing…

46 Likes

Is there any reason you didn’t just change the default behaviours of those functions, and instead add a new task library? Does this have something to do with parallel lua :flushed:

Anway’s glad that useless old function has now been given a replacement

22 Likes

I believe this should answer your question:

The reason using wait() with no parameters is bad is because it (task.wait()) is

13 Likes

i’m wondering if this wait() alternative:

function Halt(Duration)
	--// Arguments: (Duration[Number])
	
	Duration = (Duration or 0)
	local cur = 0;
	if (Duration <= 0) then
		Heartbeat:Wait();
	end
	while cur < Duration do
		cur += Heartbeat:Wait();
	end
	return cur;
	
end

is the same as:

task.wait()
11 Likes

(post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

8 Likes

It should be noted that results of these aren’t equivalent:

task.spawn(function()
	print(task.wait())
end)
task.spawn(function()
	print(game:GetService"RunService".Heartbeat:Wait())
end)

task.wait()'s DeltaTime result will count from the time of the call, while Heartbeat:Wait() will count from the previous Heartbeat.


Also, task.delay() causes a crash instead of an error.

26 Likes

Is this almost like some kind of task manager for Studio?

3 Likes

Because you can’t do Heartbeat:Wait(2) since :Wait() function on RBXScriptSignals doesn’t take any arguments?

16 Likes

What is the fastSpawn pattern?

6 Likes

So what’s the difference of task.wait, task.spawn, and wait, spawn?

3 Likes

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”.

12 Likes

deferred on Nevermore (originally fastSpawn)

“An expensive way to spawn a function. However, unlike spawn(), it executes on the same frame, and unlike coroutines, does not obscure errors.”

(edit: changed link to folder with README, was originally set to a release branch)

5 Likes

I assume for legacy purposes, old games experiences whose behavior could be compromised if they just redo the old functions.

11 Likes

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).

5 Likes

Hmmm… what is happening??


Is this basically like coroutine? I mean, I am a builder, not knowing much about these, but isn’t it the same thing?

9 Likes

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!

34 Likes

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:

image
image
image

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.

29 Likes

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.

11 Likes

Documentation will be coming soon. In the meantime you can refer to this thread.

17 Likes

you can do for example task.wait(1) and it will wait 1 second, and VERY ACCURATLY

4 Likes