how to get the return value of a function called by coroutine ?

Posted by Supertu on Mon 09 Mar 2015 08:12 AM — 4 posts, 15,921 views.

#0
co=coroutine.create(
function (str)
return str
end)

print(coroutine.resume(co,"hello world"))


Although the above code can show "true hello world",I can not
obtain the return value when I typed

"local x=coroutine.resume(co,"hello world") print(x)"

and Var 'x' is just printed "true".

How should I gain the return value from coroutine just like normal function?

I know using Globle variable is also a solution ,but I don't like to do it because I want to keep concision in code.

I appreciate if someone can show me a way.
Australia Forum Administrator #1
See the documentation:

Quote:

On success, returns true, followed by arguments to the coroutine.yield inside the function (if called), or the return value of the function itself.


The first result is true, the following results are the results of the yield.
Australia Forum Administrator #2
eg.


local x, y = coroutine.resume(co,"hello world") 
print(y)



Or:


y = assert (coroutine.resume(co,"hello world"))
print(y)
#3
oh,i ignore to see detail in document.

Got it! Thanks Very Much!