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

[lua] [question] Command not sending through rednet?

Started by TheJaberwocky, 01 November 2012 - 06:15 PM
TheJaberwocky #1
Posted 01 November 2012 - 07:15 PM
Hello, just needed to ask what I'm doing wrong here. No matter what configuration I try, I can't run a simple command through rednet. I am trying to set up a "master" computer, which will interface with various other computers in my world. For now, I'm just trying to turn on a redstone wire to learn the basics. This is my code so far:

"Sending Computer"

rednet.open("back") –allow wireless networking

command = read() –user inputs command to select process

if command == copper then –This is where the command is sent, to computer 2, with the message copper

rednet.send(2,"copper")

end

shell.run("factory") –returns the master computer to its default program



"Receiving Computer"

rednet.open("top")

while true do

command = rednet.receive()

if command == "copper" then

redstone.setOutput("front","true")

end

end


There is a wireless modem on both computers. Any suggestions?
Luanub #2
Posted 01 November 2012 - 07:58 PM
This:

if command == copper then --This is where the command is sent, to computer 2, with the message copper
should be:

if command == "copper" then --This is where the command is sent, to computer 2, with the message copper

You need to put quotes around strings. No quotes makes the code think it is variable and since there isn't one named copper the condition is not met and the message not sent.
remiX #3
Posted 01 November 2012 - 11:41 PM
Yeah and for the receiver's program use os.pullEvent("rednet_message") :

rednet.open("back")
command = string.lower(read())   -- so it isn't case sensitive
if command == "copper" then   -- copper needs " " like luanub said.
    rednet.send(2,"copper")
else print("Unknown command.")
end
shell.run("factory")   --  Is factory this program? if so then remove this and just make a while true do loop


rednet.open("top")
while true do
    event, sID, msg, dist = os.pullEvent("rednet_message")    -- So it only continues if a rednet message is received.
    if msg == "copper" then
        redstone.setOutput("front",true)   -- booleans (true, false) do not need quotes
    end
end
TheJaberwocky #4
Posted 02 November 2012 - 08:40 AM
Excellent, thanks for the quick replies. I feel like such a noob for forgetting the quotations. I'll make those changes sometime today and let you guys know if it worked. :D/>/>
Also, the "factory" program is a separate program from the code I put here, it acts as a menu for the computer so I put the shell.run command there to return it to the menu.
TheJaberwocky #5
Posted 02 November 2012 - 05:54 PM
Worked perfectly this time, thanks for the quick help. If I may ask, why use the command os.pullEvent() rather than rednet.receive?
Cruor #6
Posted 02 November 2012 - 09:16 PM
rednet.receive is just a fancier way of using os.pullEvent("rednet_message"), in a event based code os.pullEvent would be prefered(IE listen to 2+ types of events)