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

how to make a program repeat something without typing it over and over again?

Started by hugoblue, 09 September 2016 - 02:01 AM
hugoblue #1
Posted 09 September 2016 - 04:01 AM
hey, I am somewhat new to computercraft, and i was wondering if it is at all possible to make a program repeat a lua command over and over a specific amount of times
im stupid, lol

heres my code that looks probably very messy…


while true do
rs.setOutput("top",true)
sleep(0.2)
rs.setOutput("top,false)
rs.setOutput("top",true)
sleep(0.2)
rs.setOutput("top,false)
rs.setOutput("top",true)
sleep(0.2)
rs.setOutput("top,false)
rs.setOutput("top",true)
sleep(0.2)
rs.setOutput("top,false)
rs.setOutput("top",true)
sleep(0.2)
rs.setOutput("top,false)
rs.setOutput("top",true)
sleep(0.2)
rs.setOutput("top,false)
rs.setOutput("left",true)
sleep(0.2)
rs.setOutput("left",false)
rs.setOutput("left",true)
sleep(0.2)
rs.setOutput("left",false)
rs.setOutput("right",true)
sleep(0.2)
rs.setOutput("right",false)
rs.setOutput("right",true)
sleep(0.2)
rs.setOutput("right",false)
rs.setOutput("right",true)
sleep(0.2)
rs.setOutput("right",false)
rs.setOutput("right",true)
sleep(0.2)
rs.setOutput("right",false)
rs.setOutput("right",true)
sleep(0.2)
rs.setOutput("right",false)
end

any ways of simplifying this?
Bomb Bloke #2
Posted 09 September 2016 - 05:54 AM
You're after the humble "for" loop:

while true do
	for i = 1, 6 do
		rs.setOutput("top",true)
		sleep(0.2)
		rs.setOutput("top",false)
	end

	for i = 1, 2 do
		rs.setOutput("left",true)
		sleep(0.2)
		rs.setOutput("left",false)
	end

	for i = 1, 5 do
		rs.setOutput("right",true)
		sleep(0.2)
		rs.setOutput("right",false)
	end
end

See this guide for more info.

Note that you'll furthermore want to introduce a delay in between activating and disabling redstone signals. ComputerCraft can toggle outputs much faster than Minecraft can register them; a 0.1 second delay is required at a minimum. Eg:

	for i = 1, 5 do
		rs.setOutput("right",true)
		sleep(0.2)
		rs.setOutput("right",false)
		sleep(0.2)
	end
Edited on 09 September 2016 - 03:55 AM
hugoblue #3
Posted 09 September 2016 - 02:42 PM
You're after the humble "for" loop:

while true do
	for i = 1, 6 do
		rs.setOutput("top",true)
		sleep(0.2)
		rs.setOutput("top",false)
	end

	for i = 1, 2 do
		rs.setOutput("left",true)
		sleep(0.2)
		rs.setOutput("left",false)
	end

	for i = 1, 5 do
		rs.setOutput("right",true)
		sleep(0.2)
		rs.setOutput("right",false)
	end
end

See this guide for more info.

Note that you'll furthermore want to introduce a delay in between activating and disabling redstone signals. ComputerCraft can toggle outputs much faster than Minecraft can register them; a 0.1 second delay is required at a minimum. Eg:

	for i = 1, 5 do
		rs.setOutput("right",true)
		sleep(0.2)
		rs.setOutput("right",false)
		sleep(0.2)
	end

thankyou very much! i knew it couldve been that simpe.
Lyqyd #4
Posted 09 September 2016 - 03:21 PM
Moved to Ask a Pro.