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

Handling Errors?

Started by SkyeLyner, 31 December 2015 - 08:40 AM
SkyeLyner #1
Posted 31 December 2015 - 09:40 AM
So this code is my personal quarry code where it mines 16 rows, moves over one, turns around, then mines 16 rows back. For 16 collums. (16x6 quarry) It is a much more efficient version of the excavate code, and is also programmed to measure whether or not its inventory is full every time it moves a block, and whether or not it needs to refuel. Pretty simple program. Me being a Java programmer I use the "Try Catch" blocks to handle errors and prevent them from crashing my code. But on line (31) when it goes to check the 16th inventory slot, it will sometimes check it as empty and return a "nil" value to the turtle. Therefore crashing my the program.

All I need help with is the correct syntax I should be using to "catch" the error when it checks its inventory. I have searcher far and wide! For the correct syntax I would use in this situation to prevent the error from crashing the turtle. I don't want/need any help with the rest of the code (unless you see an obvious error) I am trying to do most of this on my own. ITS FUN! :)/> I love programming and most of what I know is self taught and I am taking classes for java. So any help you can provide would be great. (I've been up all night. Its 3:36 AM for christ's sake. would love to get some sleep)

Here is a link to the code (copy pasting it removes all of the spacing and organization)

Pastebin: http://pastebin.com/KpSzh2Rd

Thanks in advance!

Edit: I forgot to put the error code in. Here it is: quarry:31:Expected number
Edited on 31 December 2015 - 02:50 PM
KingofGamesYami #2
Posted 31 December 2015 - 02:48 PM
The closest thing to try catch in lua would be pcall( func, … ).

In this case, pcall is not useful. Instead, you should use an existance check:


            if inventory >= 1 then

Adding the check:


            if inventory and inventory >= 1 then


This way, it will never attempt to compare a nil value to a number.
SkyeLyner #3
Posted 31 December 2015 - 03:49 PM
I noticed I neglected to post the error code in the original post. My apologies. I am posting it here.

quarry:31: Expected number

Now I have tested out this solution by editing line 32.

if inventory and inventory >= 1 then
(this is line 32)

and I can verify that this wont work, specifically because the error occurs BEFORE this line. Thanks for the quick response, but I believe the error is occuring because when I am changing the value of inventory on line 31 sometimes the bottom right inventory slot in the turtle is empty. Therefore giving it the value of nil and crashing my turtle.

Again, sorry for not putting the error code in the beginning post.
KingofGamesYami #4
Posted 31 December 2015 - 05:42 PM
On line 31, you are calling


turtle.getItemCount()

Which is throwing an error:


Expected number

'Expected' meaning that function wanted an argument that it didn't get. 'number' meaning it wanted a number.

According to the documentation on getItemCount, it wants the number of the slot you wish to check.

A note: You don't have to select slot 16 to use turtle.getItemCount. You just have to supply 16 to turtle.getItemCount when calling it.
SkyeLyner #5
Posted 31 December 2015 - 06:11 PM
Thanks a ton, I was able to resolve the problem. But now later on in the code I have found a new error (after redesigning this specific portion of code)
The portion of code I am trying to change is between lines 47 - 71

After reprogramming this area do to a logic error I had been having (no errors, just not working properly) This area of code is trying to determine whether or not it needs to turn left or right at the end of a row of mining.

if evenOdd = 1 then
(turn to the right)
else (it would be 2)
(turn to the left)
end

That is the simplest version of the code.
Here is the ACTUALL code: http://pastebin.com/bkDd11uC
Here is the error: For input string: "1s"

I wouldnt normally consider myself an amateur programmer, but lua…lua makes me hurt. Its considered an easy program, but I am used to the object oriented code of java. I appreciate the help.
Dragon53535 #6
Posted 31 December 2015 - 06:52 PM
A couple of things, one, your if statement is actually

if evenOdd == 2 then
else
end
Second, your while loops in the if statements can be rewritten as this:

while turtle.detect() do
  turtle.dig()
end
However, if there for whatever reason is a mob, in the case of a cave, I'd use the normal way of going forwards.

while not turtle.forward() do
  turtle.dig()
  turtle.attack()
end

As for your logic error, you're just telling us it's not doing what you want, what specifically is it doing?
HPWebcamAble #7
Posted 31 December 2015 - 07:00 PM
Here is the error: For input string: "1s"

A quick search for '1s' in your code found this on line 26:

collum = collum + 1s

I'd imagine this is a simple typo (happens to me all the time). Just remove the 's'.
SkyeLyner #8
Posted 31 December 2015 - 07:05 PM
wow, I am really dissapointed I neglected to notice the "s". thanks for pointing it out.

So the quarry digs in a zig zag pattern (sorry if I wasnt clear about this). if we imagine the quarry from a birds eye view as a 16x16 grid it will be easier to explain using X and Y coordinates.
With (1,1) being the bottom left of the grid and (16,16) being the top right.

The turtle starts at (1,1) moves towards (1,16) moves over to (2,16) then back down to (2,1) over to (3,1) up to (3,16) continuing in this pattern until it reaches the end of the grid. Then moving down a few layers in the quarry and repeating these steps. This is what the turtle should be doing. Here is what it is actually doing.

Starts at (1,1) moves towards (1,16) over to (2,16) back down to (2,1) over to (1,1) up to (1,16) over to (2,16) down to (2,1) over to (1,1) and repeats this pattern until it is time for it to move down a layer. Basically what it is doing is it is only turning to the right at the end of each row.

I hope that makes sense. Here is a picture of the movement it should be making.

Edited on 31 December 2015 - 06:20 PM
SkyeLyner #9
Posted 31 December 2015 - 11:22 PM
I finally figured it out. I will be sure to add it to the programs page if anybody is interested. I want to be able to give back to this community. Thanks everyone!