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

Better Way to do Loop?

Started by lebuildman, 20 September 2014 - 02:18 AM
lebuildman #1
Posted 20 September 2014 - 04:18 AM
Hi, again! I need more help, this time from Looping:

repeat
 tn.printCentered("            ")
 tn.printCentered("[ "..string.upper(side[sel]).." ]",5)
 local event, param, cX, cY = os.pullEvent("mouse_click")
 if cY == 5 then
  if cX > 2 and cX < 5 then -- x < a and x > b = b < x < a
   sel = sel - 1
  end
  if cX > x - 5 and cX < x then
   sel = sel + 1
  end
  if sel < 1 then sel = 7 end
  if sel > 7 then sel = 1 end
 end

 if cY > y - 1 and cY < y then
  if cX > 1 and cX < 11 then -- x < a and x > b = b < x < a
   local loop=false
   local save=true
  end
  if cX > x - 12  and cX < x - 1 then
   local save=false
   local loop=false
  end
 end
until loop == false

This is the Loop of my Program

It catches my Mouse Pos and make all the things. But, when I press the Start or the Cancel Button in bottom of screen the Loop should stop, and start the Saving Process, but It don't happen!

If you are like… Lost, here's the screenshot: http://imgur.com/AXLTNUs. The Code is looking for Clicking the "<<" buttons or "[ SAVE ]" and "[ CANCEL ]" ones at bottom of Screen.

Variables of the Loop:
local x,y = term.getSize()
local side = {"auto","left", "right", "front", "back", "top", "bottom"}
local sel = 1
local loop=true

PrintCentered/Left/Right() comes from my TodtNetworks API. Just imagine all as write()
Bomb Bloke #2
Posted 20 September 2014 - 04:27 AM
Two points:

1) This will never be true:

if cY > y - 1 and cY < y then

If cY is greater than y - 1, then that means cY must be either equal to y or greater. That can't be true if cY is also less than y.

2) Be careful where you put your "local" statements. Localise a variable to a given block, and it'll get wiped once that block ends. Try running this for eg:

local x = 3

if true then
  local x = 5
end

print(x)
lebuildman #3
Posted 20 September 2014 - 04:37 AM
1) This will never be true:

if cY > y - 1 and cY < y then

If cY is greater than y - 1, then that means cY must be either equal to y or greater. That can't be true if cY is also less than y.
Since that I go to School I got confused about Inequations :P/>/>


2) Be careful where you put your "local" statements. Localise a variable to a given block, and it'll get wiped once that block ends.
Ops… Local x Global Variables. Fixed.

But, for this Loop, What's better? Repeat? While? or Other?

EDIT: The Repeat Loop should fill what I need…
Edited on 20 September 2014 - 02:44 AM
Anavrins #4
Posted 21 September 2014 - 06:07 PM
In your case, I would use a 'while true do' loop, and end it with a 'break'