resume & yield
Part of the Logic & Flow section of Coddy's Lua journey — lesson 51 of 54.
In the previous lesson, you learned how a coroutine can pass values back to the main code using coroutine.yield(). But communication can flow in both directions. You can also send values into a coroutine when you resume it.
When you call coroutine.resume() with additional arguments after the coroutine object, those arguments are passed into the coroutine. Inside the coroutine, these values become the return values of the coroutine.yield() call that paused it.
Here's how it works:
function receiver()
print("Coroutine started")
local value = coroutine.yield()
print("Received: " .. value)
end
local co = coroutine.create(receiver)
coroutine.resume(co) -- Starts and pauses at yield
coroutine.resume(co, "Hello") -- Passes "Hello" into the coroutineWhen the coroutine resumes the second time, the string "Hello" is passed in and becomes the return value of coroutine.yield(), which gets stored in the value variable.
Challenge
EasyWrite a function createEchoCoroutine that takes initialMessage and returns a string showing the two-way communication between the main code and a coroutine.
Create a coroutine that first yields the initialMessage, then receives a response when resumed, and finally yields that response back. The function should demonstrate passing values both out of and into the coroutine.
Logic:
- Create a function that yields
initialMessageand stores the return value from that yield - After receiving the value, yield it back out
- Wrap this function in a coroutine using
coroutine.create() - Resume the coroutine the first time to get the initial message
- Resume the coroutine a second time, passing in
"Response" - Capture the value yielded from the second resume
- Return a string in the format:
First: [first_yield]\nSecond: [second_yield]
Parameters:
initialMessage(string): The message the coroutine will yield first
Returns: A string showing both yielded values separated by a newline (string). Format: First: Hello\nSecond: Response
Cheat sheet
You can send values into a coroutine when resuming it by passing additional arguments to coroutine.resume(). These arguments become the return values of the coroutine.yield() call that paused the coroutine.
function receiver()
local value = coroutine.yield() -- Pauses and waits for input
print("Received: " .. value)
end
local co = coroutine.create(receiver)
coroutine.resume(co) -- Starts coroutine, pauses at yield
coroutine.resume(co, "Hello") -- Passes "Hello" into the coroutineWhen the coroutine resumes the second time, "Hello" is passed in and becomes the return value of coroutine.yield(), which gets stored in the value variable.
Try it yourself
function createEchoCoroutine(initialMessage)
-- Write code here
end
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Table Iteration
Iterating with pairs()Iterating with ipairs()pairs() vs. ipairs()Recap - Character Sheet2More Table Library Functions
table.concat()table construction & unpack()table.sort()Custom Sorting with FunctionsRecap - High Score Board3Advanced Function Concepts
Returning Multiple ValuesVariadic Functions (...)Functions First-Class ValuesAnonymous FunctionsWhat is a Closure?Recap - Simple Event Handler9Coroutines for Beginners
What is a Coroutine?coroutine create & resumePausing with coroutine.yield()resume & yieldChecking Coroutine StatusRecap - Number GeneratorRecap - Vector Math