Een coroutine is een programmeertool waarmee je functies op bepaalde punten kunt pauzeren en hervatten. In tegenstelling tot normale functies die direct doorlopen, stoppen coroutines op aangewezen pauzes en gaan later verder. Deze functie is nuttig voor het beheren van taken die over een langere tijd moeten plaatsvinden zonder het hele programma te stoppen.
Beschrijving | |
---|---|
Yield | De operatie die wordt gebruikt binnen een coroutine om de uitvoering ervan te pauzeren, zodat andere code kan draaien. Het stopt de coroutine tijdelijk en slaat de status ervan op. |
Resume | De operatie die een coroutine hervat vanaf het punt waar het voor het laatst is gepauzeerd, en de uitvoering voortzet. |
Status | Een functie om de huidige status van een coroutine te controleren, zoals of deze is onderbroken, actief is of voltooid (gestopt). |
Wrap | Een wrapperfunctie die een bepaalde coroutine hervat wanneer deze wordt aangeroepen en alleen de gepauzeerde waarden retourneert. |
Voorbeeld: 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
Voorbeeld: Gebruik van 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
Als u deze tutorial nuttig vond en mijn werk wilt ondersteunen, overweeg dan om mij een kop koffie te kopen.
Hartelijk dank voor uw steun!
Koop een kop koffie voor mij