Jump to content




[Lua][Question] Problem using Parallel API


  • You cannot reply to this topic
4 replies to this topic

#1 Retriever

  • Members
  • 7 posts
  • LocationUK

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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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!

#3 ChunLing

  • Members
  • 2,027 posts

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).

#4 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

Posted 30 December 2012 - 02:44 AM

Because putting in failsafes is harder than telling programmers the APIs.

#5 Retriever

  • Members
  • 7 posts
  • LocationUK

Posted 30 December 2012 - 06:11 AM

View PostLyqyd, on 29 December 2012 - 09:56 PM, said:

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 :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users