←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Selecting problem

W00dyR's Photo W00dyR 20 Jan 2013

Hey guys, I am having a turtle build a big building fully automatic. Now I have it take a few stacks of blocks from a chest then it starts building. I have a simple function for placing the blocks, which includes checking how many items it has left in the selected slot + switching slots if not enough.

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) <= 1 do
		slot = slot + 1
		turtle.select(slot)
  end
  turtle.placeDown()
end

Now this is used within the script that places the blocks for the building.

local function build()
  turtle.forward()
  blockbelow()
  turtle.forward()
  blockbelow()
end

I just typed that, since it would be kind of big to copy the entire thing. The thing that happens now is that for every block, it checks how many left, and goes through the entire inventory causing somewhat of a delay and slowing the process down, nonetheless, it works, but I feel like it can be better.

I have two questions with this which are most likely very easy, but I'm just not seeing the solution :P
- How do I make it so it does not go through the entire inventory, but keeps the slot selected until it runs out
- Right now it leaves 1 block in every slot, I've tried a few things, but I can't get it to freaking use up all the blocks

Thanks ahead :)
Quote

mibac138's Photo mibac138 20 Jan 2013

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
			slot = slot + 1
			turtle.select(slot)
  end
  turtle.placeDown()
end
;)
Quote

W00dyR's Photo W00dyR 20 Jan 2013

View Postmibac138, on 20 January 2013 - 07:56 AM, said:

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
			slot = slot + 1
			turtle.select(slot)
  end
  turtle.placeDown()
end
;)

Alright thanks, that solves one question, now it still scrolls through the entire inventory before placing a block, how do I prevent this?
Quote

mibac138's Photo mibac138 20 Jan 2013

Maybe this but i don't know if it help :/

local function blockbelow()
for i=1, 16 do
  if turtle.getItemCount(slot) >=1 then
   turtle.placeDown()
  else
	slot = slot + 1
  end
end

Script Kiddie! :D
Quote

W00dyR's Photo W00dyR 20 Jan 2013

View Postmibac138, on 20 January 2013 - 08:23 AM, said:

Maybe this but i don't know if it help :/

local function blockbelow()
for i=1, 16 do
  if turtle.getItemCount(slot) >=1 then
   turtle.placeDown()
  else
	slot = slot + 1
  end
end

Script Kiddie! :D

Grats on your rank,

OT: I edited it so it would work properly, this does solve the issue, if it wasn't for the time it spend doing the checks 16 times, so it goes even slower :P

I tried editing to make it work quick, but I can't figure it out :/
Quote

FF0084's Photo FF0084 20 Jan 2013

View PostW00dyR, on 20 January 2013 - 08:03 AM, said:

View Postmibac138, on 20 January 2013 - 07:56 AM, said:

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
			slot = slot + 1
			turtle.select(slot)
  end
  turtle.placeDown()
end
;)

Alright thanks, that solves one question, now it still scrolls through the entire inventory before placing a block, how do I prevent this?

Simple: make 'slot' variable a global variable ( I mean, the local variable but outside the function scope ). The modified version:

local slot = 1

local function blockbelow()
  while turtle.getItemCount(slot) == 0 do
	 slot = slot + 1
	 if not turtle.select(slot) then
		   -- the slot is outside the 1-16 limit, thus: there's no more blocks available.
		   -- handle it for yourself, I'm returning false here
		   return false
	 end
  end
  turtle.placeDown()
  return true
end


--build 1x200 bridge

for i=1,200 do
   if not blockbelow() then
	   --there's no blocks, quit prematurely
	   break
   end
   turtle.forward()
end

I hope I didn't make any typos; I was writing it on the fly. Cheers
Quote

mibac138's Photo mibac138 20 Jan 2013

So, i think this code (below) is the best ;)

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
						slot = slot + 1
						turtle.select(slot)
  end
  turtle.placeDown()
end

@Edit Or no the best code :X
Quote

crazyguymgd's Photo crazyguymgd 20 Jan 2013

View Postmibac138, on 20 January 2013 - 08:34 AM, said:

So, i think this code (below) is the best ;)

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
						slot = slot + 1
						turtle.select(slot)
  end
  turtle.placeDown()
end

@Edit Or no the best code :X

If the inventory is empty, slot will increase until it's 17, then you will try to select slot 17 and cause an error. So before selecting slot, check if it is 17 and call a go back and get items from chest function or something like that.
Quote

W00dyR's Photo W00dyR 20 Jan 2013

View PostFF0084, on 20 January 2013 - 08:33 AM, said:

View PostW00dyR, on 20 January 2013 - 08:03 AM, said:

View Postmibac138, on 20 January 2013 - 07:56 AM, said:

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
			slot = slot + 1
			turtle.select(slot)
  end
  turtle.placeDown()
end
;)

Alright thanks, that solves one question, now it still scrolls through the entire inventory before placing a block, how do I prevent this?

Simple: make 'slot' variable a global variable ( I mean, the local variable but outside the function scope ). The modified version:

local slot = 1

local function blockbelow()
  while turtle.getItemCount(slot) == 0 do
	 slot = slot + 1
	 if not turtle.select(slot) then
		   -- the slot is outside the 1-16 limit, thus: there's no more blocks available.
		   -- handle it for yourself, I'm returning false here
		   return false
	 end
  end
  turtle.placeDown()
  return true
end


--build 1x200 bridge

for i=1,200 do
   if not blockbelow() then
	   --there's no blocks, quit prematurely
	   break
   end
   turtle.forward()
end

I hope I didn't make any typos; I was writing it on the fly. Cheers

Ahhh, told you it was something really easy that I just didnt realise :P,

Thanks all for the help :)
Quote