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

attempt to preform arithmetic __mul on number and nil?

Started by Avous, 18 January 2013 - 04:48 AM
Avous #1
Posted 18 January 2013 - 05:48 AM
EDIT: I've found the error and corrected it (I misspelled a reference i was making to gapNum) but now it's telling me
"Lumberjack:2: 'for' limit must be a number"

local function treeChop()
  for i=1, treeNum do
	turtle.select(1)
	turtle.compare()
	  if turtle.compare() then
		turtle.dig()
		turtle.forward()
		while turtle.detectUp() do
		  turtle.digUp()
		  turtle.up()
		end
		while not turtle.detectDown() do
		  turtle.down()
		end
		turtle.back()
		turtle.select(2)
		turtle.place()
		turtle.select(3)
		turtle.place()
	  end
	if treeNum > 1 then
	  turtle.turnRight()
	  for i=1, gapNum+1 do
		turtle.forward()
	  end
	turtle.turnLeft()
	end
  end
end
local function rowSwap()
  if rowNum > 1 then
	turtle.turnLeft()
	turtle.forward()
	turtle.turnRight()
	for i=1, gapNum do
	  turtle.forward()
	end
	turtle.turnLeft()
	for i=1, treeGap-1 do
	  turtle.forward()
	end
	turtle.turnRight()
	turtle.turnRight()
  else
  sleep(1)
  end
end
local function goHome()
  if treeNum > 1 then
	turtle.turnLeft()
	for i=1, treeGap-1 do
	  turtle.forward()
end
	turtle.turnLeft()
  end
  if rowNum > 1 then
	for i=1, rowGap do
	  turtle.forward()
	end
	turtle.turnRight()
	turtle.forward()
	turtle.turnRight()
  end
  for i=firstItem,lastItem do
	turtle.select(i)
	turtle.dropDown()
  end
end
local firstItem,lastItem = 4, 16
term.clear()
print("Lumberjack: Please confirm I have the type of Logs in Slot 1, Saplings in Slot 2, and Bonemeal in Slot 3.")
print("Lumberjack: How many Trees are in a Row?")
local treeNum = tonumber(read())
term.clear()
print("Lumberjack: How many Rows are there?")
local rowNum = tonumber(read())
term.clear()
print("Lumberjack: How big is the gap inbetween trees?")
local gapNum = tonumber(read())
term.clear()
local treeGap = (treeNum - 1) * gapNum + treeNum - 1
local rowGap = (rowNum - 1) * gapNum + rowNum - 1
while true do
  for i=1, rowNum-1 do
	treeChop()
	rowSwap()
  end
  goHome()
  sleep(1)
  print("Lumberjack: Completed, Should I run this task again?\n(Y/N)")
  local evt, Rerun = os.pullEvent("char")
  Rerun = string.lower(Rerun)
  if Rerun == "y" then
	sleep(1)
term.clear()
  elseif Rerun == "n" then
	sleep(1)
term.clear()
	break
  end
end

Heeeeelp Q.Q
jewelshisen #2
Posted 18 January 2013 - 05:54 AM
Did you define treeNum as a Number?
Avous #3
Posted 18 January 2013 - 05:55 AM
Did you define treeNum as a Number?

Yup! it's 100% user defined it requests it before it attempts to use it.
jewelshisen #4
Posted 18 January 2013 - 05:56 AM
Did you define treeNum as a Number?

Yup! it's 100% user defined it requests it before it attempts to use it.

Can you show me how you defined it?
Avous #5
Posted 18 January 2013 - 05:57 AM
Did you define treeNum as a Number?

Yup! it's 100% user defined it requests it before it attempts to use it.

Can you show me how you defined it?

print("Lumberjack: How many Trees are in a Row?")
local treeNum = tonumber(read())
jewelshisen #6
Posted 18 January 2013 - 05:58 AM
weird… That should be working. Have you tried removing the local tag?
Avous #7
Posted 18 January 2013 - 06:00 AM
Yeah I got past it but I'm on the new problem saying "Lumberjack:2: 'for' limit must be a number" but I fixed my original error I misspelled a variable
jewelshisen #8
Posted 18 January 2013 - 06:01 AM
Strange. Let me do a little testing and report back.
jewelshisen #9
Posted 18 January 2013 - 06:06 AM
Can you put up the full code someplace like pastebin and let me look it over?

I made a tiny test program that should be working just like the problem part of yours should and it is working fine so I am not sure what or where the error is.
Avous #10
Posted 18 January 2013 - 06:09 AM
I know right? I mean it has to exist but I can't see where I screwed up a number or a variable I believe I'm "for" correctly in all the ones I "CTRL-F" 'd
jewelshisen #11
Posted 18 January 2013 - 06:11 AM
I know right? I mean it has to exist but I can't see where I screwed up a number or a variable I believe I'm "for" correctly in all the ones I "CTRL-F" 'd

I don't know. Like I said I'll look over the full code and see if I can't find the problem.
remiX #12
Posted 18 January 2013 - 06:15 AM
A situation happened to some other guy where he wanted to use a global within a function that was defined
outside the function hoping it would work, but it didn't. The way he fixed it was by changing the arguments
of the function for the number to use within the loop.

So do this:

local function treeChop(num)
  for i=1, num do
        turtle.select(1)
        turtle.compare()
          if turtle.compare() then
                turtle.dig()
                turtle.forward()
                while turtle.detectUp() do
                  turtle.digUp()
                  turtle.up()
                end
                while not turtle.detectDown() do
                  turtle.down()
                end
                turtle.back()
                turtle.select(2)
                turtle.place()
                turtle.select(3)
                turtle.place()
          end
        if num > 1 then
          turtle.turnRight()
          for i=1, gapNum+1 do
                turtle.forward()
          end
        turtle.turnLeft()
        end
  end
end

Call it like:

treeChop(treeNum)
jewelshisen #13
Posted 18 January 2013 - 06:18 AM
A situation happened to some other guy where he wanted to use a global within a function that was defined
outside the function hoping it would work, but it didn't. The way he fixed it was by changing the arguments
of the function for the number to use within the loop.

So do this:

local function treeChop(num)
  for i=1, num do
		turtle.select(1)
		turtle.compare()
		  if turtle.compare() then
				turtle.dig()
				turtle.forward()
				while turtle.detectUp() do
				  turtle.digUp()
				  turtle.up()
				end
				while not turtle.detectDown() do
				  turtle.down()
				end
				turtle.back()
				turtle.select(2)
				turtle.place()
				turtle.select(3)
				turtle.place()
		  end
		if num > 1 then
		  turtle.turnRight()
		  for i=1, gapNum+1 do
				turtle.forward()
		  end
		turtle.turnLeft()
		end
  end
end

Call it like:

treeChop(treeNum)

That may work but I manged to get a similar code to work just fine without needing the call.


local num = tonumber(read())
local function count()
  for i=1, num do
    print(i)
  end
end
count()


Same basic code but mine gives no error.
remiX #14
Posted 18 January 2013 - 06:22 AM
Now try this.

local function count()
  for i=1, num do
    print(i)
  end
end

local num = tonumber(read())

count()

Problem is that you define the function before getting the value of treeNum from read()
jewelshisen #15
Posted 18 January 2013 - 06:28 AM
Now try this.

local function count()
  for i=1, num do
	print(i)
  end
end

local num = tonumber(read())

count()

Problem is that you define the function before getting the value of treeNum from read()

Ah… You are correct! Seems I still have lots to learn about Lua.
Avous #16
Posted 18 January 2013 - 06:29 AM
Thanks! i got it workin
Mads #17
Posted 18 January 2013 - 08:14 AM
You could do this, though:


local num = nil -- Delcare num

local function count()
    for i = 1, num do
        -- do stuff
    end
end

num = tonumber(io.read())

count()