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

Can't use While and Repeat?

Started by Imprivate, 28 December 2013 - 07:49 AM
Imprivate #1
Posted 28 December 2013 - 08:49 AM
Hello all,

So I made a program that listens for a redstone signal and when it is turned off it prints a message to a monitor. Since it's always litening I thought it would be useful to have an 'X' that exited the program. I implemented this, however now neither te redstone event nor the 'X' work. It doesn't make a difference whether I put the redstone code or the 'X' code first. Anybody got any ideas?:

mon = peripheral.wrap("right")
count = 0
term.clear()
term.setCursorPos(1, 1)
sleep(1)
print("Waiting for redstone input...")
print("Hold 'CTRL+T' to terminate")
term.setCursorPos(51, 18)
print("C")
term.setCursorPos(1, 18)
print("X")
term.setCursorPos(1, 4)
while true do
count = count+1
os.pullEvent("redstone")
if rs.getInput("bottom") == false then
  print(" ")
  print("Redstone input detected ("..count..")")
  mon.setTextColor(colors.red)
  mon.setTextScale(2)
  mon.clear()
  mon.setCursorPos(6, 2)
  mon.write("DOORS")
  mon.setCursorPos(5, 3)
  mon.write("OPENED!")
  sleep(2)
  mon.clear()
end
end
repeat
event, button, x, y = os.pullEvent("mouse_click")
if x == 51 and y == 18 then
term.clear()
term.setCursorPos(1, 1)
print("Waiting for redstone input...")
print("Click X to terminate")
term.setCursorPos(1, 18)
print("X")
term.setCursorPos(51, 18)
print("C")
elseif x == 18 and y == 1 then
sleep(1)
term.clear()
term.setCursorPos(1, 1)
error()
end
until nil

I would appreciate any help :)/>
wieselkatze #2
Posted 28 December 2013 - 08:59 AM
The repeat until loop won't even run, because a while true do loop never finishes (unless terminated).
Why don't you put all in one loop?
Could look like this:


while true do
ev, prm1, prm2, prm3 = os.pullEvent()
  if ev == "redstone" then
	if rs.getInput("bottom") == false then
	print(" ")
	print("Redstone input detected ("..count..")")
	mon.setTextColor(colors.red)
	mon.setTextScale(2)
	mon.clear()
	mon.setCursorPos(6, 2)
	mon.write("DOORS")
	mon.setCursorPos(5, 3)
	mon.write("OPENED!")
	sleep(2)
	mon.clear()
	end
  elseif ev == "mouse_click" then
	if prm2 == 51 and prm3 == 18 then
	term.clear()
	term.setCursorPos(1, 1)
	print("Waiting for redstone input...")
	print("Click X to terminate")
	term.setCursorPos(1, 18)
	print("X")
	term.setCursorPos(51, 18)
	print("C")
	elseif prm2 == 18 and prm3 == 1 then
	sleep(1)
	term.clear()
	term.setCursorPos(1, 1)
	break
	end
  end
end
Edited on 28 December 2013 - 08:00 AM
Imprivate #3
Posted 28 December 2013 - 09:30 AM
This worked, and fixed another problem :)/> Thank you so much!

P.S:
The repeat until loop won't even run, because a while true do loop never finishes (unless terminated).
- snip -
Wouldn't my second 'end' terminate the 'while true' loop?
Edited on 28 December 2013 - 08:31 AM
gezepi #4
Posted 28 December 2013 - 09:52 AM
The end closes the block that the while began but there is no condition when 'true' will be false. To exit the while you could put a 'break' or if the while was in a function you could use 'return'. You would put either of those within an if block and test for some condition such as the x key being pressed.
Imprivate #5
Posted 28 December 2013 - 10:34 AM
Ah i see, i thought end would do what break did. New to coding :)/> thanks!