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

Infinite looping a program

Started by Kghareus, 30 August 2012 - 02:50 AM
Kghareus #1
Posted 30 August 2012 - 04:50 AM
I'm trying to make a program that makes a turtle pop out of the wall whenever a button is pushed. This is what I have so far.

if redstone.getInput("left") == true then
turtle.digUp()
turtle.up()
turtle.up()
sleep(3)
turtle.down()
turtle.down()
turtle.placeUp()
elseif redstone.getInput("left") == false then
end

Is there any way to make it loop infinitely?
Luanub #2
Posted 30 August 2012 - 05:13 AM
You can do an infinite while loop

while true do
local event, p1 = os.pullEvent("redstone")
if redstone.getInput("left") == true then
turtle.digUp()
turtle.up()
turtle.up()
sleep(3)
turtle.down()
turtle.down()
turtle.placeUp()
end
end
Kghareus #3
Posted 30 August 2012 - 05:21 AM
You can do an infinite while loop

while true do
local event, p1 = os.pullEvent("redstone")
if redstone.getInput("left") == true then
turtle.digUp()
turtle.up()
turtle.up()
sleep(3)
turtle.down()
turtle.down()
turtle.placeUp()
end
end
Thanks! I had tried that but I didn't know I needed a second "end". I'm fairly new to Lua.
GrisGris #4
Posted 27 August 2013 - 03:56 PM
p1 = os.pullEvent("redstone")

I'm learning too, what does the above line do exactly?
GopherAtl #5
Posted 27 August 2013 - 04:03 PM
Thanks! I had tried that but I didn't know I needed a second "end". I'm fairly new to Lua.

For reference, anything that begins a block needs it's own end; two block statements can never share a single end statement. This is what indenting is all about, you indent in after every block statement and back out on each end. The code above indented properly makes it clear there's an end for each block statement:
Spoiler

while true do
	local event, p1 = os.pullEvent("redstone")
	if redstone.getInput("left") == true then
		turtle.digUp()
		turtle.up()
		turtle.up()
		sleep(3)
		turtle.down()
		turtle.down()
		turtle.placeUp()
	end --ends the if
end --ends the while
note that the ends line up vertically with their matching statements, in this case the first with the if and the 2nd with the while

The statements that begin a block are if…then, while…do, for…do, and function. There's also repeat … until, which is unique in lua, ending with "until <condition>" instead of an end.

For completeness, there's also the rarely-used "do…end" blocks, which have no conditions and don't loop, but you don't really need to think about those.

GrisGris: this thread explains os.pullEvent in some detail os.pullEvent - what is it and how is it useful?
GrisGris #6
Posted 27 August 2013 - 04:41 PM
Thanks GopherAtl, I'll study that thread :)/>
Imred Gemu #7
Posted 27 August 2013 - 06:26 PM
p1 = os.pullEvent("redstone")

I'm learning too, what does the above line do exactly?
os.pullEvent() is a function in the computercraft bios that allows you to get various types of events, for example:

local event, param1, param2, param3 = os.pullEvent()
will wait for any kind of event to occur and the store the event's name in the variable 'event' and any arguments in the param variables, you can also add an argument to the api so it will only be triggered by certain events:

local event, param1, param2, param3 = os.pullEvent("redstone")
will only stop waiting for events and return if the computer or turtle receives redstone inputs on one or more sides.
You can catch all kinds of events such as key's being typed on a keyboard, rednet modems receiving a message, mouse clicks on the computer's screen (for advanced computers and turtles only), and much more.
Read here for more details.