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

[Fixed] os.pullEvent problems in coroutines?

Started by Awesome As, 05 June 2013 - 06:15 PM
Awesome As #1
Posted 05 June 2013 - 08:15 PM
Hello everyone!

While I was trying to make a fancy game using an advanced ComputerCraft computer, I found a problem that seems quite weird, and I don't really know what might be the cause of this or how to solve it.

Basically, when I'm trying to do os.pullEvent("mouse_click") inside a coroutine in a function I have (the coroutine is to make my custom input fields work while still have a "back" button), os.pullEvent doesn't seem to trigger. Outputs before the line works, but not after. No output, and if I remove the coroutine, it works, but the input fields will no longer work then.

The problematic line is line 125, feel free to try it out… :P/>


Link to program code:
http://pastebin.com/vpQsn5iD


Thank you,
- As
Bomb Bloke #2
Posted 05 June 2013 - 08:32 PM
My bet is that the event is getting eaten before the coroutine can chew on it by the "readInput()" statements a few lines down. Turns out lots of commands pull events (whether they're documented as doing so or not is an entirely different story…), and those that do, tend to just discard most of what they get and keep on pulling until they get what they're interested in.

I'm not sure of the best way to deal with that here. I had great fun working around this problem while trying to pull timer events and run turtle functions (event eaters) at the same time in this thread. Though it does dribble on a bit you may find something of use there.
ElvishJerricco #3
Posted 05 June 2013 - 09:11 PM
You're just wrapping your coroutine and running it once. That's not how it works. Coroutines wrap a function and allow you to hold onto it after it yields. You're never resuming your function. Look at the official coroutines tutorial.
Bubba #4
Posted 05 June 2013 - 09:22 PM
I've written a pretty in-depth tutorial on coroutines specifically for ComputerCraft here. It talks about os.pullEvent and its interaction with coroutines more there.

Here's a shortened version though:
os.pullEventRaw == coroutine.yield
os.pullEvent == os.pullEventRaw + some additional features like termination
Awesome As #5
Posted 06 June 2013 - 04:23 AM
Hm, I've been working with coroutines and whatever in a modified version of Lua (more specifically Rbx.Lua, I don't think there's any changes to coroutines… only functions like wait() (os.sleep), tick() (unix time) ect), so I'm still a bit confused about coroutine.yield.

Well, Bubba's respond seems to fit into what I've already gathered of information yesterday *yawn*
And Elvish, coroutine.wrap(somefunction)() is in fact just like coroutine.resume(coroutine.create(somefunction)), but it can error (which resume(create() doesn't do in Rbx.Lua :/). Also, I did get output from within the coroutine (output: T1).

I'll go put the back-button into the custom input fields though :/

- As