Task Library - Now Available!

Will some of these deprecate syntax functions/events down the road like spawn(), delay(time,func), etc? Is the task library meant to be a replacement for some of those functions/events?

3 Likes

I’ve found a bug with this, or atleast unintended behavior:

local camera = workspace.CurrentCamera do
    local desiredType = Enum.CameraType.Scriptable
    local start = os.clock()
    local retries = 30

    while camera.CameraType ~= desiredType and os.clock() < (start + 5) do
        retries -= 1
        wait()

        camera.CameraType = desiredType
    end

    warn("(DEBUG)", "Setting camera type to", desiredType.Name, "took:", string.format("%.2f", os.clock() - start) .. "(s)")
end

While using wait(), this code sets the camera type to scriptable as intended, and warns a number anywhere from 2 to 3 as usual.

However, swapping the wait() for task.wait() causes the camera type to not change, and the warned number will be from 0.00 to 0.06.

6 Likes

Did you even read the post? lol This is

4 Likes

I love how you can pass additional arguments after the function, I hated this kind of syntax:

delay(10, function() SpawnEnemies(5) end)

this is much cleaner:

task.delay(10, SpawnEnemies, 5)

awesome library!

15 Likes

What’s the performance cost of task.spawn()? Some relative comparisons would be very useful. For example, how many math operations can I do in the same time, or what sort of memory allocation does it incur?

13 Likes

spawn and task.spawn called on a function both create a new coroutine, so there’s no raw performance benefit in that regard. However task.spawn gives you the option of passing a coroutine instead if you want to manage things more closely yourself including potentially reusing coroutines for better perf.

The task library is a low level library, it does what you tell it to do. If you use task.spawn and/or task.wait to resume 100,000 threads in a frame it will dutifully try to run the 100,000 threads that frame (though there’s no guarantee the client / server in question can actually handle that without lagging / running out of memory).

22 Likes

Is it exactly equivalent in terms of performance as game:GetService(“RunService”).Heartbeat:Wait()?

7 Likes

Hey there :wave:t5:, quick question, how does task.wait(n) look like in lua, and how does wait(n) look like in lua?

5 Likes

This creates a lot more simplicity for programmers looking to create accurate solutions to wait(). I’m always excited to see improvement, especially in an area like this!

To my understanding, some of the built-in instances/functions use the wait and delay functions. Will these eventually use the task library?

6 Likes

Swap the two, you’ll get opposite results. It’s exactly the same.

10 Likes

That’s a great update, finally.
It would be cool if you added

function task.debounce(event, duration, callback, ...)

for example

-- Triggered by touched event only after 3 seconds passed of the last call
local function PartTouched(debounce, HitPart, RandomData)
	if RandomData then
		print("The part",HitPart)
		-- the cooldown of the debounce refresh after being consumed properly
		debounce.consume()
	end
end
task.debounce(part.Touched, 3, PartTouched, RandomData)

Thank you!
It would be really helpful for beginners

12 Likes

The latter would be faster due to task.wait() being built into the roblox engine (and therefore runs on C++ rather then lua).

8 Likes

Amazing, amazing, amazing - some more detail on what’s changed from the old implementations would be great! No longer having to rely on open source modules such as Sleitnicks handy Thread module is really nice but sadly am losing a little bit of built in functionality, Thread.DelayRepeat() for instance but suppose that’s outside the scope of what an engine should provide at a Libary level.

2 Likes

I’m very proud of you Roblox, finally we get a proper upgrade

@WallsAreForClimbing

so the first noticeable difference between coroutine.wrap and task.spawn is that one would return back a value while the other doesn’t

Code
local values = task.spawn(function()
	return "string"
end, 1, 2, 3)

print(values) -- nil

local values = coroutine.wrap(function()
	return "string"
end)(1,2,3)

print(values) -- string

Performance test:

5 Likes

task.spawn causes a crash if it is used too many times.

local function R(N,V)
    if N ~= 0 then
        return V,R(N-1,V)
    end
end
task.spawn(R(16000,task.spawn))

This should generate a C stack overflow like coroutine.resume.

7 Likes

Ah yes. Now we can wait faster.

29 Likes

Will this have any performance benefits over coroutines and spawn?

4 Likes

Do libraries run on C++? I thought they went through Lua.

1 Like

You shouldn’t be doing this anyways, it’s no wonder it would crash.

I’m not 100% sure, since this is directly using the task manager, I’m assuming the task manager is run on C++.

2 Likes