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

Communication Between Multiple Computers

Started by PSI_Starman, 29 September 2012 - 03:37 AM
PSI_Starman #1
Posted 29 September 2012 - 05:37 AM
I have never written more than 2 succeeding lines of code in a row before in any language, so despite my wishes to become experienced at programming… I'm not there yet. Or on that road. But, I'm trying to make it so, in my Tekkit game of 4 people, I can send and receive messages between all those computers, connected wirelessly. Additionally, I want a monitor that is connected via cables to display words with the print command, as well. Unfortunately, I know nothing more than where to start.

To receive messages instantly whenever sent,, would I be typing in "do rednet.receive = loop end", minus the quotes?

How would I give messages to all the other computers at once?

To anyone who could answer, thanks. :P/>/>
KaoS #2
Posted 29 September 2012 - 07:20 AM
essentially sending a message is done with rednet.send(computerID, message) to send to one computer and rednet.broadcast(message) to send to all computers in range
to receive a message we use rednet.receive(timeout) or os.pullEvent('rednet_message')

DO NOT FORGET TO ACTIVATE THE MODEM: rednet.open(modemSide)

a basic implementation would be:
computer1

rednet.open('right')
rednet.broadcast('this is a test message')

computer2

rednet.open('right')
while true do
  local id,msg,dist=rednet.receive()
  print('the message ''..msg..'' was received from ID '..id)
end
–the while true loop is the correct way to make it keep listening for input

printing to monitors is fairly simple but printing to a monitor and to the terminal is not so easy for beginners so I will give you a code

local temp={}
for k,v in pairs(term) do
  temp[k]=function(...) v(...) term.native[k](...) end
end
term.redirect(temp)

that should (hopefully :P/>/>) make everything appear on the monitor and on the terminal
PSI_Starman #3
Posted 29 September 2012 - 05:07 PM
Thank you very much! When the server starts up again I'll be sure to try it out!

:)/>/>
PSI_Starman #4
Posted 29 September 2012 - 07:36 PM
Hmm… how would I make a computer constantly loop something, but still do something else?

(How would I make the computer listen for messages forever, but still let me send them?)
Anonomit #5
Posted 29 September 2012 - 07:40 PM
The parallel API
http://computercraft.info/wiki/index.php?title=Parallel_%28API%29

There's likely a tutorial on how to use it in the tutorials section.
KaoS #6
Posted 30 September 2012 - 07:21 AM
put each loop in a function, then define coroutines and loop start them


rednet.open('right')
local function loop1()
  while true do
    local id,msg,dist=rednet.receive()
    print('the message ''..msg..'' was received from ID '..id)
  end
end
local function loop2()
  --your other code
end
local tThreads={coroutine.create(loop1);coroutine.create(loop2)}
local events={}
while true do
  for k,v in pairs(tThreads) do
    coroutine.resume(v,unpack(events))
    events={os.pullEvent()}
  end
end