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

loop with rednet

Started by Brian87, 22 February 2014 - 05:49 PM
Brian87 #1
Posted 22 February 2014 - 06:49 PM
Hello, I wondered how I could loop this code until whenever it receives a rednet message which says "startup".
I tried this but it doesn't loop:


while true do
  local side = {"right", "left", "top", "bottom", "front", "back"}
  local random_side = side[math.random(1, #side)]
  rs.setOutput(random_side, true)
  sleep(math.random(0.5, 1))
  rs.setOutput(random_side, false)
  sleep(math.random(0, 0.5))
  id, event, message = os.pullEvent()
  if event == "redstone_message" and message == "startup" then
   return
  end
end
Thib0704 #2
Posted 22 February 2014 - 07:38 PM
1. What are you trying to do?
2. Are you using REDNET or REDSTONE ??
karelmikie3 #3
Posted 22 February 2014 - 07:45 PM
if you're trying to use rednet then use this:

while (message ~= "startup")  do
event, replyChannel, message, senderDistance = os.pullEvent("modem_message")
end
Edited on 22 February 2014 - 06:50 PM
Brian87 #4
Posted 22 February 2014 - 08:08 PM
correction :

if event == "rednet_message" and message == "startup" then
   return
  end


I'm trying to run programs through another pc via rednet, where the first pc runs a program, and if input is "start", this script starts on another pc which sends random redstone signals to redstone lamps, but I want it to loop until it gets an message saying "startup" which starts up the startup-script. Planning on making a concert where a main computer turns on blinking lights.
theoriginalbit #5
Posted 22 February 2014 - 08:18 PM
okay so I need to correct you on some incorrect thinking here…

When a redstone signal changes for a computer (whether it's on or off) it will fire an event named redstone
When a message is sent from one computer to another (via wired or wireless) it will fire two events (1 in older ComputerCraft) named modem_message and rednet_message; in the case of how you are using your code you should make use of the rednet_message event
Edited on 22 February 2014 - 07:18 PM
Thib0704 #6
Posted 23 February 2014 - 09:50 AM
You want something like this:


local running = true
local modemSide = "right" -- Modem side.
local outputSide = "left" -- Redstone output side.
local toggleMode = true

rednet.open(modemSide)
while running do
	id,msg,dist = rednet.receive()
		if msg == "toggle" then
				 rs.setOutput(outputSide, toggleMode)
				 if toggleMode then toggleMode = false else toggleMode = true end
		end
end
Edited on 23 February 2014 - 08:51 AM
TheOddByte #7
Posted 23 February 2014 - 10:57 AM
I just want to point out to the OP that you're using os.pullEvent wrong, Since it returns the event first.
And it's rednet, Not redstone in this case
Example

local sides = rs.getSides() -- Get the redstone sides

while true do
	local event, id, message = os.pullEvent( "rednet_message" )
	if message == "blah"
		local side = sides[math.random( 1,#sides )]
		if rs.getOutput( side ) then
			 rs.setOutput( side, false)
		else
			rs.setOutput( side, true )
		end
    end
end
Edited on 23 February 2014 - 09:58 AM
Brian87 #8
Posted 23 February 2014 - 11:48 AM
Alright, thanks everybody :)/>