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

Arithmetic Error

Started by Birdgeek3, 03 January 2013 - 04:57 AM
Birdgeek3 #1
Posted 03 January 2013 - 05:57 AM
Back to askin a pro!

My torch placing function does not work now, I fixed the arithmetic sub problem!


-- Variables
tArgs = { ... }
local distance = (tArgs[1])
local togo = tonumber ( distance )
local num = 0
-- Functions
function torch ()
  if num == 9 then
	turtle.select(15)
	turtle.place()
	turtle.forward()
	num = 0
  end
end
function layer ()
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  turtle.turnLeft()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  turtle.turnRight()
  turtle.dig()
  turtle.up()
  turtle.dig()
  turtle.up()
  turtle.dig()
  turtle.turnLeft()
  turtle.down()
  turtle.down()
end
function tfuel(amount)
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel < 50 then
	turtle.select(16)
	turtle.refuel(amount)
	turtle.select(1)
  end
end



--Main Script
print ("this will dig a 3x3 tunnel for a specified amount of blocks")
print("put coal in slot 16 (bottom right) so the turtle will fuel it self")
print("Put torches in slot 15 so the turtle will light things up")
print("To verify, you want the turtle to dig" ..togo "blocks")
print("Press ENTER to begin")
local sEvent, param = os.pullEvent("key")
if sEvent == "key" then
  if param == 28 then
	repeat
	  tfuel(1)
	  torch()
	  num = num + 1
	  layer ()
	  togo = togo - 1
	until togo == 0
  end
end

I hope it's an easy fix!

Thanks
-Birdgeek
Orwell #2
Posted 03 January 2013 - 06:09 AM
The full error message is important, it contains the line at which the error occurred. My simple guess, have you actually given a number as parameter to the program?
Birdgeek3 #3
Posted 03 January 2013 - 06:23 AM
Full error is

tunnelDigger:7: attempt to perform arithmetix__sub on a numb and nil

This happens when I enter both 5 and 25
darkrising #4
Posted 03 January 2013 - 06:25 AM
line 6

you have - Functions instead of – Functions
darkrising #5
Posted 03 January 2013 - 06:28 AM
Also line 54 should be:
print("To verify, you want the turtle to dig" ..togo.. "blocks")

instead of
print("To verify, you want the turtle to dig" ..togo "blocks")
Birdgeek3 #6
Posted 03 January 2013 - 07:28 AM
line 6

you have - Functions instead of – Functions
Simple mistakes changes everything!
Thanks!
And now the torch placing function does not work.
Any clue?
remiX #7
Posted 03 January 2013 - 08:00 AM
No reason why it shouldn't work?

Did you maybe forget to place torches in slot 15? :P/>

Change the repeat loop to this and check if it prints anything

repeat
    tfuel(1)
    if num == 9 then
        turtle.select(15)
        turtle.place()
        turtle.forward()
        num = 0
        print("Placed torch!")
    else
        num = num + 1
    end
    layer ()
    togo = togo - 1
until togo == 0
 
Birdgeek3 #8
Posted 03 January 2013 - 08:35 AM
No reason why it shouldn't work?

Did you maybe forget to place torches in slot 15? :P/>

Change the repeat loop to this and check if it prints anything

repeat
	tfuel(1)
	if num == 9 then
		turtle.select(15)
		turtle.place()
		turtle.forward()
		num = 0
		print("Placed torch!")
	else
		num = num + 1
	end
	layer ()
	togo = togo - 1
until togo == 0

What happens is he places the torch and then breaks it right away
Here is my updated code

-- Variables
tArgs = { ... }
local distance = (tArgs[1])
local togo = tonumber ( distance )
local num = 0
-- Functions
function torch ()
  if num == 9 then
    turtle.select(15)
    turtle.dig()
    turtle.place()
    turtle.forward()
    turtle.select(1)
    num = 0
  end
end
function layer ()
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  turtle.turnLeft()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  turtle.turnRight()
  turtle.dig()
  turtle.up()
  turtle.dig()
  turtle.up()
  turtle.dig()
  turtle.turnLeft()
  turtle.down()
  turtle.down() 
end
function tfuel(amount)
  local fuelLevel = turtle.getFuelLevel()
  if fuelLevel < 50 then
    turtle.select(16)
    turtle.refuel(amount)
    turtle.select(1)
  end
end
 
 
 
--Main Script
print ("this will dig a 3x3 tunnel for a specified amount of blocks")
print("put coal in slot 16 (bottom right) so the turtle will fuel it self")
print("Put torches in slot 15 so the turtle will light things up")
print("To verify, you want the turtle to dig " ..togo.. " blocks")
print("Press ENTER to begin")
local sEvent, param = os.pullEvent("key")
if sEvent == "key" then
  if param == 28 then
    repeat
	  tfuel(1)
	  torch()
	  num = num + 1
	  print (num)
	  layer ()
	  togo = togo - 1
    until togo == 0
  end
end
remiX #9
Posted 03 January 2013 - 09:42 AM
It's because layer() is called after torch and the first command of layer() is turtle.dig(), so it places and then digs.

Make the torch be placed on the right or left:

function torch ()
  if num == 9 then
    turtle.select(15)
    turtle.dig()
    turtle.turnLeft()
    turtle.place()
    turtle.turnRight()
    turtle.forward()
    turtle.select(1)
    num = 0
  end
end
Birdgeek3 #10
Posted 03 January 2013 - 11:43 AM
It's because layer() is called after torch and the first command of layer() is turtle.dig(), so it places and then digs.

Make the torch be placed on the right or left:

function torch ()
  if num == 9 then
	turtle.select(15)
	turtle.dig()
	turtle.turnLeft()
	turtle.place()
	turtle.turnRight()
	turtle.forward()
	turtle.select(1)
	num = 0
  end
end
I fixed it :)/>
Thanks for the help :)/>