Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ how to get the return value of a function called by coroutine ?

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

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Supertu   (4 posts)  Bio
Date Mon 09 Mar 2015 08:12 AM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 09 Mar 2015 08:40 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Mon 09 Mar 2015 08:43 AM (UTC)
Message
eg.


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



Or:


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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Supertu   (4 posts)  Bio
Date Reply #3 on Mon 09 Mar 2015 09:03 AM (UTC)
Message
oh,i ignore to see detail in document.

Got it! Thanks Very Much!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,115 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.