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

Rednet Conversations

Started by shiskebab, 31 July 2014 - 10:41 PM
shiskebab #1
Posted 01 August 2014 - 12:41 AM
Title: Rednet Conversation

Hey, all you people with spectacularly more knowledge about Lua and Computercraft

I am Finished (finally) writing my turtle program.

Anyway, instead of having to write the computer 'id' it will be talking to. Into every rednet.send() and receive command

I made a function that asks the user to input the computers id here:


function Startup()
term.clear()
  term.setCursorPos (1,1)
  print ("   this turtle's id: ", os.getComputerID())
  print ("   Set this turtle's label:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   Set The Computer Terminal's ID")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  term.setCursorPos(5,4)
  local IgnLabel = read()
  os.setComputerLabel(IgnLabel)
  term.setCursorPos(5,4)
  print ("Label successful")
  term.setCursorPos(5,9)
  local CompId = read()
  term.setCursorPos(5,9)
  print ("ID Set")
  sleep(0.5)
  Middle()
  sleep(2)
end


And yes, i did, kinda originally take the outlay from a much more talented person. But he had such good ideas!

The thing is if i put a tonumber in the above like this

 local CompId = read() 

To

 local CompId = tonumber(read()) 

Gives me an error like "calling nil"

And so i put the tonumber() in the rednet.send(). Only problem is that now the turtle doesn't recognize an incoming signal

Code for the turtle when it is waiting for the signal:


function RednetStasis()
  term.clear()
  term.setCursorPos (1,1)
  print ("turtle is in receive mode")
  print ("go to your terminal to start a dig cycle")
  while true do
	rednet.open("right")
   id, message = rednet.receive()
   if id == CompId and message == "start" then
   rednet.close("right")
   Sixcolumnstr()
   break
   end
end
end

Oh and also. Can i use the turtles label as an id? Like: rednet.send(Digger1, "start")
Neywiny #2
Posted 01 August 2014 - 03:50 AM
can you please post the whole, entire, unedited code? it looks complete but without the whole code, and the error line, nothing can be done. what catches my eye is the function Middle(), which while isn't the problem as it's called correctly as far as I see, I can't know what the problem is. Also, try string.tonumber(read())
shiskebab #3
Posted 01 August 2014 - 10:50 AM
the entire code is 502 lines. It mostly consist of slightly alternate and different repeating functions. Nevertheless, I will uppload it to pastebin momentarily!
shiskebab #4
Posted 01 August 2014 - 11:05 AM
I have tested all the Menu functions like; Middle(), Startup(), RednetStasis() and Moving()

And all of them work, except for RednetStasis() which does nothing when I send it rednet signals.


Entire code: http://pastebin.com/DfKjTb23
Edited on 01 August 2014 - 09:06 AM
shiskebab #5
Posted 01 August 2014 - 03:58 PM
so I started tinkering with the rednet side of the script.

i created two dummy programs which are independent but use the same framework:

Sender:

local function Startup()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   Set receiving computer's id:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   message:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  term.setCursorPos(5,4)
  compid = tonumber(read())
  term.setCursorPos(5,4)
  print ("id saved")
  term.setCursorPos(5,9)
  local message = read()
  term.setCursorPos(5,9)
  print ("message saved")
  sleep(0.5)
  send()
  sleep(2)
end

function send()
  term.clear()
  term.setCursorPos (1,1)
  print ("sending message in: ")
  print ("3")
  sleep(1)
  print ("2")
  sleep(1)
  print ("1")
  rednet.open("right")
  sleep(1)
  rednet.send(compid, message)
  rednet.close("right")
  sleep(1)
  print ("message sent")
  sleep(1)
  print ("press any key to send backup message")
  local event = os.PullEvent("key")
  rednet.open("right")
  sleep(1)
  rednet.send(1, "hei")
  rednet.close("right")
  print ("messageb sent")  
  sleep(1)
  Startup()  
end

Startup()

receiver:

local function Startup()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   Set the sending computers id:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  term.setCursorPos(5,4)
  compid = tonumber(read())
  term.setCursorPos(5,4)
  print ("id saved")
  sleep(2)
  meh()
end


function meh()
  term.clear()
  term.setCursorPos (1,1)
  print ("   this Computer's id: ", os.getComputerID())
  print ("   waiting for message:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")
  print ()
  print ("   Terminal:")
  print ("   +--------------------------+   ")
  print ("							  |   ")
  print ("   +--------------------------+   ")

  while true do
  rednet.open("right")
  senderID, message = rednet.receive()
  if senderID == compid then
  rednet.close("right")
  term.setCursorPos (4,4)
  print ("message: ", message)
  term.setCursorPos (4,9)
  print ("press any key to continue")
  local event = os.pullEvent("key")
  Startup()
  break
  end
  end
end

Startup()

I've been going back and forth with this and found that all the computer sends is a nil value. Which explains why i never got a message on the receiving computers screen :o/>

anyone know why my "sending" script doesn't send a message ?
Edited on 01 August 2014 - 02:00 PM
shiskebab #6
Posted 01 August 2014 - 04:27 PM
I do not how but i fixed it.

the finished code is here: http://pastebin.com/gtgP12Ry

I am giving anyone permission to use it in any way they see fit
Edited on 01 August 2014 - 02:27 PM