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

Easier to Calculate Number of Items in Turtle Inventory Than This...

Started by Shadow_Assailant, 18 June 2013 - 08:16 PM
Shadow_Assailant #1
Posted 18 June 2013 - 10:16 PM
Hello! I've been working on a simple program to create and harvest dark iron blocks in Feed the Beast via a Mining Turtle. For reasons pertaining to this script I've been trying to figure out how to calculate the exact amount of items in the turtle's inventory. The first and probably most obvious idea that came to me in order to go about calculating this is:


items = turtle.getItemCount(1) + turtle.getItemCount(2) + turtle.getItemCount(3) + turtle.getItemCount(4) + turtle.getItemCount(5) + turtle.getItemCount(6) + turtle.getItemCount(7) + turtle.getItemCount(8) + turtle.getItemCount(9) + turtle.getItemCount(10) + turtle.getItemCount(11) + turtle.getItemCount(12) + turtle.getItemCount(13) + turtle.getItemCount(14) + turtle.getItemCount(15) + turtle.getItemCount(16)

As you can see this is quite lengthy and for this reason, I have not even tested this to see if it even works.

I came up with this then:


for i = 1, 16 do -- Scans each inventory slot
  items = turtle.getItemCount(i)
end

I figured this would work because the variable i makes the function account for slots 1-16, yet when I use this in a larger code:


local function inventory()
  local items = 0
  for i = 1, 16 do --Scan every inventory slot
	items = items + turtle.getItemCount(i)
  end
  print( items )
end
inventory()

I receive the error:

test:6: attempt to call number

Any help in fixing this would be much appreciated! :D/>
theoriginalbit #2
Posted 18 June 2013 - 11:06 PM
Is it possible that we can see your larger code (post to pastebin for ease), as that function should work fine.
Based off the error message the most likely cause is somewhere you do this
print = 5
or something along those lines. Then when you attempt to call the print function it doesn't actually exist anymore because you have overrode it with your number variable.
Shadow_Assailant #3
Posted 18 June 2013 - 11:09 PM
Well, the small code above is a separate "test" code from my larger, but I will get the larger one to you ASAP. :)/>
theoriginalbit #4
Posted 18 June 2013 - 11:13 PM
Ok and when running the small test code it still does it?

Try restarting the computer, making sure no other program is run, and then running the test code. It should work in this case.
Shadow_Assailant #5
Posted 18 June 2013 - 11:32 PM
So I did exactly as you said and my turtle gave me this:

reboot:4: attempt to call number

Well, I don't have a program called reboot. Lol. I picked the turtle back up and retried the test code and it actually worked this time. No idea what just happened. Peculiar bug. Not sure why it didn't work in the first place. Lol.

But as you asked, here's the pastebin link. :)/>
theoriginalbit #6
Posted 18 June 2013 - 11:36 PM
you do actually have a program called reboot. When you type "reboot" into the terminal it runs the program "rom/programs/reboot". That program uses the print command to say goodbye. It is better to use CTRL + R when there are problems as a hard reboot.


Ok so the problem isn't in that script. Are there any other programs that you have run on that turtle?
Shadow_Assailant #7
Posted 18 June 2013 - 11:42 PM
you do actually have a program called reboot. When you type "reboot" into the terminal it runs the program "rom/programs/reboot". That program uses the print command to say goodbye. It is better to use CTRL + R when there are problems as a hard reboot.

Weird. I did not know that. Lol.

Ok so the problem isn't in that script. Are there any other programs that you have run on that turtle?

The turtle with the "test" program only has "test" and the OreQuarry program by AustinKK and the Advanced Ore Finder by Henness. Neither of these are named "startup".

The Dark Iron Forge turtle only has the program "startup" which is used to create the dark iron.
theoriginalbit #8
Posted 18 June 2013 - 11:46 PM
Well if it's all working now clearly one of the other scripts was overriding the print function. next time it happens remember which program you used and take a look at the code.
Shadow_Assailant #9
Posted 18 June 2013 - 11:57 PM
The Dark Iron Forge turtle is still not working, though. I've reset Minecraft. I've rebooted the turtle. I've destroyed it and replaced it. The code still won't work.
theoriginalbit #10
Posted 19 June 2013 - 12:05 AM
What is the problem? Error? Or just doesn't work as expected?
Shadow_Assailant #11
Posted 19 June 2013 - 12:09 AM
turtle:18: Expected number

For testing purposes, I deleted the 5 long lines of comments before this line to see if for whatever reason they were interfering yet this still occurred.

Edit: Yeah. I have no idea as to why it says this.
theoriginalbit #12
Posted 19 June 2013 - 12:48 AM
in the checkInv function you don't define the variable b. It is localised to the while loop, so it is best to just pass it to the checkInv function like so.

Function declaration

local function checkInv( b )

Function usage (in the while loop)

checkInv( b )
Shadow_Assailant #13
Posted 19 June 2013 - 01:19 AM
in the checkInv function you don't define the variable b. It is localised to the while loop, so it is best to just pass it to the checkInv function like so.

Function declaration

local function checkInv( b )

Function usage (in the while loop)

checkInv( b )

Huh. Works perfect now. Thanks so much!
theoriginalbit #14
Posted 19 June 2013 - 01:22 AM
no problems :)/>