This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

Coroutines & tArgs?

Started by KingofGamesYami, 26 November 2014 - 03:26 PM
KingofGamesYami #1
Posted 26 November 2014 - 04:26 PM
I'm currently working on multitasking.

How would you pass arguments to a coroutine? You can't call the function with arguments, and resuming it will wait for coroutine.yeild, not the arguments. Should I simply add an 'in between' function that handles this?
Exerro #2
Posted 26 November 2014 - 05:10 PM
You pass arguments to a coroutine through coroutine.resume. If it is the first time resuming the coroutine, the args you resume it with are passed in as if you were calling the original function with those args. If you have already resumed it, then it must have just called coroutine.yield() for you to be running any code, and resuming it will cause whatever you resume it with to be returned by the coroutine.yield() called from the coroutine.
KingofGamesYami #3
Posted 26 November 2014 - 05:17 PM
So, just to be clear…

local co = coroutine.create( function( ... ) print( textutils.serialize( { ... } ) ) end )
coroutine.resume( co, "hello" )

Will print

{
  Hello
}
Lyqyd #4
Posted 26 November 2014 - 05:19 PM
Technically, no (capitalization & table formatting are off). But close enough, yeah. :P/>