Difference between revisions of "Receiving a rednet message through os.pullEvent()"

From ComputerCraft Wiki
Jump to: navigation, search
(Explanation)
(Undo revision 2179 by 188.143.232.12 (talk))
Line 12: Line 12:
 
  end
 
  end
  
What a pleasure to find soemone who identifies the issues so clearly
+
== Explanation ==
 +
rednet.open("Direction_of_modem")
 +
Here we just open the modem.
 +
 
 +
while true do
 +
We start a loop because os.pullEvent() terminates itself after 10 seconds.
 +
 
 +
event, id, text = os.pullEvent()
 +
We wait for an event to occur and when this happens, we store the event's info into 3 variables.
 +
NOTE: You can give your variables whichever name you want.
 +
 
 +
if event == "rednet_message" then
 +
We check if the event was a rednet message.
 +
 
 +
print(id .. "> " .. text)
 +
We print the message and the id of the sender.
 +
 
 +
end
 +
end
 +
We close our previous statements.
 +
 
 +
 
 +
Note: From my research, I didn't find a similar article and decided to make one. I hope that someone will improve it.
 +
--[[User:XDot|XDot]] 07:19, 17 April 2012 (UTC)

Revision as of 13:43, 17 July 2012

The following method is a good method of receiving a rednet message without using rednet.receive(). It is usually used in IRC, Chat programs e.t.c. When a message arrives to a computer, a "rednet_message" event occurs.

The Code

This is a typical Listener which prints the content and the sender id of any incoming message.

rednet.open("Direction_of_modem")
while true do
  event, id, text = os.pullEvent()
  if event == "rednet_message" then
    print(id .. "> " .. text)
  end
end

Explanation

rednet.open("Direction_of_modem")

Here we just open the modem.

while true do

We start a loop because os.pullEvent() terminates itself after 10 seconds.

event, id, text = os.pullEvent()

We wait for an event to occur and when this happens, we store the event's info into 3 variables. NOTE: You can give your variables whichever name you want.

if event == "rednet_message" then

We check if the event was a rednet message.

print(id .. "> " .. text)

We print the message and the id of the sender.

end
end

We close our previous statements.


Note: From my research, I didn't find a similar article and decided to make one. I hope that someone will improve it. --XDot 07:19, 17 April 2012 (UTC)