Jump to content




Defining and executing a function

turtle command help

3 replies to this topic

#1 Rulyon

  • Members
  • 3 posts

Posted 22 September 2016 - 01: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()


#2 Rulyon

  • Members
  • 3 posts

Posted 22 September 2016 - 05: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 by Bomb Bloke, 22 September 2016 - 07:02 AM.


#3 valithor

  • Members
  • 1,053 posts

Posted 22 September 2016 - 06: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


#4 Rulyon

  • Members
  • 3 posts

Posted 22 September 2016 - 11:03 PM

Thank you, Valithor. I will edit my code accordingly. A for loop makes much more sense than a while loop in this use. :-)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users