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

[Rednet question] Checking if ID exists?

Started by remiX, 30 October 2012 - 05:08 PM
remiX #1
Posted 30 October 2012 - 06:08 PM
Hey guys, just wanted to know if there is an easier way to check if the ID of a computer of a inputted number actually exists than what I have:

Main PC:

Spoiler

tMsgs = {
["PCFound"] = "Connected to PC with ID: ",
["PCNotFound"] = "Unable to connect to PC with ID: ",
}


function cls(x,y)
    term.clear()
    term.setCursorPos(x,y)
end

rednet.open("top")

while true do
    repeat
        cls(1,1)
        write("Enter the ID of the computer you would like to interact with: ")
        cID = tonumber(read())
    until type(cID) == "number"
    print("\nAttemping to connect to PC with ID: "..cID)
    sleep(3)
    rednet.send(cID,"test")
    os.startTimer(0.1)
    event, sID, msg, dist = os.pullEvent()
    cls(1,1)
    if event == "timer" then
        print(tMsgs["PCNotFound"]..cID)
    elseif event == "rednet_message" then
        if msg == "true" then
            print(tMsgs["PCFound"]..cID)
        end
    else print("Event: "..event)
    end
    sleep(3)
end

Receiver PC:

Spoiler

function cls(x,y)
    term.clear()
    term.setCursorPos(x,y)
end

rednet.open("top")

while true do
    cls(1,1)
    print("Waiting for a rednet message...")
    event, rID, msg, dist = os.pullEvent("rednet_message")
    if msg == "test" then
        cls(1,1) print("Received message: "..msg.."\nSending message: true to PC with ID: "..rID)
        rednet.send(rID, "true")
    end
end

This does work nicely but it isn't that ethical.
ChunLing #2
Posted 30 October 2012 - 06:28 PM
…ethical?

The simplest way to check if a computer is ready to send/receive rednet is to send it something and get a response. That seems to be the basis of what you're doing here.
remiX #3
Posted 30 October 2012 - 06:52 PM
Oh… lol thought there was an easier way :/
ChunLing #4
Posted 30 October 2012 - 07:16 PM
Not that I know. The problem is that you need to know that the computer exists and can be contacted by rednet and is in range and will do something if you contact it. Oh, you only wanted the first of those. Hmm…well, not from within a computer on ComputerCraft. Cause that would be…kinda cheaty, don't you think?
remiX #5
Posted 30 October 2012 - 07:50 PM
:P/>/>