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

How to make some type of loop

Started by axciom, 25 April 2012 - 08:26 AM
axciom #1
Posted 25 April 2012 - 10:26 AM
Ok im trying to make some type of sentry turret using a computer and a dispenser on top.
But i dont know how to make it loop from a specific line of code.

Please help a fellow beginner
Luanub #2
Posted 25 April 2012 - 10:40 AM
There are a couple of different types of loops to use. while, for, repeat

Example while loops

--infinite while loop
while true do
code here
end

--conditional while loop
while a ~= 1 do
code here
end

Example repeat loop

repeat
code
until a == 1

There are a couple for loops that you can do.

--first repeats a line of code for a set number of times. (simple counter demonstrated)
a = 0
for x=1, 5, 1 do -- x==1 indicates the starting point, the 5 is the number of iterations to run, and 1 is the step so increase by 1 each pass
a = a + 1
print (a)
end

-- the next will use a table and will run the loop for each value in the table example of turning on redstone signals to multiple sides of the comp
local tSides = { "right", "back", "left", "front" }
for var, x in pairs(tSides) do  -- var, x sets which var you want to replace with the info from the table, pairs(tSides) selects the table to use
rs.setOutput( x, true)
end

If you run into any problems or need any help let me know.
axciom #3
Posted 26 April 2012 - 08:01 AM
Thanks for your help, Works like a charm :)/>/>