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

item checking turtle thingy

Started by southiebob, 08 April 2014 - 09:49 PM
southiebob #1
Posted 08 April 2014 - 11:49 PM
Hi all

I think I messed up , mostly I have no idea whats going on with the programming but back in the day I did some java and well I am able to get the turtle to build my castle walls for me in a way that is though I wish to get the part working where it will check the item in slots 5 - 16 and if 5 is empty then move to slot 6 and so on . Well here is the script thingy I am working on I have messed with it but I get some funky errors can some one please have a look and help me ?


print("how long?")
a = read()
print("how high?")
b = read()
for i=1,a do

for i=1,b do
  print( "i is "..tostring(i) ) -- refuel script I coppied
  print ("CheckingFuel...")
  if turtle.getFuelLevel() < 5 then
  for i = 1, 4 do -- loop through the slots
  turtle.select(i) -- change to the slot
  if turtle.refuel(0) then -- if it's valid fuel
  local halfStack = math.ceil(turtle.getItemCount(i)/2) -- work out half of the amount of fuel in the slot
  turtle.refuel(halfStack) -- consume half the stack as fuel
  end
  end
  else
  print ("Enough fuel")
  end						  -- end refuel script I copied

turtle.select(5) -- select slot 5 after fuel check from slot 1 to 4


  z = 5		  -- check turtle slot for item it no item move to next slot
  turtle.getItemCount(z)
  if turtle.getItemCount(z) == 0 then
  z = z + 1
  turtle.selcect(z)
  else
  write("slot has blocks in")
  end	-- end check for item

  turtle.placeRight() -- build tower layer
  turtle.placeLeft()  -- build tower layer
  turtle.up()		 -- build tower layer
  turtle.placeDown()  -- build tower layer
  end
turtle.forward() --move turtle back down to start new collom
for i=1,b do
  turtle.down()
  end
end

thank you all :)/>
apemanzilla #2
Posted 08 April 2014 - 11:59 PM
Could you provide the errors?
southiebob #3
Posted 09 April 2014 - 12:01 AM
sure it reads wall : 51 : attempt to call nil
Agoldfish #4
Posted 09 April 2014 - 12:13 AM
sure it reads wall : 51 : attempt to call nil
I'm kinda confused, there aren't 51 lines in this program…
apemanzilla #5
Posted 09 April 2014 - 12:15 AM
sure it reads wall : 51 : attempt to call nil
Near the middle:
turtle.selcect(z)
Change that to:
turtle.select(z)

sure it reads wall : 51 : attempt to call nil
I'm kinda confused, there aren't 51 lines in this program…
I'm assuming part of the code is missing from the provided code.
Edited on 08 April 2014 - 10:15 PM
southiebob #6
Posted 09 April 2014 - 12:18 AM
Hi sorry yea I cut something out by accidently it shows as line 38 now hmmm apemanzilla seems my spelling failed me again there adjusting it now ty :3
apemanzilla #7
Posted 09 April 2014 - 12:22 AM
Glad I could help, tell me if there's still errors.
Edited on 08 April 2014 - 10:23 PM
southiebob #8
Posted 09 April 2014 - 12:32 AM
error is gone thanks loads though now it checks though just noticed that I might have the check item in the wrong place here is the code as I have it now

print("how long?")
a = read()
print("how high?")
b = read()
z = 5
for i=1,a do

for i=1,b do
  print( "i is "..tostring(i) ) -- refuel script I coppied
  print ("CheckingFuel...")
  if turtle.getFuelLevel() < 5 then
  for i = 1, 4 do -- loop through the slots
  turtle.select(i) -- change to the slot
  if turtle.refuel(0) then -- if it's valid fuel
  local halfStack = math.ceil(turtle.getItemCount(i)/2) -- work out half of the amount of fuel in the slot
  turtle.refuel(halfStack) -- consume half the stack as fuel
  end
  end
  else
  print ("Enough fuel")
  end						  -- end refuel script I copied

turtle.select(z) -- select slot 5 after fuel check from slot 1 to 4


			-- check turtle slot for item it no item move to next slot
  turtle.getItemCount(z)
  if turtle.getItemCount(z) == 0 then
  z = z + 1
  turtle.select(z)
  else
  write("slot has blocks in")
  end	-- end check for item
  turtle.turnRight()
  turtle.place() -- build tower layer
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.place()  -- build tower layer
  turtle.turnRight()
  turtle.up()		 -- build tower layer
  turtle.placeDown()  -- build tower layer
  end
turtle.forward() --move turtle back down to start new collom
for i=1,b do
  turtle.down()
  end
end

though I think I messed it up some how , it does check if slot 5 is empty and then moves to slot 6 , but then just carries on with the rest of the program instead of checking slot 6 as well before starting to build sigh sorry I am new at this .

as well sorry for a late reply I must head to bed must be at work again in about 2hrs :)/>

thanks for the help I am slowly getting the hang of how this works

ps well it looks much better then what I started with

turtle.select(5)
if turtle.select(5) < 5 then
  turtle.select(6)
if turtle.select(6) < 5 then
  turtle.select(7)
if turtle.select(7) < 5 then
  turtle.select(8)
if turtle.select(8) < 5 then
  turtle.select(9
if turtle.select(9) < 5 then
  turtle.select(10)
if turtle.select(10) < 5 then
  turtle.select(11)
if turtle.select(11) < 5 then
  turtle.select(12)
if turtle.select(12) < 5 then
  turtle.select(13)
if turtle.select(13) < 5 then
  turtle.select(14)
if turtle.select(14) < 5 then
  turtle.select(15)
if turtle.select(15) < 5 then
  turtle.select(16)

and yes that failed horribly
Edited on 08 April 2014 - 10:35 PM
southiebob #9
Posted 09 April 2014 - 10:15 AM
Thank you all finally got it working :)/> here is how it looks now , and yes I know my spelling fails sorry about that


print("how long?")
a = read()	    --read number for use in lenth of tower
print("how high?")
b = read()	   --read number for use in hight of tower
check = 5	    -- define check start slot for turtle
for i=1,a do	    -- loop start for the lenth of tower
for i=1,b do	    -- loop start for hight of tower
print( "i is "..tostring(i) )	  -- refuel script I coppied
print ("CheckingFuel...")	 -- refuel script I copied
if turtle.getFuelLevel() < 5 then    -- refuel script I copied
for i = 1, 4 do -- loop through the slots   -- refuel script I copied , changed so that it only goes from slot 1 to 4 for fuel
turtle.select(i) -- change to the slot    -- refuel script I copied
if turtle.refuel(0) then -- if it's valid fuel   -- refuel script I copied
local halfStack = math.ceil(turtle.getItemCount(i)/2)   -- work out half of the amount of fuel in the slot -- refuel script I copied
turtle.refuel(halfStack) -- consume half the stack as fuel -- refuel script I copied
end	    -- refuel script I copied
end	    -- refuel script I copied
else	    -- refuel script I copied
print ("Enough fuel")	  -- refuel script I copied
end	    -- end refuel script I copied
turtle.select(check)	  -- turle select slot after refuel
if turtle.getItemCount(check) <=5 then    -- turtle check amount in slot check if less then 5 statment true
check = check + 1	  -- change check number for next slot
turtle.select(check)	  -- change to next slot
end	    -- end of turtle slot check if

turtle.turnRight()	  -- turn turtle for building
turtle.place()	    -- build tower layer
turtle.turnLeft()	  -- turn turtle for building
turtle.turnLeft()	  -- turn turtle for building
turtle.place()	    -- build tower layer
turtle.turnRight()	  -- turn turtle back to frunt facing posistion
turtle.up()			  -- move turtle for next layer
turtle.placeDown()	    -- finish building pref layer
end		 -- loop end for tower hight
turtle.forward()	   -- move turtle forward to get back to starting posistion
for i=1,b do	   -- loop for moving turtle back down , loops for hight amount
turtle.down()	   -- turtle moves down
end	    -- end of loop for turtle reset posistion
end	    -- end of loop for lenth of tower
Edited on 09 April 2014 - 08:23 AM
CometWolf #10
Posted 09 April 2014 - 02:24 PM

turtle.select(check)	  -- turle select slot after refuel
if turtle.getItemCount(check) <=5 then    -- turtle check amount in slot check if less then 5 statment true
check = check + 1		 -- change check number for next slot
turtle.select(check)	  -- change to next slot
end
This probably won't work very well if there is a gap in the slots. I'd suggest using a loop instead of an if statement.

local check = 5
while turtle.getItemCount(check) <= 5 do
  check = check+1
  if check > 16 then
    error"Out of blocks!"
  end
end
turtle.select(check)
southiebob #11
Posted 09 April 2014 - 02:42 PM

turtle.select(check)	  -- turle select slot after refuel
if turtle.getItemCount(check) <=5 then	-- turtle check amount in slot check if less then 5 statment true
check = check + 1		 -- change check number for next slot
turtle.select(check)	  -- change to next slot
end
This probably won't work very well if there is a gap in the slots. I'd suggest using a loop instead of an if statement.

local check = 5
while turtle.getItemCount(check) <= 5 do
  check = check+1
  if check > 16 then
	error"Out of blocks!"
  end
end
turtle.select(check)

hmmm changed my if statement to the loop one seems to work as well not bad , I am currently messing around with turtles since they are so much fun to play with :)/> every bit of extra code help :D/>