21 posts
Posted 02 April 2012 - 05:15 AM
I'm having a bit of trouble with this code and can't seem to figure out why. Based on my understanding of what I've coded here, the system should listen for rednet messages sent to it and if it receives one, it should print that message. However, when this program runs, it only seems to receive every other rednet message and I can't figure out why.
rednet.open("back")
print()
print("Waiting for authentication requests...")
while true do
event = os.pullEvent()
if event == "rednet_message" then
id = rednet.receive()
print("Authentication request received from: " .. id) shell.run("time")
print()
end
end
718 posts
Location
Hawaii
Posted 02 April 2012 - 05:25 AM
Try:
rednet.open("back")
print("Waiting for authentication requests...")
while true do
id, message = rednet.receive()
print("Authentication request receive from: " .. id) shell.run("time")
end
454 posts
Location
London
Posted 02 April 2012 - 05:29 AM
I'm having a bit of trouble with this code and can't seem to figure out why. Based on my understanding of what I've coded here, the system should listen for rednet messages sent to it and if it receives one, it should print that message. However, when this program runs, it only seems to receive every other rednet message and I can't figure out why.
rednet.open("back")
print()
print("Waiting for authentication requests...")
while true do
event = os.pullEvent()
if event == "rednet_message" then
id = rednet.receive()
print("Authentication request received from: " .. id) shell.run("time")
print()
end
end
What's happening is:
You pull an event with os.pullEvent, but you don't save the computerID and message you'll get from it (os.pullEvent returns the event, and any parameters associated with it).
Example:
local event, param1, param2 = os.pullEvent()
print(event)
print(param1)
print(param2)
Then, you pull another rednet event with rednet.recieve(), and use it's ID and message to write stuff.
21 posts
Posted 02 April 2012 - 05:39 AM
I understand. Thank you both for your quick replies and explanations. Very much appreciated.