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

[ERROR] event red net message detection

Started by wrothmonk, 19 March 2012 - 05:49 PM
wrothmonk #1
Posted 19 March 2012 - 06:49 PM
I'm attempting to create a red net based secret door; where one computer sends a specific red net message and the receiving computer sends a redstone signal to some pistons. When it receives a different message it stops the redstone signal. I want it to be able to receive the signal at any time. The code is located as the startup.

Finished code:
Startup code on computer #28

while true do
rednet.open("back")
Rednet1, Rednet2, Rednet3 = os.pullEvent("rednet_message")
print(Rednet1)
print(Rednet2)
print(Rednet3)
if Rednet3 == "open" then
redstone.setOutput("left", false
elseif Rednet3 == "close" then
redstone.setOoutput("left", true)
else
end
end
open_trap_door code on computer #21

rednet.open("left")
rednet.send(28, "open")
close_trap_door code on computer #21

rednet.open("left")
rednet.send(28, "close")
Jan #2
Posted 19 March 2012 - 07:54 PM
I'm attempting to create a red net based secret door; where one computer sends a specific red net message and the receiving computer sends a redstone signal to some pistons. When it receives a different message it stops the redstone signal. I want it to be able to receive the signal at any time. The code is located as the startup.
CODE

while true do
rednet.open("back")
Rednet1, Rednet2, other1, other2, other3 = os.pullEvent()
if "red net_message", "open" = Rednet1, Rednet2 then
redstone.setOutput("left", false)
elseif "rednet_message", "open" = Rednet1, Rednet2 then
redstone.setOutput("left", true)
else
end
ERROR

bios:206: [string "startup]:4: 'then' expected
Use '==' instead of '='. I have that same error all the time too :D/>/>.
wrothmonk #3
Posted 19 March 2012 - 08:54 PM
ty for pointing that out. However it still didn't fix the bug :D/>/>

missed one… although I'm getting another bug

CODE

while true do
rednet.open("back")
Rednet1, Rednet2, other1, other2, other3 == os.pullEvent()
if "red net_message", "open" == Rednet1, Rednet2 then
redstone.setOutput("left", false)
elseif "rednet_message", "open" == Rednet1, Rednet2 then
redstone.setOutput("left", true)
else
end
ERROR

bios:206: [string "startup"]:3: '=' expected
Luanub #4
Posted 19 March 2012 - 09:05 PM
To many ='s in os.pullEvent() statements.

proper syntax with the = is:
a = 1
if a == 1 then

Should be(you probably dont need this many vars and would be better to use locals)

Rednet1, Rednet2, other1, other2, other3 = os.pullEvent()

For the first example anyway. I don't think the second method would work.

You also have some typo's and errors in your if statements.

I would personally do it like this

while true do
rednet.open("back")
local event, other1, other2 = os.pullEvent("rednet_message")
   if other2 == "open" then
	  redstone.setOutput("left", true)
   elseif other2 == "close" then
	   redstone.setOutput("left", false)
   end
end

event var - will get set as the event type
other1 var - will get set as the sending computer id
other2 var - will get set as the message

the os.pullEvent("rednet_message") filters out all other events except rednet_message
Edited on 19 March 2012 - 08:16 PM
Espen #5
Posted 19 March 2012 - 09:08 PM
@wrothmonk:
= is used to assign a variable.
== is used for comparison.

Specifically in your code:
Use = for assigning variables to os.pullEvent(), but == if you want to compare them.

EDIT: Whoops, too late, got Ninja'd. :D/>/>
Edited on 19 March 2012 - 08:09 PM
wrothmonk #6
Posted 19 March 2012 - 09:11 PM
ok then. Heres the new code and error.
CODE

while true do
rednet.open("back")
Rednet1, Rednet2, other1, other2, other3 = os.pullEvent()
if "red net_message", "open" == Rednet1, Rednet2 then
redstone.setOutput("left", false)
elseif "rednet_message", "open" == Rednet1, Rednet2 then
redstone.setOutput("left", true)
else
end
ERROR

bios:206: [string "startup"]:4: 'then' expected
Liraal #7
Posted 19 March 2012 - 09:11 PM
add two 'end's
wrothmonk #8
Posted 19 March 2012 - 09:17 PM
updated original post code/error. Still having the same error as before though.
Also thank you liraal for pointing that out. I hadn't realized at first that I needed two ends. Makes sense with the if though.
Luanub #9
Posted 19 March 2012 - 09:19 PM
Wow alot going on here… lol

Try the edit I put it.
wrothmonk #10
Posted 19 March 2012 - 09:39 PM
ok tried your edit no errors popped up. However when I attempt to send a message using the close_trap_door code it doesn't do anything. I have update main post with all info.
Liraal #11
Posted 19 March 2012 - 09:50 PM
check whether the messages get through at all. open the lua prompt and type os.pullEvent("rednet_message") is something pops up when you send the message, it's all good.
Luanub #12
Posted 19 March 2012 - 09:51 PM
try doing a rednet.broadcast( "open" ) to see if it will pick that up. If so make sure you are using the correct id.

If not make sure the computers are in range and that the ports are opening.
Espen #13
Posted 19 March 2012 - 09:56 PM
@wrothmonk:
Case-Sensitivity is important not only in Lua code, but especially when using Strings.
E.g. you used "rednet_Message" with a capital M, but it should be lower-case.
wrothmonk #14
Posted 19 March 2012 - 09:57 PM
ok broadcast didn't work. Can you tell me the range of the rednet? I also don't know how to check if the ports are opening.
@Espen: sorry that was a typo on the post. It is lower case in code. (now its updated in OP)
Edited on 19 March 2012 - 08:58 PM
wrothmonk #15
Posted 19 March 2012 - 10:04 PM
ah I figured out the problem. it turns out the message was not being received properly in luanub's.
Updated OP for any of those interested inthe working code.
Edited on 19 March 2012 - 09:06 PM