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

[Lua] [Question] Why is my loop printing twice?

Started by juiceblox, 23 September 2012 - 04:26 PM
juiceblox #1
Posted 23 September 2012 - 06:26 PM
I've rewritten and simplified my simple program for an hour now, but this problem still persists. I'm using a redstone event and checking the back redstone state to change the front redstone state and print the result. I know it must be obvious, but I can't see it.


term.clear()
term.setCursorPos(1,1)
event = 0
param1 = 0
while param1 ~= "q" do
  event, param1 = os.pullEvent()
  if event == "redstone" and redstone.getInput("back") == true then
    redstone.setOutput("front", true)
    print ("Front is now on.")
  elseif event == "redstone" and redstone.getInput("back") == false then
    redstone.setOutput("front", false)
    print ("Front is now off.")
  end
end

The output print is doubled for either case.
MysticT #2
Posted 23 September 2012 - 06:55 PM
"redstone" events are generated when redstone next to the computer changes. That means that when you change an output (turn on or off) it will also generate an event.
juiceblox #3
Posted 23 September 2012 - 07:49 PM
So the first event is changing the redstone state in the back, and then when the front changes, the event changes again, nearly instantly? Didn't think about that. Thanks!