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

Defining and executing a function

Started by Rulyon, 22 September 2016 - 12:49 AM
Rulyon #1
Posted 22 September 2016 - 02:49 AM
Hello, folks. I would like to preface with the information that I am a complete beginner at ComputerCraft specifically and coding in general, and am learning through trial and error. I have watched tutorials on YouTube and read tutorials on the forum, but cannot locate the answer to my question. I am certain the answer is out there somewhere, but there is such a wealth of information that I am having difficulty locating the proverbial needle in a haystack. I hope someone here can help me. For references dimensions will be stated in a Width X Height X Length format.

I am programming a Crafty Miner Turtle to create shaft mining stations off of my Bedrock staircase. The idea is to plunk the turtle down on one of the steps on the desired level, run the program, then return later to the completed mining station. The intended design is for a 1 x 2 x 3 link tunnel to open into a chamber that is 5 x 3 x 5. One corner of the room, front left as you enter, is dug out one level lower into the wall creating a 2 x 1 x 2 hole for an endless water source.

I accomplished this last night with a simple (if somewhat lengthy) program that accomplished this goal by giving each command one at a time. The chamber took nearly 180 lines of code.

I am attempting to condense a program to utilize fewer lines of code by defining functions. My first snag hit immediately when defining the initial "digLink" function. I intend for the "digLink" function to result in the requisite 1x2x3 link tunnel carved into the wall. However when I run the program the turtle merely digs the block in front of it once then stops. I am sure I have an error somewhere, but I am not knowledgeable or experienced enough for it to be apparent to me. Can anyone assist me?

function digLink()
   LinkLength = 0
	  while LinkLength < 3 do
		 turtle.dig()
		 turtle.forward()
		 turtle.digUp()
		 LinkLength = LinkLength + 1
	  end
   end

digLink()
Rulyon #2
Posted 22 September 2016 - 06:31 AM
Never mind. I forgot to fuel the damn thing before executing the program. Once I realized what a total idiot I was, I finished the program to open a chamber and was quite pleased with the results. :-)

(Edit by BB: this post was invisible at the time the one below was posted!)
Edited on 22 September 2016 - 06:02 AM
valithor #3
Posted 22 September 2016 - 07:01 AM
Does the turtle have fuel? The code you posted should make it dig go forward and dig above it 3 times, resulting in a 1x2x3 tunnel. Btw you might want to look into for loops instead of using a while loop.

For loop example:

start = 1
end = 5
for i = start, stop do
  print(i) --# will print 1, 2, 3, 4, 5
end

You can put numbers directly where the start and stop variables are, I did it with variables to emphasize what those positions were for.


--# same as above
for i = 1, 5 do
  print(i)
end
Rulyon #4
Posted 23 September 2016 - 12:03 AM
Thank you, Valithor. I will edit my code accordingly. A for loop makes much more sense than a while loop in this use. :-)