A coroutine is a programming tool that lets you pause and resume functions at certain points. Unlike regular functions that run straight through, coroutines stop at designated pauses and continue later. This feature is helpful for managing tasks that need to happen over time without stopping the entire program.
Description | |
---|---|
Yield | The operation used within a coroutine to pause its execution, allowing other code to run. It temporarily stops the coroutine and saves its state. |
Resume | The operation that restarts a coroutine from the point where it last yielded, continuing with its execution. |
Status | A function to check the current state of a coroutine, such as whether it is suspended, running, or dead (completed). |
Wrap | A wrapper function that will resume a particular coroutine when it is called and will return only the yielded values. |
Example: Coroutine
local function countingCoroutine()
for i = 1, 3 do -- Count from 1 to 3
coroutine.yield("Count: " .. i) -- Yield the count value
end
return "Counting completed." -- Return a final message when done
end
-- Create the coroutine from the function
local co = coroutine.create(countingCoroutine)
-- Start the coroutine and check the status and results at each step
print("Starting coroutine...")
print("Status before start:", coroutine.status(co)) -- Initially, status should be 'suspended'
local success, result = coroutine.resume(co) -- Resume for the first count
print(success, result) -- Should print true, "Count: 1"
print("Status after first count:", coroutine.status(co)) -- Should still be 'suspended'
success, result = coroutine.resume(co) -- Resume for the second count
print(success, result) -- Should print true, "Count: 2"
print("Status after second count:", coroutine.status(co)) -- Should still be 'suspended'
success, result = coroutine.resume(co) -- Resume for the third count
print(success, result) -- Should print true, "Count: 3"
print("Status after third count:", coroutine.status(co)) -- Should still be 'suspended'
success, result = coroutine.resume(co) -- Resume for the final return
print(success, result) -- Should print true, "Counting completed."
print("Status after completion:", coroutine.status(co)) -- Should now be 'dead'
-- Check status after completion to confirm no further actions can be taken
success, result = coroutine.resume(co)
print(success, result) -- Should print false and possibly an error message
print("Status after attempting to resume a completed coroutine:", coroutine.status(co)) -- Should be 'dead'
Roblox Studio
Example: Using coroutine.wrap
local function countToThree()
for i = 1, 3 do
coroutine.yield("Count: " .. i) -- Yield with a return value
end
return "Counting completed." -- Return a final message when done
end
local wrappedCoroutine = coroutine.wrap(countToThree)
print("Starting coroutine...")
local result
result = wrappedCoroutine() -- Executes and yields "Count: 1"
print(result) -- Prints "Count: 1"
result = wrappedCoroutine() -- Resumes and yields "Count: 2"
print(result) -- Prints "Count: 2"
result = wrappedCoroutine() -- Resumes and yields "Count: 3"
print(result) -- Prints "Count: 3"
result = wrappedCoroutine() -- Ends the counting, returns "Counting completed."
print(result) -- Prints "Counting completed."
result = wrappedCoroutine() -- Error
Roblox Studio
If you found this tutorial helpful and would like to support my work, please consider buying me a coffee.
Thank you very much for your support!
Buy me a coffee