Jump to content




creating a loop for frame quarry

help lua

  • You cannot reply to this topic
5 replies to this topic

#1 teeto69

  • Members
  • 3 posts

Posted 08 April 2015 - 07: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 by teeto69, 08 April 2015 - 08:03 PM.


#2 valithor

  • Members
  • 1,053 posts

Posted 08 April 2015 - 08: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.

#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 08 April 2015 - 09: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.).

#4 teeto69

  • Members
  • 3 posts

Posted 08 April 2015 - 09:48 PM

Quote

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

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 08 April 2015 - 09: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 by Bomb Bloke, 08 April 2015 - 09:54 PM.


#6 teeto69

  • Members
  • 3 posts

Posted 08 April 2015 - 11:53 PM

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users