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

Selecting problem

Started by W00dyR, 20 January 2013 - 06:34 AM
W00dyR #1
Posted 20 January 2013 - 07:34 AM
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 :)/>
mibac138 #2
Posted 20 January 2013 - 07:56 AM

local function blockbelow()
  slot = 1
  while turtle.getItemCount(slot) == 0 do
			slot = slot + 1
			turtle.select(slot)
  end
  turtle.placeDown()
end
;)/>
W00dyR #3
Posted 20 January 2013 - 08:03 AM

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?
mibac138 #4
Posted 20 January 2013 - 08:23 AM
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/>
W00dyR #5
Posted 20 January 2013 - 08:33 AM
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 :/
FF0084 #6
Posted 20 January 2013 - 08:33 AM

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
mibac138 #7
Posted 20 January 2013 - 08:34 AM
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
crazyguymgd #8
Posted 20 January 2013 - 08:48 AM
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.
W00dyR #9
Posted 20 January 2013 - 08:49 AM

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 :)/>