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

[LUA] Can't figure out what's wrong with this function

Started by sixalpha, 22 January 2013 - 01:58 PM
sixalpha #1
Posted 22 January 2013 - 02:58 PM
I can't figure out what's wrong with this. getItemCount() returns an integer according to the wiki. Please help.
Error message says:

pad:19: Expected number

Refering to the line

	count = count + turtle.getItemCount()


function checkInv()
  turtle.select(1)
  local count = 0
  for i = 1, 16 do
	turtle.select(i)
	count = count + turtle.getItemCount()
  end
  if count >= totalblocks then
	return true
  else
	return false
  end
end

The (incomplete) program:
Spoiler

-- START VARIABLES --
local width = 0
local length = 0
local totalblocks = 0
local fuellevel = 0
local xpos = 0
local ypos = 0
local invslot
-- END VARIABLES --
-- START FUNCTIONS --
function checkInv()
  turtle.select(1)
  local count = 0
  for i = 1, 16 do
	turtle.select(i)
	count = count + turtle.getItemCount()
  end
  if count >= totalblocks then
	return true
  else
	return false
  end
end
function placeX(width)
  if turtle.detect() then
	turtle.dig()
	turtle.forward()
	xpos = xpos + 1
	turtle.placeDown()
  end
end
-- END FUNCTIONS --
print("Welcome to the pad builder!")
print("---------------------------")
print("How wide should the pad be?")
width = io.read()
print("Width " .. width)
print("How long should the pad be?")
length = io.read()
print("Width " .. width)
print("Length " .. length)
totalblocks = width * length
print("You need " .. totalblocks .. " blocks to accomplish this.")
fuellevel = turtle.getFuelLevel()
print("You have " .. fuellevel .. " fuel.")
print("You need " .. totalblocks .. " fuel.")
if fuellevel < totalblocks then
  print("Not enough fuel to perform this action.  Exiting.")
  sleep(5)
  shell.exit()
end
if not checkInv() then
print("Not enough blocks")
end

Thanks for the help. I have a lot to learn for sure!
theoriginalbit #2
Posted 22 January 2013 - 03:01 PM
turtle.getItemCount() requires a parameter… so instead of changing slots just put i in getItemCount
sixalpha #3
Posted 22 January 2013 - 03:06 PM
I really need to learn how to read the wiki. Sorry and thanks!
theoriginalbit #4
Posted 22 January 2013 - 03:08 PM
I really need to learn how to read the wiki. Sorry and thanks!
Thats ok :)/>