As I’m trying to learn more about programming I have been stuck on how to use coroutine I have tried learning about coroutine but, I’m too stupid to learn it in the ROBLOX dev website
So… can anyone explain coroutine in a simple way?
As I’m trying to learn more about programming I have been stuck on how to use coroutine I have tried learning about coroutine but, I’m too stupid to learn it in the ROBLOX dev website
So… can anyone explain coroutine in a simple way?
It allows you to do 2 things at the same time.
struggles to think of a actual example
Lets say that whenever a player joins the game and for whatever magical reason we need to load their inventory before anything else which takes a long time due to other magical reasons.
Well everything else doesn’t care about the inventory so we can run the inventory loading and everything else simultaneously using a coroutine.
-- example
local inventoryCoroutine = coroutine.create(function()
-- load the inventory(takes long time bc magical reasons)
end)
coroutine.resume(inventoryCoroutine)
-- continue doing other stuff while the inventory is loading
If we didn’t use a coroutine here everything else would have to wait for the inventory to load before doing anything and since that takes a long time the player would have to wait a while for their data to load just bc the inventory takes a long time.
Mya has basically explained it already
You can run multiple parts of a script at the same time
you could even just do
coroutine.resume(coroutine.create(function()
-- Load the inventory
end))
or even
function LoadInv()
-- Load Inventory
end
coroutine.resume(coroutine.create(function()
LoadInv()
end))
It doesn’t matter how you do it, as long as you have the coroutine.resume and the coroutine.create
if you only have 1 it won’t work, at least to my knowledge
coroutine.resume(coroutine.create(LoadInv))
Coroutine has the ability to run functions without yielding (pausing) the rest of the code in the script.
Coroutine Functions and States (I don’t know much about coroutine but I’ll try my best here):
Functions
coroutine.create(<function>)
: Creates a new coroutine.coroutine.resume(<courotine>)
: Starts or resumes the given coroutine’s function.coroutine.running()
: Returns the currently running coroutine.coroutine.status(<courotine>)
: Returns the status of the given coroutine as a string. It can be in four states: running, suspended, normal, ordead
. See below for more infocoroutine.wrap(<courotine>)
: Same thing ascreate
, but can return a function that resumes the coroutine each time it is called.coroutine.yield(<courotine>)
: Pauses the coroutine function.
States
running
: The coroutine is currently runningsuspended
: The coroutine is yielded and is waiting to be resumed.normal
: The coroutine is active but not runningdead
: The coroutine has been forcefully yielded (resumed or thrown an error) and can no longer be used.
More info:
https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine
Hey, can i know, what’s the difference between spawn(function() and coroutine.resume(coroutine.create())?
I’m just confusing what’s better to use
spawn(function() isn’t reliable it has a wait until coroutines, coroutines spawn immediately while spawn(function() may take up to 25 seconds to run.
thank you for information! Always was confused
I’m stupid, what’s the differences/actual use cases between parallel lua and coroutines? I’m told they both allow things to run at the same time…?
@anthropomorphic_dev has already struggled to give examples.
I can give 3, but in general, when you are waiting for something, task.wait
uses yield
so your co-routines will give other, non-dependent co-routines the chance to run, and if they can’t, then they can all wait at the same time, saving time.
Here is an example of sequencing without co-routines:
all the waiting and idleness is additive, which is exactly what co-routines help avoid. Waiting for gravity is if you want to know the settled position of a part, so you just drop it from a bit higher and wait a second. This could be a solution for a great many parts, which would cause considerable delay. Hence co-routines.
Loading assets will incur a delay, but probably not that much, and with no explicit task.wait
operation, we’re at the mercy of the implementers to yield and allow our other co-routines to run.
Waiting for a download will typically involve idleness, or a call back. The call back is non-blocking anyway, but this is just an example I’m also struggling to give you, rather than, you know just 3 parts falling from the sky.
By wrapping each function in a co-routine the waiting is shared, parallelised. No computation is parallelised, that is where we could investigate threads.
whats the difference between coroutines and spawn functions?
I personally use task library more than coroutine but here are more exampes for threads:
task library is a easier way to manage coroutines (threads) and its more recommended the only reason I can think the use of coroutines is using the main thread like here:
local mainThread = coroutine.running()
coroutine.yield() --yield the script–somewhere else in the script
coroutine.resume(mainThread)
I personally cannot see a other use
speaking about task here are useful stuff about it:
task.spawn() it accepts function or a thread as the first argument unlike the deprecated spawn() function it doesnt wait to execute
task.spawn(function()
–do something
end)
task.defer() it accepts function or a thread as the first argument but this unlike task.spawn() doesnt run immediately. Uses are when you want to make a thread but dont want to run it first than everything
task.defer(function()
–do something
end)
task.cancel() it kills the thread and blocks it from running
so the uses of threads other than running independently from the main thread and you can cancel them very easily
–somewhere in the script:
local helloThread = task.spawn(function()
task.wait(2)
print(“Hello”)
end)–somewhere other in the script:
–something happened and you need to cancel it safely rather than using if statements
task.cancel(helloThread)
basically more control over functions, very useful if you try to use it more often
sorry if my english is bad its my second language