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

[Lua] [Question] Calling for loops

Started by ikon106, 06 December 2012 - 09:21 AM
ikon106 #1
Posted 06 December 2012 - 10:21 AM
Is there a way to call for loops, in other words reuse a loop, make the loop do the same thing again?
OmegaVest #2
Posted 06 December 2012 - 10:25 AM
… If you mean making multiple of the same loop, then yes, just close it in another loop.


for x =1 , 2 do
   for y = 10, 15 do
	  term.setCursorPos(x,y)
	  term.write("*")
   end
end

And you can nest them pretty much forever, but I would be wary of doing that.
Leo Verto #3
Posted 06 December 2012 - 10:28 AM
Are you talking about calling a loop from anywhere in your code?
In that case, you should look up how to use functions.
faubiguy #4
Posted 06 December 2012 - 10:46 AM
If you want to reuse a loop, you can just enclose it in a function:
function loop()
  for 1, 7 do
	-- Do stuff here
  end
end

Then you can call it from anywhere in your code (after you create the function).
ChaddJackson12 #5
Posted 07 December 2012 - 03:16 PM
You could probably do something like this too, if you wanted: (With a while loop)


while UseLoop do
  -- What you want to be repeated while UseLoop is true...
end

How you would use this loop?


UseLoop = true -- Enables loop

-- And

UseLoop = false -- Disables loop
PixelToast #6
Posted 07 December 2012 - 04:01 PM
or even:

while true do
  if something then
    break -- jump to the inner most loop's end
  end
end
ikon106 #7
Posted 09 December 2012 - 02:36 AM
If you want to reuse a loop, you can just enclose it in a function:
function loop()
  for 1, 7 do
	-- Do stuff here
  end
end

Then you can call it from anywhere in your code (after you create the function).

OK, thank you, I was wondering if you could do that :)/>