Jump to content




Sending Ids Between Turtles And Computers


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

#1 CyborgAlex

  • Members
  • 9 posts

Posted 30 July 2013 - 12: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

#2 Jappards

  • Validating
  • 116 posts
  • LocationHolland

Posted 30 July 2013 - 12: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))


#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 30 July 2013 - 12:41 PM

View PostJappards, on 30 July 2013 - 12:22 PM, said:

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


#4 CyborgAlex

  • Members
  • 9 posts

Posted 30 July 2013 - 01: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?

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 30 July 2013 - 01:04 PM

View PostCyborgAlex, on 30 July 2013 - 01:00 PM, said:

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.

View PostJappards, on 30 July 2013 - 12:22 PM, said:

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.

#6 CyborgAlex

  • Members
  • 9 posts

Posted 30 July 2013 - 01: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


#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 30 July 2013 - 01: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)

#8 CyborgAlex

  • Members
  • 9 posts

Posted 30 July 2013 - 01:28 PM

OH thanks.You helped me a lot





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users