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

Title: [Lua][Error] Item Count -1 Out of Range

Started by Greyhat, 10 March 2013 - 12:35 PM
Greyhat #1
Posted 10 March 2013 - 01:35 PM
Solved! - Thank you Immibis
Lesson Learned - Practice like you play. If you program relies on having items in the turtle, have them there for every test.

The plan for this program is to have it build 3 different structure types. It will do this using tables that contain the definitions for each possible type of "pillar" that makes up each building, then building pillars according to the coordinates specified in the program. I suspect that my problem is related to my implementation of the tables, given my lack of experience in using them

Attempting to compile results in "turtle:18: Item count -1 out of range". I can't find any information on the error, so I don't even know what in my code I should be looking at to try to fix it. I've tried commenting out the for loop that extends from line 12 to line 56, and still get an error on line 18. Any help at all is greatly appreciated!

edit - Updated code in pastebin, and updated link, still having the same issue.
edit2 - Solved!
Pharap #2
Posted 10 March 2013 - 02:18 PM
line 133, you declare for i = 0,9 then try to call turtle.select(0)
Turtles have 16 item slots declared as 1-16, meaning 0 is an invalid number.
Try changing to for i = 1,9 instead.
I'm assuming you're used to more C-like languages where arrays are 0-indexed right? lol

ofc that might not be the only bug, but it's the only one I've spotted so far.
If that doesn't work, just say so and I'll have another quick scan.
Greyhat #3
Posted 10 March 2013 - 03:01 PM
I'm assuming you're used to more C-like languages where arrays are 0-indexed right? lol

Indeed I am. Even though I know that lua is 1-indexed, I keep making that mistake.


If that doesn't work, just say so and I'll have another quick scan.

Fixed that, and am still getting the exact same error, "turtle:18: Item count -1 out of range" =/.
Pharap #4
Posted 10 March 2013 - 04:11 PM
What version of lua are you using?
I've had a look through turtle, but in 1.481 line 18 doesn't seem like it's anything that would cause any issues.
(yeah, I haven't updated yet, I'm waiting a bit because it means updating all my mods and I'm lazy lol)
Greyhat #5
Posted 10 March 2013 - 05:45 PM
I'm using whatever notepad++ is on lol. Presumably 5.2, but I'm not certain. Line 18 is just a comment though, adding to my confusion. I appreciate your help by the way.
Pharap #6
Posted 10 March 2013 - 06:04 PM
I'm using whatever notepad++ is on lol. Presumably 5.2, but I'm not certain. Line 18 is just a comment though, adding to my confusion. I appreciate your help by the way.

Notepad++ doesn't determine what version of the mod you are using.
Go to "/.minecraft/mods" and it should say what version of computercraft you're running.
The line 18 it refers to is in "turtle", which is in the computercraft zip in the mods folder in "/lua/rom/apis/turtle/turtle".
I like giving help, it makes me feel purposeful and restores my faith in people.
immibis #7
Posted 10 March 2013 - 09:23 PM
It might be this:

if i==1 then
  for j=10,12 do
    turtle.select(j)
    turtle.transferTo(1,(turtle.getItemCount(j)-1))
  end
end
Are slots 10, 11 or 12 empty?
Greyhat #8
Posted 11 March 2013 - 04:54 AM
What version of lua are you using?
I've had a look through turtle, but in 1.481 line 18 doesn't seem like it's anything that would cause any issues.
(yeah, I haven't updated yet, I'm waiting a bit because it means updating all my mods and I'm lazy lol)

ahh, I thought you were asking about Lua itself. CC version is 1.5
Greyhat #9
Posted 11 March 2013 - 05:07 AM
It might be this:

if i==1 then
  for j=10,12 do
	turtle.select(j)
	turtle.transferTo(1,(turtle.getItemCount(j)-1))
  end
end
Are slots 10, 11 or 12 empty?

They were, and filling them seems to have fixed it. I'm a bit confused as to why though. The program hasn't reached a point where that code would be run, so why would that throw an error if it's syntactically correct? also, why would that error show up as being on line 18? Thank you very much for your help by the way!
Pharap #10
Posted 11 March 2013 - 10:21 AM
They were, and filling them seems to have fixed it. I'm a bit confused as to why though. The program hasn't reached a point where that code would be run, so why would that throw an error if it's syntactically correct? also, why would that error show up as being on line 18? Thank you very much for your help by the way!


if i==1 then
  for j=10,12 do
	turtle.select(j)
	turtle.transferTo(1,(turtle.getItemCount(j)-1))
  end
end

It errors because if j is empty, turtle.getItemCount(j) returns 0, and 0-1 is -1, therefore you are attempting to transfer -1 items from slot j to slot 1, which is kind of impossible. Just make sure to test if the slot is empty.
Also, if you think the program shouldn't have reached there yet, open a file at the start and make the program log what it is doing at each step so you can see the order it's actually doing things in to make sure it's not doing things it shouldn't be.

Also the line 18 referred to is line 18 of the turtle API, not line 18 of your program. I'm assuming it's something related to the transferTo function.