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

[Lua] File:lines() doesn't work anymore?

Started by darkroom, 05 March 2013 - 01:31 AM
darkroom #1
Posted 05 March 2013 - 02:31 AM
The error I get is "attempt to index ? (a nil value)" on line 6. Here is the code.

function loadLevel()
player = nil
pieces = nil
local file = io.open("/pec/levels/"..level..".nfa","r")
local y = 1
local row = { }
for lines in file:lines() do
  for x=1,#lines do
   row[y][x] = lines:sub(x,x)
  end
  local char = row[y][x]
  if char == "p" then
   player = loadSprite("pec/player.nfa",x,y)
  elseif char == "1" or char == "2" then
   table.insert(pieces,loadSprite(piecesPaths[tonumber(char)]))
  end
  y = y + 1
end
file:close()
end
Thank you for your time
-Dark
Cloudy #2
Posted 05 March 2013 - 02:37 AM
Appears to me that the file doesn't exist, hence is returning nil.
darkroom #3
Posted 05 March 2013 - 02:50 AM
Fixed the file not existing but now it is saying "index expected got nil" on line 9. The file looks like this
++++
+p +
+12+
++++
KaoS #4
Posted 05 March 2013 - 02:59 AM
the variable x is not defined…

EDIT derp… my bad
Edited on 05 March 2013 - 02:01 AM
theoriginalbit #5
Posted 05 March 2013 - 03:12 AM
You havent told it that row[y] is a table.
KaoS #6
Posted 05 March 2013 - 03:14 AM
and pieces is nil, on line 15 you treat it like a table