## File Access gone wrong

General discussion about LÖVE, Lua, game development, puns, and unicorns.
bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

### File Access gone wrong

I am trying to input a tilemap for my game but i dont really know how to continue on the loading part. Any help or ideas would be much appreciated.

Code: Select all

function saveTileMap()
local map = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
local strg = "map = {\n"
for x = 1, #map do
strg = strg .. "\t{ "
for y = 1, #map[1] do
strg = strg .. tostring(map[x][y])
if y < #map[1] then
strg = strg .. ", "
end
end
strg = strg .. " },\n"
end
strg = strg .. " \n}"
file = love.filesystem.newFile( "map.lua" )
file:open('w')
--file:write(Tserial.pack(map,map, true))
file:write( strg )
file:close()
end

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### Re: File Access gone wrong

The saved file is source code in Lua (if everything went right). To load, you have to load the file's content into a chunk and run it:

Code: Select all

love.filesystem.load('filename')()

bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

### Re: File Access gone wrong

I have this in my code:

Code: Select all


local result = chunk()

but if i wanted to pull out a specific row/column in a file how would i do that?
EX: getting the 2nd row and 3rd column value
0,0,0,0,0
0,0,5,0,0
0,0,0,0,0

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: File Access gone wrong

bbdude95 wrote:but if i wanted to pull out a specific row/column in a file how would i do that?

Code: Select all

map[2][3]
Now, the way you've set it up, loading a map file overwrites a global called map, which might be problematic. If you change the line local strg = "map = {\n" to local strg = "return {\n", then after loading result will point to the map, and you can access a specific cell with result[2][3].

raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

### Re: File Access gone wrong

bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

### Re: File Access gone wrong

Robin wrote:
bbdude95 wrote:but if i wanted to pull out a specific row/column in a file how would i do that?

Code: Select all

map[2][3]
Now, the way you've set it up, loading a map file overwrites a global called map, which might be problematic. If you change the line local strg = "map = {\n" to local strg = "return {\n", then after loading result will point to the map, and you can access a specific cell with result[2][3].
So i did as you said and now i am just faced with this error(i think once i fix this everything will work): main.lua:34: attempt to index local 'result' (a nil value)
I know what it means but i don't know where the problem is stemming from.
Attachments
TestLua.rar

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: File Access gone wrong

That is a .RAR. I can't open that. More people will be able to help you if you make it a .ZIP file.

raidho36
Party member
Posts: 1685
Joined: Mon Jun 17, 2013 12:00 pm

### Re: File Access gone wrong

You can easily tell if someone is russian if they send you archives in RAR format. Another obvious clue is them sending you arrays of parenthesis instead of emoticons.

bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

### Re: File Access gone wrong

Sorry about that. Here is my .lua main file and a .zip.
Attachments
TestLua.zip
main.lua

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: File Access gone wrong

For some reason, you haven't included your map.lua file. What I think what happened is that your map.lua doesn't return anything, but because it's not there, I can't check.