-snip-
Bear with me here, I am a non-native English speaker. I tried reading the whole document again, which is almost the same as the post on CC forums, and I still don't get what I am doing wrong here.
That's fine. I admit it probably sounds more confusing if you didn't write it. Here's a more basic overview without the complicated code:
Resuming a Coroutine - ArgumentsWhat happens when you call coroutine.yield? In simple terms, the function (or block of code) currently running will stop until it can be resumed with coroutine.resume. But that's not the full story. coroutine.yield is quite handy in that you can get variables into and out of the running function simple by yielding. Let's look at an example:
local function resumeMe()
print("Yielding now...")
local a, b, c = coroutine.yield()
print("The return values of coroutine.yield are: ")
print(a .. "\t" .. b .."\t" .. c)
end
local co = coroutine.create(resumeMe)
coroutine.resume(co) --#We have to start our coroutine first - this will print "Yielding now..."
coroutine.resume(co, "arg1", "arg2", "arg3")
coroutine.status(co) --# The status is 'dead' because our function has finished
So what would you guess the output of this code would be? If you guessed "arg1 arg2 arg3", you'd be right. So what's going on here? Basically, anything extra arguments you pass through the coroutine.resume function will be returned by coroutine.yield. But what if you wanted to get output from a coroutine? How would you do that?
Well in fact it's pretty easy: We just add arguments to coroutine.yield():
local function resumeMe()
print("Yielding now...")
local a,b,c = coroutine.yield("something1", "something2", "something3")
print("The return value of coroutine.yield is:")
print(a .. "\t" .. b .. "\t" .. c)
end
local co = coroutine.create(resumeMe)
local a, b, c, d = coroutine.resume(co)
print("The coroutine yielded and returned these values: ")
print(a .. "\t" .. b .."\t" .. c .. "\t" .. d)
coroutine.resume("hi", "hello", "bye")
So what happens here? Well it might not be quite what you'd expect because calling coroutine.resume(co) is going to first return a boolean which indicates whether there were any errors or if the coroutine is dead. In the case, our output would be something like this:
Yielding now…The coroutine yielded and returned these values:true something1 something2 something3The return value of coroutine.yield is:hi hello byeSo, if we wanted to get that third argument of coroutine.yield we would need to add one more variable.
Now onto ComputerCraft and eventsSo you've probably been wondering: How does os.pullEvent() factor into this? What happens when we call os.pullEvent(). Well if you were to open up bios.lua, you might find something rather surprising:
function os.pullEventRaw( _sFilter )
return coroutine.yield( _sFilter )
end
That's right. os.pullEvent actually is just calling coroutine.yield. This means that you can use os.pullEvent exactly like you would a coroutine: let's give it a shot.
local function getEvent()
local eventType, arg1, arg2 = os.pullEvent("key")
end
local co = coroutine.create(getEvent)
local status, eventFilter = coroutine.resume(co)
coroutine.resume(co, os.pullEvent(eventFilter))
And that's it!
I kind of wrote this in a rush, so apologies if there are mistakes or it's hard to understand.