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

Identify out of object

Started by Riddle, 12 July 2015 - 02:31 PM
Riddle #1
Posted 12 July 2015 - 04:31 PM
Hello!

I was wondering how you make a turtle identify it is out of an object. I've made a program that identifies the fuel level but I was wondering how to have the turtle identify it is out of stone and then report this, or return to an area to receive more.

Thanks!
Lignum #2
Posted 12 July 2015 - 08:53 PM
turtle.getItemDetail will do the trick. This function only exists on ComputerCraft 1.6+, however.


local function isOutOfItem(item)
  for i=1,16 do --# 16 slots
	local detail = turtle.getItemDetail(i)
	if detail and detail.name == item then
	  return false --# Item was found in the current slot. This means we have it, so return false.
	end
  end

  return true --# Nothing was found, return true.
end

print("Are we out of stone?")
print(isOutOfItem("minecraft:stone") and "Yes" or "No") --# Prints "Yes" if the turtle is out of stone, "No" if not.

If you only need to check whether a certain slot is empty, you can use turtle.getItemCount.
Riddle #3
Posted 13 July 2015 - 12:37 AM
Thank you so much! This is exactly what I needed.