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

Sending Ids Between Turtles And Computers

Started by CyborgAlex, 30 July 2013 - 11:11 AM
CyborgAlex #1
Posted 30 July 2013 - 01:11 PM
Hello,I was wondering how i can send from a turtle id to a master computer(just a example) without knowing the computer`s ID.That`s because i want to make a strip minner program that involves,more turtles that dig a 3X3 area each, so I don`t want to rewrite the ID in the program every time if i would go play on another server,or just make a new world.
And another thing that doesn`t bother me that much,but i would like to know how to do it.The thing I would like to know is :Session persistance.
I hope you can help me!
Thanks!
Alex
Jappards #2
Posted 30 July 2013 - 01:22 PM

local side = "back" --asuming the modem is on the back
rednet.open(side)
ID = os.computerID()--this function will return the computer ID
rednet.broadcast(tostring(ID))
TheOddByte #3
Posted 30 July 2013 - 01:41 PM

local side = "back" --asuming the modem is on the back
rednet.open(side)
ID = os.computerID()--this function will return the computer ID
rednet.broadcast(tostring(ID))
Even better way to open modem sides :)/>

-- Opening modems
for _, side in pairs(rs.getSides()) do
   if peripheral.getType(side) == "modem" then
      rednet.open(side)
     end
  end
CyborgAlex #4
Posted 30 July 2013 - 02:00 PM
Ok,that should do it.But assuming that the person that uses the program has limited programing knowledge,is it possible to make the program that doesn`t need any changes?
theoriginalbit #5
Posted 30 July 2013 - 02:04 PM
Ok,that should do it.But assuming that the person that uses the program has limited programing knowledge,is it possible to make the program that doesn`t need any changes?
Yes indeed. Using the method that Hellkid98 posted it will automatically open all modems on the computer ready for transmission.


local side = "back" --asuming the modem is on the back
rednet.open(side)
ID = os.computerID()--this function will return the computer ID
rednet.broadcast(tostring(ID))
Well actually you could just do this


local side = "back"
rednet.open(side)
rednet.broadcast("PONG")

As when you receive a message you actually get the sender's id.

NOTE: I chose PONG because PING is used for the GPS system.
CyborgAlex #6
Posted 30 July 2013 - 02:15 PM
But with Jappards`s method,how would the mastercomputer that the id is from the right turtles.
With your method for the MC,would be very simple


event, p1, p2 = os.pullEvent("rednet_message")
if p2 == "PONG" then
do something
end
theoriginalbit #7
Posted 30 July 2013 - 02:24 PM
Turtle code:


--# open the modems
for _, side in pairs(rs.getSides()) do
  if peripheral.getType(side) == "modem" then
    rednet.open(side)
  end
end

--# the variable with the master computers id
local masterID

--# on first boot up (check this with a file, that you then create after this process)
if not fs.exists(".data") then
  --# request a master computer
  rednet.broadcast("Hey I want a master!")
  --# wait for a response
  local id, msg
  repeat
    id, msg = rednet.receive()
  until msg == "I am your master!"
  --# write that id out to file
  local h = fs.open(".data","w")
  h.write(id)
  h.close()
  masterID = id
else
  --# we know the master, read it
  local h = fs.open(".data","r")
  masterID = tonumber(h.readLine())
  h.close()
end

--# now just listen to only that computer and do what it says

Then the master computer would follow a similar structure to the above code, except the messages would be the reverse (waiting for the turtle message and sending a response, unless there is a save file with it's ID)
CyborgAlex #8
Posted 30 July 2013 - 02:28 PM
OH thanks.You helped me a lot