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

FS/IO API troubles

Started by Otoris, 08 January 2013 - 08:34 PM
Otoris #1
Posted 08 January 2013 - 09:34 PM

local VER = "0.4.1"
local x,y,z,face
term.clear()
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )

local function printUsage()
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
end

local tArgs = { ... }
if #tArgs < 1 then
  printUsage()
  return
else
  setGPS()
end

function setGPS()
  if #tArgs >= 4 then
	x = tArgs[1]
	y = tArgs[2]
	z = tArgs[3]
	face = tArgs[4]
	print(x,y,z,face)
	if x == nil or y == nil or z == nil or face == nil then
	  printUsage()
	  return
	end
	
	local coord = fs.open("pos", "w")
	coord.writeLine(x)
	coord.writeLine(y)
	coord.writeLine(z)
	coord.close()

	local dir = fs.open("face", "w")
	dir.writeLine(face)
	dir.close()
	
	print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
  else
	print( "Run \"setGPS <x> <y> <z> <face>\" to manually set the position." )
  end
end

Returns:

setGPS:33: attempt to index ? (a nil value)

This is driving me nuts, starting to think my brain is broke or this fs API is messed up…

Thanks in advance!
D3matt #2
Posted 08 January 2013 - 09:45 PM
Add a line to check if coord == nil and throw an error message if it is.
Otoris #3
Posted 08 January 2013 - 09:51 PM
Updated:

local VER = "0.4.2"
local x,y,z,face
term.clear()
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )

local function printUsage()
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
end

local tArgs = { ... }
if #tArgs < 1 then
  printUsage()
  return
else
  setGPS()
end

function setGPS()
  if #tArgs >= 4 then
    x = tArgs[1]
    y = tArgs[2]
    z = tArgs[3]
    face = tArgs[4]
    print(x,y,z,face)
    if x == nil or y == nil or z == nil or face == nil then
	  printUsage()
	  return
    end
  
    local coord = fs.open("pos", "w")
    if coord == nil then
	  print("coord returned nil")
	  return
    end
    coord.writeLine(x)
    coord.writeLine(y)
    coord.writeLine(z)
    coord.close()

    local dir = fs.open("face", "w")
    dir.writeLine(face)
    dir.close()
  
    print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
  else
    print( "Run \"setGPS <x> <y> <z> <face>\" to manually set the position." )
  end
end

Updated. Returns same error.

http://puu.sh/1L8UZ
theoriginalbit #4
Posted 08 January 2013 - 09:53 PM
Add a line to check if coord == nil and throw an error message if it is.
They are writing to file. It will create the file and the handle.

seems to me like the error would be caused by the fact that you are attempting to call setGPS() before the compiler even knows what it is… move the function declaration above the arg check and you should be good… :)/>

EDIT: Also put the line local tArgs = { … } near the top

EDIT 2: Tested it and it worked
Edited on 08 January 2013 - 08:56 PM
Otoris #5
Posted 08 January 2013 - 09:59 PM
Updated:

local VER = "0.4.1"
local x,y,z,face
term.clear()
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )
local function printUsage()
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
end
local tArgs = { ... }
if #tArgs < 1 then
  printUsage()
  return
end
function setGPS()
  if #tArgs >= 4 then
    x = tArgs[1]
    y = tArgs[2]
    z = tArgs[3]
    face = tArgs[4]
    print(x,y,z,face)
    if x == nil or y == nil or z == nil or face == nil then
	  printUsage()
	  return
    end
   
    local coord = fs.open("pos", "w")
    if coord == nil then
	  print("coord returned nil")
	  return
    end
    coord.writeLine(x)
    coord.writeLine(y)
    coord.writeLine(z)
    coord.close()
    local dir = fs.open("face", "w")
    dir.writeLine(face)
    dir.close()
   
    print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
  else
    print( "Run \"setGPS <x> <y> <z> <face>\" to manually set the position." )
  end
end
setGPS()

Returns http://puu.sh/1L8ZY

setGPS:35: attempt to index ? (a nil value)


+1 for logic error fix ^^
theoriginalbit #6
Posted 08 January 2013 - 10:02 PM
Umm are you sure thats all of your code? I'm not getting the issue and I'm using that…
Otoris #7
Posted 08 January 2013 - 10:04 PM
As a note… I'm using Tekkit Lite, is that a problem?
Updated with suggested edits:

local tArgs = { ... }
if #tArgs < 1 then
  printUsage()
  return
end
local VER = "0.4.1"
local x,y,z,face
term.clear()
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )
local function printUsage()
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
end
function setGPS()
  if #tArgs >= 4 then
	x = tArgs[1]
	y = tArgs[2]
	z = tArgs[3]
	face = tArgs[4]
	print(x,y,z,face)
	if x == nil or y == nil or z == nil or face == nil then
	  printUsage()
	  return
	end
  
	local coord = fs.open("pos", "w")
	--if coord == nil then
	  --print("coord returned nil")
	  --return
	--end
	coord.writeLine(x)
	coord.writeLine(y)
	coord.writeLine(z)
	coord.close()
	local dir = fs.open("face", "w")
	dir.writeLine(face)
	dir.close()
  
	print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
  else
	print( "Run \"setGPS <x> <y> <z> <face>\" to manually set the position." )
  end
end
setGPS()

Edit: Yup, all the code.
theoriginalbit #8
Posted 08 January 2013 - 10:05 PM
As a note… I'm using Tekkit Lite, is that a problem?

It shouldn't be a problem. Give me a sec, I'll make a few logic changes and another change and get you to try it and see if it works…
Alekso56 #9
Posted 08 January 2013 - 10:06 PM
Meh, wouldn't it be easier to write it like this?


local VER = "0.4.1"
local x,y,z,face
term.clear()
term.setCursorPos(1,1)
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )
local tArgs = { ... }
if #tArgs < 4 then
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
  return
elseif #tArgs >= 4 then
	    x = tArgs[1]
	    y = tArgs[2]
	    z = tArgs[3]
	    face = tArgs[4]
	    print(x,y,z,face)
	    if x == nil or y == nil or z == nil or face == nil then
		  return
	    end
	    local coord = fs.open("pos", "w")
	    coord.writeLine(x)
	    coord.writeLine(y)
	    coord.writeLine(z)
	    coord.close()
	    local dir = fs.open("face", "w")
	    dir.writeLine(face)
	    dir.close()
	   
	    print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
end
Otoris #10
Posted 08 January 2013 - 10:09 PM
Meh, wouldn't it be easier to write it like this?


local VER = "0.4.1"
local x,y,z,face
term.clear()
term.setCursorPos(1,1)
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )
local tArgs = { ... }
if #tArgs < 4 then
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
  return
elseif #tArgs >= 4 then
		x = tArgs[1]
		y = tArgs[2]
		z = tArgs[3]
		face = tArgs[4]
		print(x,y,z,face)
		if x == nil or y == nil or z == nil or face == nil then
		  return
		end
		local coord = fs.open("pos", "w")
		coord.writeLine(x)
		coord.writeLine(y)
		coord.writeLine(z)
		coord.close()
		local dir = fs.open("face", "w")
		dir.writeLine(face)
		dir.close()
	  
		print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
end
Yes, yes it would be easier ^^

Same error though with your code. Now on line 22.

Also should mention this is on a mining turtle.
theoriginalbit #11
Posted 08 January 2013 - 10:15 PM
ok heres what I've put together. logic cleanup… similar to Alekso56 … and changed fs to io ( some people have issues with io, some with fs )

I've commented some lines with explanations

http://pastebin.com/F8rthGV7
Alekso56 #12
Posted 08 January 2013 - 10:23 PM
Added a check for exsisting data .-.

local VER = "0.4.1"
local x,y,z,face
term.clear()
term.setCursorPos(1,1)
print( "The Otoris Corperation GPS\nCopyright 2013 The Otoris Corp.\nVer: ".. VER )
local tArgs = { ... }
if fs.exists("pos" or "face") then
   fs.delete("pos")
   fs.delete("face")
   print("Previous GPS data deleted")
elseif #tArgs < 4 then
  term.clear()
  print( " - Usage - " )
  print( "setGPS <x> <y> <z> <face>" )
  return
elseif #tArgs >= 4 then
	    x = tArgs[1]
	    y = tArgs[2]
	    z = tArgs[3]
	    face = tArgs[4]
	    print(x,y,z,face)
	    if x == nil or y == nil or z == nil or face == nil then
		  return
	    end
	    local coord = fs.open("pos", "w")
	    coord.writeLine(x)
	    coord.writeLine(y)
	    coord.writeLine(z)
	    coord.close()
	    local dir = fs.open("face", "w")
	    dir.writeLine(face)
	    dir.close()
		 print( "Position X:"..x..", Y:"..y.." and Z:"..z.."\n Facing "..face.."\nSET SUCCESS" )
end
Otoris #13
Posted 08 January 2013 - 10:27 PM
Even cleaner!

I got the same freaking error. Raged a bit, tried running up to my Advanced Computer and running your version OriginalBIT. Worked perfect… Ran back to my turtle… Didn't work. Same exact error. Broke the turtle, replaced it, redownloaded the script. AND IT FREAKING WORKS NOW!

WHY, I don't know. But it was rage inducing ^^ <3

Thanks again! It is a sexy small piece of code now.
theoriginalbit #14
Posted 08 January 2013 - 10:32 PM
Ahhh I think I know what the issue was! lol. The file handle could have been open from previous runs where it errored. So it would have had issues opening and closing the file handle. If it was that a reboot of Minecraft would have fixed it…

As for the suggestion that Alesko56 just made about deleting the file first. it is not required, you can if you wish. but with the parameter "w" it overwrites the contents of the file. "r" reads, "w" overwrites and "a" appends…
Edited on 08 January 2013 - 09:32 PM
ChunLing #15
Posted 08 January 2013 - 11:49 PM
Shouldn't rebooting the computer/turtle in question also work to clear the environment and garbage collect any open file handles? Merely a technical question, cause that does sound like a rage inducing problem to have.
theoriginalbit #16
Posted 08 January 2013 - 11:51 PM
Shouldn't rebooting the computer/turtle in question also work to clear the environment and garbage collect any open file handles? Merely a technical question, cause that does sound like a rage inducing problem to have.
Nah I've heard stories of Minecraft holding onto the handle. I can neither confirm nor deny this, but some have said it so. However with CCEmu yeh a reboot seems to be enough…
ChunLing #17
Posted 09 January 2013 - 12:29 AM
Wow. I'm still in favor of persistence, but…wow. Hopefully completely clearing the environment during a reboot will be part of that.