Jump to content




Lower Variable number value


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

#1 BowWhalley

  • Members
  • 25 posts
  • LocationMelbourne (AU)

Posted 16 July 2015 - 05:07 AM

Hello, I am trying to make a program that builds me a giant rail.

I want it to automatically place down powered rail every ten blocks.

Here is my code:
local X
X = 10
while true do
  if turtleedu.inspect() ~= "minecraft:air" then
    turtle.dig()
    turtle.forward()
    X = X - 1
  end
  if X > 0 then
    turtle.select( 1 )
    turtle.forward()
    turtle.placeDown()
  end
  if X == 0 then
    turtle.select( 2 )
    turtle.placeDown()
    X = 10
  end
end
VISUAL
ComputerCraftEdu:variable X
ComputerCraftEdu:equalTo
ComputerCraftEdu:number 10


#2 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 16 July 2015 - 05:48 AM

View PostBowWhalley, on 16 July 2015 - 05:07 AM, said:

  if turtleedu.inspect() ~= "minecraft:air" then
	turtle.dig()
	turtle.forward()
	X = X - 1
  end

You can do this instead:
while not turtle.forward() do
  turtle.dig()
end
It takes care of sand / gravel.


Also, read this carefully:
while true do
  if turtleedu.inspect() ~= "minecraft:air" then
    turtle.dig()
    turtle.forward()
    X = X - 1
  end

  ... 

end
If it finds just air in front of the turtle, it won't decrease X!
Just put that after the if-statement (or the while statement if you use my suggestion)


Are you having any other problems?

Edited by HPWebcamAble, 16 July 2015 - 05:51 AM.


#3 Bomb Bloke

    Hobbyist Coder

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

Posted 16 July 2015 - 05:49 AM

Part of the problem here is that you're only decrementing X if there's something other than air in front of the turtle.

Try it like this:

local X = 10

while true do
  -- Until an attempt to go forward succeeds, wave the pickaxe at blocks/mobs:  
  while not turtle.forward() do
    turtle.dig()
    turtle.attack()
  end

  -- Just in case there's something blocking our rail:
  if turtle.detectDown() then turtle.digDown() end

  -- Change X and choose our rail:
  if X > 0 then
    turtle.select( 1 )
    X = X - 1
  else
    turtle.select( 2 )
    X = 10
  end

  -- Place the selected rail:
  turtle.placeDown()
end

Which in the visual editor would look like this:

Posted Image

You'd still need to think about how to ensure that there's a block to place the rails on, though!

#4 BowWhalley

  • Members
  • 25 posts
  • LocationMelbourne (AU)

Posted 16 July 2015 - 06:04 AM

Thankyou guys, I've taken in your feedback and gotten the program to work :).
Also to your last statement BombBloke, I needed this information to also create the second program for a second turtle that was placing down blocks for the rail to go onto

#5 jerimo

  • Members
  • 74 posts

Posted 16 July 2015 - 06:19 PM

View PostBowWhalley, on 16 July 2015 - 06:04 AM, said:

Thankyou guys, I've taken in your feedback and gotten the program to work :)/>.
Also to your last statement BombBloke, I needed this information to also create the second program for a second turtle that was placing down blocks for the rail to go onto
You may wish to try having your turtle go backwards, that way it can both place the rails and check for a stable floor. When it runs into something you can turn it around to break the blocks until it can move forward again, at which point you make it go backwards and placing rails again.
Alternatively you could also make it go up the hills it could run into





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users