Is There Any Downside to Using ContentProvider:PreloadAsync() in a task.spawn?

I’m messing with a loading screen and wondering if there are any downsides to wrapping ContentProvider:PreloadAsync() in a task.spawn. Mainly concerned about if it’ll cause performance issues and would like some insight.

1 Like

Why a task.spawn()? You could just use a simple for loop.

For my use case, it takes forever if it’s in a for loop as shown below

for Index, Asset in ipairs(Assets) do
    ContentProvider:PreloadAsync({Asset})
end

but when I wrap it in a task.spawn like below, it tends to take less time and still loads them all

for Index, Asset in ipairs(Assets) do
    task.spawn(function()
        ContentProvider:PreloadAsync({Asset})
    end)
end

so I’m just wondering if there are any issues that could arise from doing something like this for preloading assets.

ContentProvider:PreloadAsinc() accepts an ARRAY of assets. So what you are essentially doing is counter productive. Just insert the whole table into the function.

task.spawn(function()
 ContentProvider:PreloadAsync(Assets)
end)

But yes, using task.spawn() is a solution, if you do not really care about when the preloading is done. Unless you want to have a loading screen.

task.spawn() is like multi-threading. It’s like multi-tasking so the script can keep going while the process is processing.

Let’s say you wan’t to load something like assets (which is what you are doing). If you don’t use task.spawn(), the script has to wait until everything has done loaded. Using task.spawn(), you could do anything while the assets is loading like making an background animation or a progress bar.

Also you can just slap all the assets in to one PreloadAsync function. I wouldn’t recommend making a table just to hold one item.

Edit: accidentally pasted a code here that is supposed to go on a different thread. You would see it if you click the pencil symbol.

It Depends on your usage, If you are concerned about Multi Tasking, which is this:

task.spawn(function()
    for _,v in Array do
        ContentProvider:PreloadAsync{v}
    end
end)

It shouldn’t cause any type of issues unless you are firing something too many times or firing something that takes time to process by the script.

If you are refering to Immediatley firing the thread within the loop itself, which is this:

for _,v in Array do
    task.spawn(function()
        ContentProvider:PreloadAsync{v}
    end)
end

I wouldn’t really recommend it as it would basically have everything running at the exact same time instead of one at a time, which doesnt seem like a good usage of it

Then that would effectively remove the Usage of a Loading Screen, If you are just going to have a Loading Screen as a Detail rather than a functional component of a game, that doesnt sound like a good usage, nor a useful one.

How would I go about knowing the percentage of assets loaded if I were to use one PreloadAsync function?

Typically when iterating through a table, the first variable (which is the one you have covered with a underscore), would tell you what number the Iteration is on, which would mean a specific object, we can use that to determine how much is finished compared to the number of items within the table, like so:

local FullAmount = #Array -- can be a large Number
for index,v in Array do
    task.spawn(function()
        ContentProvider:PreloadAsync{v}
    end)
    print(`{index}/{FullAmount}`) -- Ex: 200/5000
    task.wait() -- so it doesnt time out
end

But that wouldn’t be a percentage, to make it a percentage you would need to do this:

print(math.floor(index / FullAmount * 100))

Which should make it a percentage

That’s similar to what I’m currently doing, except I made it increment a number each time an asset is loaded as shown below

local AssetsToLoad, AssetsLoaded = #Assets, 0
for Index, Asset in ipairs(Assets) do
    task.spawn(function()
        ContentProvider:PreloadAsync({Asset})
        AssetsLoaded += 1
    end)
end

I’ll probably add a wait in that loop so it doesn’t time out or something, though.