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

Having 2 things running at the same time in one program?

Started by cheekycharlie101, 04 December 2012 - 08:44 AM
cheekycharlie101 #1
Posted 04 December 2012 - 09:44 AM
ok, so i wanna make a rednet irc. i know people already have but i want to have a go my self. thing is i cant figure out how to have it open to type text in and been open to be receiving rednet messages at the same time.i have thought about doing this in many different ways but none have been successful. i figure i have to move on from:

senderId, message, distance = rednet.receive()
and start using

event, id, message = os.pullEvent("rednet_message")
any ideas? thanks guys in advance
Lyqyd #2
Posted 04 December 2012 - 09:48 AM
IRC-type chat is basically done by modifying read() to handle rednet messages, so you may wish to start by reading through it and performing basic handling of the rednet_message event in a copy of it.
cheekycharlie101 #3
Posted 04 December 2012 - 09:54 AM
IRC-type chat is basically done by modifying read() to handle rednet messages, so you may wish to start by reading through it and performing basic handling of the rednet_message event in a copy of it.
how do you mean? like taking a rednet message into a varible to print it?
GopherAtl #4
Posted 04 December 2012 - 10:34 AM
He means you'll basically have to implement your own version of the read function which will take in a char at a time, draw them at the end of the input area, keep track of the cursor position, etc. The read function is, at heart, pullEvent loop, so you would extend it to add handlers of your own for rednet_messages and any other events your program handles, and print any messages sent by other computers to the screen above the input line where you're reading.

The only other option is to use coroutines, but that wouldn't really be any easier, as to call read() in one coroutine and not disturb it printing from another coroutine, you'd need to buffer everything on screen and handle drawing and scrolling the chat log part in your own code. I'd say it'd be a bit harder, actually, to get it really working right.
anonimo182 #5
Posted 04 December 2012 - 10:36 AM
look for the function read() in bios.lua, copy it and add a line to check for rednet messages

Edit: Ninj'd
KaoS #6
Posted 04 December 2012 - 08:52 PM
I prefer the coroutine method to be honest with you, do you know how to use coroutines?
cheekycharlie101 #7
Posted 05 December 2012 - 06:36 AM
No, i have never heard of them:P there is still a lot of things i dont know about in lua. which is why i keep making posts so i can learn more. could you explain them to me?
KaoS #8
Posted 05 December 2012 - 10:09 AM
Well they are very complicated and I fear I am not good enough at explaining them, especially as my brain is not listening at the moment, it wants to sleep. take a look at this tutorial and then ask about anything you do not understand, hopefully by then I will have recuperated