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

if statement in a while loop with out pause

Started by Rougeminner, 25 July 2014 - 08:47 PM
Rougeminner #1
Posted 25 July 2014 - 10:47 PM
I recently started to use pneumatic craft with this mod and i want to check the pressure of my pneumatic system with a while loop BUT here is the catch i need to terminate the program with a if statement here is my code WITHOUT the pneumatic craft api as part of it




active = true
while active do
i = 0
i = i+1
print( i )
   local event, key = os.pullEvent("key")
   if key == 18 then
  print("Exiting")
active == false
end
end
wieselkatze #2
Posted 25 July 2014 - 10:59 PM
Your mistake is that it actually should be

active = false

You use one = if you want to assign a value to a variable (in this case you want to set active to false).
The == is only used for comparing two values.
Rougeminner #3
Posted 25 July 2014 - 11:22 PM
ok, i am assuming that the while loop is not updating because of the if statement. This script needs to update on its own instead of every time i press a button.
wieselkatze #4
Posted 25 July 2014 - 11:50 PM
So you essentialy want the script to update every x second to check the pressure? That's easy to do.
Remove the filter of your os.pullEvent() and put the event in an if-statement like this:


local active = true
local interval = 1

os.startTimer(interval)

while active do
  local e = {os.pullEvent()}

  if e[1] == "key" then
	if e[2] == 18 then
	  print("Exiting")
	  active = false
	end
  elseif e[1] == "timer" then
	--# Put your check function in here
	os.startTimer(interval)
  end
end

Note: The "local e = {os.pullEvent()} is just a fancier way to do it. It'll just dump all the returns in that table and you'll only have one variable for all stuff.
In your case the loop was not updating, because the "os.pullEvent("key")" was waiting for a key event (keypress) and everything else had been ignored.
To solve this, I'll pass every event and filter it later. You could now set the value of "interval" and the program would check the pressure every interval seconds.
Edited on 25 July 2014 - 09:51 PM