Jump to content




Selecting problem


8 replies to this topic

#1 W00dyR

  • Members
  • 135 posts

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

#2 mibac138

  • Members
  • 132 posts
  • LocationPoland

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
;)

#3 W00dyR

  • Members
  • 135 posts

Posted 20 January 2013 - 08:03 AM

 mibac138, 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?

#4 mibac138

  • Members
  • 132 posts
  • LocationPoland

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

#5 W00dyR

  • Members
  • 135 posts

Posted 20 January 2013 - 08:33 AM

 mibac138, 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 :/

#6 FF0084

  • New Members
  • 10 posts
  • LocationPoland

Posted 20 January 2013 - 08:33 AM

 W00dyR, on 20 January 2013 - 08:03 AM, said:

 mibac138, 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

#7 mibac138

  • Members
  • 132 posts
  • LocationPoland

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

#8 crazyguymgd

  • Members
  • 139 posts
  • LocationUSA

Posted 20 January 2013 - 08:48 AM

 mibac138, 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.

#9 W00dyR

  • Members
  • 135 posts

Posted 20 January 2013 - 08:49 AM

 FF0084, on 20 January 2013 - 08:33 AM, said:

 W00dyR, on 20 January 2013 - 08:03 AM, said:

 mibac138, 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 :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users