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

creating a loop for frame quarry

Started by teeto69, 08 April 2015 - 06:48 PM
teeto69 #1
Posted 08 April 2015 - 08:48 PM
I'm having issues coding an infinate loop to test for a redstone signal to do the move and mine on my frame quarry. below is the coding i used ( i know i could add the functions to the loop instead of a second program.)

this is the startup loop set as test1 for now so can manually run it:


if rs.getInput( "top" , true)
print("lever activated…)
shell.run("move")
sleep(1)
shell.run("test1")

else
shell.run("test1")
end

this is the move program


function quarryMove()
rs.setOutput( "right" , true )
sleep(1)
rs.setOutput( "right" , false )
sleep(1)
end

function quarryMine()
rs.setOutput( "left" , true )
sleep(25)
rs.setOutput( "left" , false )
sleep(1)
end

quarryMove()
quarryMine()
Edited on 08 April 2015 - 07:03 PM
valithor #2
Posted 08 April 2015 - 09:58 PM
Computercraft has a thing, which prevents infinite recursive loops. To accomplish what you are wanting you will need to use a while loop, and put your code inside of it.

Example

while true do
  print("hi")
  sleep()
end

CC limits recursion by 256 (I believe), where everytime something calls something else it is put into a table. When the thing that was called finishes running it is removed from the table. If the table ever reaches the maximum of 256 it will error, which is what I believe you are getting.
KingofGamesYami #3
Posted 08 April 2015 - 10:06 PM
To clarify, the call stack is 256, but some of this is filled by CraftOS so you won't recurse 256 times.

This can be solved by using return, ei

local test
function test()
  return test()
end

Keep in mind, you will still need to yield (pull an event, use a turtle movement function, etc.).
teeto69 #4
Posted 08 April 2015 - 10:48 PM
CC limits recursion by 256 (I believe), where everytime something calls something else it is put into a table. When the thing that was called finishes running it is removed from the table. If the table ever reaches the maximum of 256 it will error, which is what I believe you are getting.

ok so here is how i changed it and works amazing thanks

function quarryMove()

rs.setOutput("right" , true)
sleep(1)
rs.setOutput("right" , false)
sleep(1)
end

function quarryMine()

rs.setOutput("left" , true)
sleep(25)
rs.setOutput("left" , false)
sleep(10)
end

local x = 1
local i = 0
repeat
while rs.getInput( "top" , true) do
quarryMove()
quarryMine()
sleep(1)
x = x+1
print("Times ran:"..x)
end
while rs.getInput("top" , false)
print("awaiting signal")
sleep(5)
end
until i > 1
end
i like the added counter and it runs smoothly now thanks for the while do it helped
Bomb Bloke #5
Posted 08 April 2015 - 10:52 PM
You can read up further on looping here.

Also bear in mind that you won't want your script constantly checking the value of the top input; that's a waste of processing power, and ComputerCraft will crash your script if it thinks you're going overboard. You can rig it to yield - sit and down nothing - until such time as a redstone state change occurs by waiting for the relevant event:

while true do
	while rs.getInput( "top" ) do   -- rs.getInput() only accepts one parameter. It *returns* either true or false.
		print("lever activated...")
		quarryMove()
		quarryMine()
		sleep(1)
	end
	
	os.pullEvent("redstone")
end
Edited on 08 April 2015 - 08:54 PM
teeto69 #6
Posted 09 April 2015 - 12:53 AM
Thanks for the helps everyone! I got it working like a dream now on a note, trying the os.pullevent("redstone") in my loop killed the computer however it wasn't necessary with while true do. Again thanks a million, i now have a frame quarry controllable by a lever.