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

[Lua][Question] Problem using Parallel API

Started by Retriever, 29 December 2012 - 04:04 PM
Retriever #1
Posted 29 December 2012 - 05:04 PM
Hello,
to learn how to do more in Lua i've set myself the task of making a simple mesaging system using Rednet.
So far i've got the modem setup and username creation parts sorted but i'm completely stuck on actually sending and receiving the messages.
I need the computer to both wait for new messages coming in from rednet.receive() and wait for new messages to be sent out with read() but i can't seem to find a way to get them both running at the same time.

I'm trying to use the Parallel API but when i try it the system just starts the first function (either send or receive) and then pauses as the function waits for either a rednet message or user input and then alternates to the other function once it gets a response.

http://pastebin.com/BgUT2Q5K

How can i fix my code so that it lets you type new messages to be sent whilst waiting for incoming messages to print?
Thanks
Lyqyd #2
Posted 29 December 2012 - 09:56 PM
Change your parallel call:


parallel.waitForAny(MessageSend,MessageReceive)

Removing the parentheses like this passes the functions to the parallel API instead of calling them and trying to pass the results. This way, the parallel API can interlace your functions properly, like you want it to!
ChunLing #3
Posted 30 December 2012 - 12:45 AM
Huh. Why doesn't the parallel function error on receiving a nil value as an argument? Or at least drop execution of that and do the next argument (that isn't nil).
zekesonxx #4
Posted 30 December 2012 - 02:44 AM
Because putting in failsafes is harder than telling programmers the APIs.
Retriever #5
Posted 30 December 2012 - 06:11 AM
Change your parallel call:


parallel.waitForAny(MessageSend,MessageReceive)

Removing the parentheses like this passes the functions to the parallel API instead of calling them and trying to pass the results. This way, the parallel API can interlace your functions properly, like you want it to!

Thankyou, works now :)/>