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

Need help with vertical building decided by user input.

Started by Loki, 04 January 2013 - 04:19 PM
Loki #1
Posted 04 January 2013 - 05:19 PM
Exactly what the turtle does after you tell him to go 10 blocks:

1.goes up 10 blocks slowly, placing no blocks on his way.
2.Continuously selects different inventory slots even though he already has blocks in slot 1.
3.goes down 1 block (I know that part of the code is wrong and i know how to fix it :P/>.)
4.Gives no error log and acts as if nothing went wrong.


function Main()
turtle.select(1)
print("Welcome to Loki's king of the ladder minigame builder.")
print("How high do you want it?")
high = read()
print("Going "..high.." blocks up! :D/>")
for i=1, high do
  turtle.select(1)
  if turtle.getItemCount(1) == 0 then
   turtle.select(2)
  end
  if turtle.getItemCount(2) == 0 then
   turtle.select(3)
  end
  if turtle.getItemCount(3) == 0 then
   turtle.select(4)
  end
  if turtle.getItemCount(4) == 0 then
   turtle.select(5)
  end
  turtle.place()
  turtle.up()
end
turtle.down(high)
print("Going down!")
end
Main()
ChiknNuggets #2
Posted 04 January 2013 - 05:39 PM
try this

function Main()
turtle.select(1)
print("Welcome to Loki's king of the ladder minigame builder.")
print("How high do you want it?")
high = tonumber(read())
print("Going "..high.." blocks up! :D/>/>/>/>/>")
for i=1, high do
  turtle.select(1)
  if turtle.getItemCount(1) == 0 then turtle.select(2)
	elseif turtle.getItemCount(2) == 0 then turtle.select(3)
  elseif turtle.getItemCount(3) == 0 then turtle.select(4)
  elseif turtle.getItemCount(4) == 0 then turtle.select(5) end
  turtle.place()
  turtle.up()
end
print("Going down!")
for i=1,high do turtle.down() end
end
Main()

Your problem i believe is that you had multiple if statements that werent joined it checked all of them meaning if kinda like 4 is empty to ill set it to 5, it didnt matter if slot one had it in there, now it checks slot 1 and if slot 1 has blocks it finishes the if statements, also fixed the going down part
ChunLing #3
Posted 04 January 2013 - 05:47 PM
You can eliminate the definition of a main function and subsequent call of main, shell turns the entire file into a function when you run the program. Also, it's probably better to use a more robust way to select the next slot, and only when you actually are out:
local sslot = 1
print("Welcome to Loki's king of the ladder minigame builder.")
print("How high do you want it?")
local high = read()
print("Going "..high.." blocks up! :D/>/>")
for i=1, high do
    while not turtle.place() do
        sslot = (sslot % 16)+1
        turtle.select(sslot)
    end
    turtle.up()
end
print("Going down!")
for i=1, high do turtle.down() end
The while not turtle.place() loop ensures that you actually end up with a slot of something that can be placed, instead of coal or an empty bucket or something (fill your turtle with lava buckets for lava climbing fun!).
ChiknNuggets #4
Posted 04 January 2013 - 06:01 PM
while not turtle.place() do
sslot = (sslot % 16)+1
turtle.select(sslot)


The problem with this is that say there was a block that was where he wanted to place, it was cause an infinite loop
remiX #5
Posted 04 January 2013 - 09:02 PM
try converting high to a number,
high=tonumber(read())
ChunLing #6
Posted 05 January 2013 - 02:59 AM
Probably wise, but it was the bit about "if turtle.getItemCount(4) == 0 then turtle.select(5) end " that was causing the problem.

No problem with adding a turtle.dig() to the while not turtle.place() loop.

But now the problem is that if you jump up on top of the turtle, it just places and digs the first block over and over.