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

Turtle Item Count -2 Out Of Range (Line 18)

Started by FalloutBe, 01 August 2013 - 04:05 AM
FalloutBe #1
Posted 01 August 2013 - 06:05 AM
Hello,

I'm getting the error "Turtle item count -2 out of range" (on line 18)
This program has been running for hours, without any problems.
Suddenly the turtle stopped after digging down 4 times (so why did the first 4 work, and the 5th time not?!)

And now, every time I want to start up the turtle again, it stops with this error :/
The weird thing is that the error happens at a moment when turtle.refuelLoop() is NOT called.

function turtle.refuelLoop()
	refuelComplete = false
	while refuelComplete == false do
		turtle.select(1)
		if turtle.getFuelLevel() < 192 then
			if turtle.getItemCount(1) > 2 then						  <= LINE 18
				turtle.refuel(2)
				refuelComplete = true
			else
				cls()
				print("Not enough fuel. Minimum amount of coal: 3")
				write("Press Enter to retry.")
				read()
			end
		else
			refuelComplete = true
		end
	end
end
getItemCount uses a constant, so how can it become -2?

If required, you can find the full program here: http://pastebin.com/0pSNJqQy


Determined by the position of the turtle, and the actions it has done when the error happens, I think it must have been operating between lines 171 and 180
(since it placed the ender chest from slot 2, but did not put any item in the enderchest).
Again, this turtle has digged over 80 holes already and they all worked fine :S
jay5476 #2
Posted 01 August 2013 - 06:14 AM
please post the whole code and then I might be able to help you :)/>
FalloutBe #3
Posted 01 August 2013 - 06:18 AM
I did post it already.
GopherAtl #4
Posted 01 August 2013 - 10:54 AM
I'm guessing the full actual error was this:

"turtle:18: Turtle item count -2 out of range"

And the error came not from your program's line 18, but from the turtle api's line 18. A look at line 18 in the turtle api will just reveal thsi line number is useless for finding the issue, but the error message itself is quite clear and direct. You passed -2 as an item count to a turtle function somewhere.

A quick scan of the program and I spotted lines 175 and 179, where you turtle.drop (variable-3). This could easily pass -2 to drop, which would generate this error, if the variable is 1, and you do no checking to make sure it's >=3 first. Add if checks there and only drop if dirtLeft/stoneLeft are >= 3 first, and that should fix.
campicus #5
Posted 01 August 2013 - 10:58 AM
Try changing your function name from turtle.refuelLoop to refuelLoop. Perhaps that is doing something screwy?
GopherAtl #6
Posted 01 August 2013 - 11:06 AM
Try changing your function name from turtle.refuelLoop to refuelLoop. Perhaps that is doing something screwy?

We appreciate your desire to help, but guessing at answers is not usually constructive, and quite often results in misleading or explicitly wrong information being spread about. That it's turtle.refuelLoop is not a problem, and doesn't do anything "Screwy" unless you consider adding new functions to the turtle api to be "screwy."

this post and the previous may well self-destruct after some undefined interval, in accordance with the new AaP moderation policy. I applaud and encourage this.
FalloutBe #7
Posted 01 August 2013 - 11:20 AM
I'm guessing the full actual error was this:

"turtle:18: Turtle item count -2 out of range"

And the error came not from your program's line 18, but from the turtle api's line 18. A look at line 18 in the turtle api will just reveal thsi line number is useless for finding the issue, but the error message itself is quite clear and direct. You passed -2 as an item count to a turtle function somewhere.

A quick scan of the program and I spotted lines 175 and 179, where you turtle.drop (variable-3). This could easily pass -2 to drop, which would generate this error, if the variable is 1, and you do no checking to make sure it's >=3 first. Add if checks there and only drop if dirtLeft/stoneLeft are >= 3 first, and that should fix.
That's it! I added the check to it and now it always works fine ;)/>
should have posted the full error :/ my bad!

Thanks a lot for solving this ;)/>

@campicus: It also works like this :)/>
campicus #8
Posted 01 August 2013 - 11:58 AM
Sorry for my misleading suggestion, I had a look and missed what Gopher saw. Glad it's sorted :)/>