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

[Lua][Error][Turtle]Sometimes works and Sometimes dosnt

Started by Sparx, 24 December 2012 - 12:53 PM
Sparx #1
Posted 24 December 2012 - 01:53 PM
I am trying to run this code which makes a platform, sometimes when I run it, it automatically goes to "Error: block in the way", but also sometimes it does not, the first time I ever ran it, it ran flawlessly, but I tried a second time and it failed; it also sometimes just stops and gives me the error "Error: block in the way" when there is not a block in the way.

After it gives me the "Error: block in the way" I goto the lua interpreter and type turtle.forward() but it comes up with "false" when I do it even when it should not.

I have the correct slots filled too.


local blocks = turtle.getItemCount(2)
for slot=3,16 do
  if turtle.compareTo(2) then
	blocks = blocks + turtle.getItemCount(slot)
  end
end
size = math.floor(math.sqrt(blocks))
sizeStr = tostring(size)
print("Making a " .. sizeStr .. "x" .. sizeStr .. " platform")
function prevBlockSlot(curSlot)
  for slot=curSlot,2,-1 do
	turtle.select(slot)
	if turtle.compareTo(2) then
	  return slot
	end
  end
end
function forward(curSlot)
  if turtle.getFuelLevel() < 1 then
	select(1)
	turtle.refuel(1)
	turtle.select(curSlot)
  end
  for i=1,10 do
	if turtle.detect() then
	  turtle.dig()
	end
	if turtle.forward() then
	  return true
	end
  end
  return nil
end
function placeDown(curSlot)
  if not turtle.compareTo(2) then
	curSlot = prevBlockSlot(curSlot)
  end
  turtle.placeDown()
  return curSlot
end
local curSlot = prevBlockSlot(16)
for row=1,size do
  for i=1,size-1 do
	curSlot = placeDown(curSlot)
	if not forward(curSlot) then
	  print("Error: block in the way")
	  return
	end
  end
  curSlot = placeDown(curSlot)
  if row%2 == 1 then
	turtle.turnRight()
	if not forward(curSlot) then
	  print("Error: block in the way")
	  return
	end
	turtle.turnRight()
  else
	turtle.turnLeft()
	if not forward(curSlot) then
	  print("Error: block in the way")
	  return
	end
	turtle.turnLeft()
  end
end
PixelToast #2
Posted 24 December 2012 - 02:24 PM
i usually do something like this whenever i try to go forward:

while not turtle.forward() do turtle.dig() end
Sparx #3
Posted 24 December 2012 - 04:22 PM
i usually do something like this whenever i try to go forward:

while not turtle.forward() do turtle.dig() end

Sorry if this a stupid question, but where would I put that if I added it.
PixelToast #4
Posted 24 December 2012 - 06:12 PM
in replace to turtle.forward()
it will ensure that turtle.forward() always returns true
it might crash if it reaches an unloaded chunk though
ChunLing #5
Posted 24 December 2012 - 06:17 PM
The forward function should already be doing that.
function forward(curSlot)
  if turtle.getFuelLevel() < 1 then
	    select(1)
	    turtle.refuel(1)
	    turtle.select(curSlot)
  end
  for i=1,10 do
	    if turtle.detect() then
		  turtle.dig()
	    end
	    if turtle.forward() then
		  return true
	    end
  end
  return nil
end
This function doesn't deal with mobs. Is there a chance that you're running into spawned mobs, and that's causing forward to fail?
Sparx #6
Posted 25 December 2012 - 12:30 AM
The forward function should already be doing that.
function forward(curSlot)
  if turtle.getFuelLevel() < 1 then
		select(1)
		turtle.refuel(1)
		turtle.select(curSlot)
  end
  for i=1,10 do
		if turtle.detect() then
		  turtle.dig()
		end
		if turtle.forward() then
		  return true
		end
  end
  return nil
end
This function doesn't deal with mobs. Is there a chance that you're running into spawned mobs, and that's causing forward to fail?

That could be it, but I am right next to a village but I have not had any villagers come right in front of my turtle.
ChunLing #7
Posted 25 December 2012 - 01:20 AM
And you've placed fuel in slot 1, or have enough fuel for that not to matter, right?
Sparx #8
Posted 25 December 2012 - 05:55 PM
And you've placed fuel in slot 1, or have enough fuel for that not to matter, right?

Yes, I also run into a problem that it sometimes builds a 8x8(even when I am full of cobble).

ChunLing #9
Posted 26 December 2012 - 02:55 PM
Hmm…could it be that one of your other mods is interfering with the turtle functions somehow? That's all I can think of.