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

Loop will not break on os.pullEvent("key")

Started by Brodur, 09 April 2015 - 12:57 AM
Brodur #1
Posted 09 April 2015 - 02:57 AM
So. I've made a monitoring program for my Big Reactor, which refreshes every 0.5 and is supposed to be able to launch an on/off menu at the press of a button. Problem is that button won't press.
Here's the main program:
http://pastebin.com/a24R68B8
local reactor=peripheral.wrap("top")
local isRunning="Undefined"
while true do

term.clear() --// Clering the screen and setting Pos\\--
term.setCursorPos(1,1)

print("[//////////////////Reactor Status/////////////////]")
print()
print("Reactor: ") -- Will enter status here--

if reactor.getActive()==true --If the reactor is on...
  then 
  isRunning="ON"
    term.setCursorPos(10,3) write(isRunning)    
elseif reactor.getActive()==false
  then
  isRunning="OFF"
    term.setCursorPos(10,3) write(isRunning)
else write("Error")  
end

term.setCursorPos(1,5)
--// Figure out how to check fuel\\--
local fuelLev= reactor.getFuelAmount()
local fuelMax= reactor.getFuelAmountMax()
local percentFuel = ((fuelLev / fuelMax) * 100)
print("Fuel: ") -- Trying to print the fuel w/o errors
print(percentFuel)
term.setCursorPos(5,6) print(" %                      ")

if percentFuel <= 10 and reactor.getActive()==true then
reactor.setActive(false)
end
print() print("Press <spacebar> to toggle the reactor!")
os.sleep(0.5)

if os.pullEvent("key")==57 then shell.run("rmenu") break end

end --Repeat until spacebar is pressed--
and here is the menu it launches http://pastebin.com/LSegr1Gv
Lyqyd #2
Posted 09 April 2015 - 02:59 AM
Even when filtering events, os.pullEvent returns the event name first, and no event that I know of uses 57 as the event name. You'll have to capture both arguments and check them to ensure that what was returned was "key", 57.
Brodur #3
Posted 09 April 2015 - 03:06 AM
so I've changed it to
if os.pullEvent("key")=="key", 57 then shell.run("rmenu") break end
and I'm getting then expected on the line
KingofGamesYami #4
Posted 09 April 2015 - 03:29 AM
You can't compare two returns on the same line.

try

if ({os.pullEvent("key")})[2]==57 then
Square789 #5
Posted 09 April 2015 - 09:42 AM
I always do it like this:

event, key = os.pullEvent("key")
if key == 57 then --#You can also use keys.enter instead of 57
	 shell.run("rmenu")
	 break
end
Personnaly, I can read code like this better than code standing in a single line
Brodur #6
Posted 09 April 2015 - 01:27 PM
I always do it like this:

event, key = os.pullEvent("key")
if key == 57 then --#You can also use keys.enter instead of 57
	 shell.run("rmenu")
	 break
end
Personnaly, I can read code like this better than code standing in a single line

Your code works fine for pulling the event, thank you. Now my problem is that the program is waiting for the keystroke, but I just want it to keep looping until/if the key is pressed.edit . So i changed it to a repeat loop.

--snipped variables--
event, key = os.pullEvent("key")
repeat
--snipped body code--
until key == 57
shell.run("rmenu")
The problem I'm having now is that when i run, then prgram prints nothing, and when i press spacebar it briefly shows the things that were supposed to be there and then launches the menu as intended…. so progress of sorts? I'm going to re-paste the code into here:
local reactor=peripheral.wrap("top")
local isRunning="Undefined"
event, key = os.pullEvent("key")
repeat

term.clear() --// Clering the screen and setting Pos\\--
term.setCursorPos(1,1)

print("[//////////////////Reactor Status/////////////////]")
print()
print("Reactor: ") -- Will enter status here--

if reactor.getActive()==true --If the reactor is on...
  then 
  isRunning="ON"
    term.setCursorPos(10,3) write(isRunning)    
elseif reactor.getActive()==false
  then
  isRunning="OFF"
    term.setCursorPos(10,3) write(isRunning)
else write("Error")  
end

term.setCursorPos(1,5)
--// Figure out how to check fuel\\--
local fuelLev= reactor.getFuelAmount()
local fuelMax= reactor.getFuelAmountMax()
local percentFuel = ((fuelLev / fuelMax) * 100)
print("Fuel: ") -- Trying to print the fuel w/o errors
print(percentFuel)
term.setCursorPos(5,6) print(" %                      ")

if percentFuel <= 10 and reactor.getActive()==true then
reactor.setActive(false)
end
print() print("Press <spacebar> to toggle the reactor!")
os.sleep(1)

until key == 57 --Repeat until spacebar is pressed--
shell.run("rmenu")
DannySMc #7
Posted 09 April 2015 - 02:19 PM
I am not sure if this is what you are going for but I would do this (I can't test code as at work, so just tell me wow it goes):
The script below will redraw what is on the screen every 2 seconds meaning it updates the stats every two seconds and you press space to run the rmenu? I think idk if you give me all your code I can rewrite it?:D/>


local reactor=peripheral.wrap("top")
local isRunning="Undefined"

function main()	
	term.clear() --// Clering the screen and setting Pos\\--
	term.setCursorPos(1,1)
	
	print("[//////////////////Reactor Status/////////////////]")
	print()
	print("Reactor: ") -- Will enter status here--
	
	if reactor.getActive()==true then 
	  	isRunning="ON"
	    term.setCursorPos(10,3) 
	    write(isRunning)    
	elseif reactor.getActive()==false then
	  	isRunning="OFF"
	    term.setCursorPos(10,3) 
	    write(isRunning)
	else 
		write("Error")  
	end
	
	term.setCursorPos(1,5)
	--// Figure out how to check fuel\\--
	local fuelLev = reactor.getFuelAmount()
	local fuelMax = reactor.getFuelAmountMax()
	local percentFuel = ((fuelLev / fuelMax) * 100)
	print("Fuel: ") -- Trying to print the fuel w/o errors
	print(percentFuel)
	term.setCursorPos(5,6) print(" %                      ")
	if percentFuel <= 10 and reactor.getActive()==true then
		reactor.setActive(false)
	end
	print(" ")
	print("Press <spacebar> to toggle the reactor!")
end

function toggleevent()
	main()
	os.startTimer(2)
	while true do
		local args = { os.pullEvent() }
		if args[1] == "key" then
			if args[2] == 57 then
				shell.run("rmenu")
			end
		elseif args[1] == "timer" then
			main()
		end
	end
end

toggleevent()
</spacebar>
Edited on 09 April 2015 - 12:21 PM
Square789 #8
Posted 09 April 2015 - 08:13 PM
The problem I'm having now is that when i run, then prgram prints nothing, and when i press spacebar it briefly shows the things that were supposed to be there and then launches the menu as intended…. so progress of sorts? I'm going to re-paste the code into here:
local reactor=peripheral.wrap("top")
local isRunning="Undefined"
event, key = os.pullEvent("key")
repeat

term.clear() --// Clering the screen and setting Pos\\--
term.setCursorPos(1,1)

print("[//////////////////Reactor Status/////////////////]")
print()
print("Reactor: ") -- Will enter status here--

if reactor.getActive()==true --If the reactor is on...
  then
  isRunning="ON"
	term.setCursorPos(10,3) write(isRunning)	
elseif reactor.getActive()==false
  then
  isRunning="OFF"
	term.setCursorPos(10,3) write(isRunning)
else write("Error")  
end

term.setCursorPos(1,5)
--// Figure out how to check fuel\\--
local fuelLev= reactor.getFuelAmount()
local fuelMax= reactor.getFuelAmountMax()
local percentFuel = ((fuelLev / fuelMax) * 100)
print("Fuel: ") -- Trying to print the fuel w/o errors
print(percentFuel)
term.setCursorPos(5,6) print(" %					  ")

if percentFuel <= 10 and reactor.getActive()==true then
reactor.setActive(false)
end
print() print("Press <spacebar> to toggle the reactor!")
os.sleep(1)

until key == 57 --Repeat until spacebar is pressed--
shell.run("rmenu")
The problem simply is that the program isn't told to show the menu before the space key is pressed.
But DannySMc's code should work fine.
Edited on 09 April 2015 - 06:13 PM
Brodur #9
Posted 09 April 2015 - 09:59 PM
Thanks for the help so far, cant actually test as I'm off to work right now, will inform you in ~5-6 hours if its working.
Dragon53535 #10
Posted 09 April 2015 - 10:52 PM
When you do test it, you're going to need to change this part:

function toggleevent()
	    main()
	    os.startTimer(2)
	    while true do
			    local args = { os.pullEvent() }
			    if args[1] == "key" then
					    if args[2] == 57 then
							    shell.run("rmenu")
					    end
			    elseif args[1] == "timer" then
					    main()
			    end
	    end
end
to this:

function toggleevent()
	    main()
	    os.startTimer(2)
	    while true do
			    local args = { os.pullEvent() }
			    if args[1] == "key" then
					    if args[2] == 57 then
							    shell.run("rmenu")
					    end
			    elseif args[1] == "timer" then
					    main()
					    os.startTimer(2)
			    end
	    end
end
If you don't add that os.startTimer inside that part of the if, then you'll only have the reactor refresh once after startup. that os.startTimer ensures that it runs main every two seconds.
Brodur #11
Posted 10 April 2015 - 05:46 AM
Thanks for all the help, it works now. Resolved.