Eine Coroutine ist ein Programmierwerkzeug, das es ermöglicht, Funktionen an bestimmten Punkten zu pausieren und wieder aufzunehmen. Im Gegensatz zu regulären Funktionen, die ohne Unterbrechung ausgeführt werden, pausieren Coroutinen an festgelegten Punkten und setzen ihre Ausführung später fort. Dieses Feature ist hilfreich für die Verwaltung von Aufgaben, die über einen längeren Zeitraum stattfinden, ohne das gesamte Programm anzuhalten.
Beschreibung | |
---|---|
Yield | Der Vorgang, der innerhalb einer Coroutine verwendet wird, um deren Ausführung zu pausieren und anderen Code auszuführen. Sie stoppt die Coroutine vorübergehend und speichert ihren Zustand. |
Resume | Der Vorgang, der eine Coroutine an dem Punkt wieder startet, an dem sie zuletzt pausiert wurde, und mit der Ausführung fortfährt. |
Status | Eine Funktion, um den aktuellen Zustand einer Coroutine zu überprüfen, z. B. ob sie pausiert, ausgeführt oder beendet (abgeschlossen) ist. |
Wrap | Eine Wrapper-Funktion, die eine bestimmte Coroutine wieder aufnimmt, wenn sie aufgerufen wird, und nur die angehaltenen Werte zurückgibt. |
Beispiel: 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
Beispiel: Verwendung von 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
Wenn Sie dieses Tutorial hilfreich fanden und meine Arbeit unterstützen möchten, können Sie mir gerne einen Kaffee spendieren.
Vielen Dank für Ihre Unterstützung!
Kaufen Sie mir einen Kaffee