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

Help with function nil value

Started by Realmcoder, 27 January 2013 - 11:15 AM
Realmcoder #1
Posted 27 January 2013 - 12:15 PM
I can't seem to get this piece of code working. All the other files are there it just keeps saying that line 19 (the line in which i call my function) is calling a nul value

local Xsize
local Zsize
local Ysize
local file1 = fs.open("stats","r")
Xsize = tonumber(file1.readLine())
Ysize = tonumber(file1.readLine())
Zsize = tonumber(file1.readLine())
local fileName = file1.readLine()
local currentFile = 0
local currentLevel = 0
local currentX = 1
local wantedDirection = east
local currentY = 1
local face = file1.readLine()
file1.close()
local currentLine
local fileThingy
DoStuff()
function DoStuff()
  m()
end
function changeSlots()
  s = string.sub(currentLine,currentY,currentY)
  i = 0
  if s == "-" then
	i = 6
	if turtle.getItemCount(6) == 0 then
	  turtle.select(6+8)
	  turtle.dig()
	  turtle.place()
	  turtle.select(6)
	  turtle.suck()
	  turtle.select(6+8)
	  turtle.dig()
	  turtle.select(6)
  
  else
  
	i = tonumber(s)
	turtle.select(i)
	if turtle.getItemCount(i) == 0 then
	  turtle.select(i+8)
	  turtle.dig()
	  turtle.place()
	  turtle.select(i)
	  turtle.suck()
	  turtle.select(i+8)
	  turtle.dig()
	  turtle.select(i)
	end
  
  end  
end
function m()
  repeat
	fileThingy = fs.open(fileName..""..currentFile,"r")
	turtle.select(5)
	turtle.refuel()
	turtle.dig()
	turtle.select(5+8)
	turtle.place()
	turtle.select(5)
	turtle.suck()
	turtle.select(13)
	turtle.dig()
	turtle.select(5)
	for i = 0, Xsize do
	  currentLine = fileThingy.readLine()
	  for i = 0, Ysize do
		turtle.digDown()
		changeSlots()
		turtle.placeDown()
		repeat
		  turtle.dig()
		until turtle.dig() == false
		turtle.goForward()
		currentY = currentY+1
	  end
	  turtle.turnLeft()
	  turtle.turnLeft()
	  for i = 0, Xsize do
		repeat
		  turtle.dig()
		until turtle.dig() == false
		turtle.forward()
	  end
	  turtle.turnLeft()
	  turtle.forward()
	  turtle.turnLeft()
	  currentY = 0
	end
  currentFile = currentFile + 1
  turtle.turnLeft()
	for i = 0, Xsize do
  
	  turtle.dig()
	  turtle.forward()
  
	end
  fileThingy.close()
  until currentFile == Zsize
end
end
Cloudy #2
Posted 27 January 2013 - 01:05 PM
Moved to own topic.
OmegaVest #3
Posted 27 January 2013 - 02:42 PM
For this one, the problem is that the function are defined below the call. Move the function definitions above the rest of the code.

And, if you are going to be calling these functions from elsewhere, you need to put them into an api, or write them into those programs as well.


And, mind you, I'm mostly making guesses by assumptions I have about you problem. If that was confusing, it's no worse than trying to read your post for what the problem is.
Realmcoder #4
Posted 27 January 2013 - 03:18 PM
Your right, I wasn't very clear. The truth is, it would help is I could understand what was appending. Parts of the code were there form me messing around to see if I could fix the problem. But no matter where I put the function call in the program it always gives me an error staying that the line has nil value. I'm probably going to just make the function as the body of the program but I thought it might be nice if I could get an answer when I couldn't defunctionize it.
ChunLing #5
Posted 28 January 2013 - 05:53 PM
Define your functions first, then call them. Even though the identifier is in scope (due to global definition), the value is nil until the function is defined, and that doesn't happen until the function definition, which you've put after the call.

Lua is a very flexible language. You could have DoStuff = "a list of things to do" or m = 1, and be using those variables, and use them as a string and number variable respectively, and then define them both as functions and use them as functions. But the cost of that is that when you attempt to call the function before defining it, the identifier is still associated with nil (or whatever other value you've assigned it).