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

[LUA] Issue with Code

Started by Bandus, 02 April 2012 - 03:15 AM
Bandus #1
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
ComputerCraftFan11 #2
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
Advert #3
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.
Bandus #4
Posted 02 April 2012 - 05:39 AM
I understand. Thank you both for your quick replies and explanations. Very much appreciated.