Page 1 of 2

File Access gone wrong

Posted: Thu Jun 20, 2013 1:24 am
by bbdude95
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

Re: File Access gone wrong

Posted: Thu Jun 20, 2013 7:16 am
by micha
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')()

Re: File Access gone wrong

Posted: Sat Jun 22, 2013 1:39 am
by bbdude95
I have this in my code:

Code: Select all

	
chunk = love.filesystem.load("map.lua")()
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

Re: File Access gone wrong

Posted: Sat Jun 22, 2013 7:05 am
by Robin
bbdude95 wrote:but if i wanted to pull out a specific row/column in a file how would i do that?
In your case:

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].

Re: File Access gone wrong

Posted: Sat Jun 22, 2013 8:12 am
by raidho36

Re: File Access gone wrong

Posted: Thu Jun 27, 2013 11:56 pm
by bbdude95
Robin wrote:
bbdude95 wrote:but if i wanted to pull out a specific row/column in a file how would i do that?
In your case:

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.

Re: File Access gone wrong

Posted: Fri Jun 28, 2013 12:45 pm
by Robin
That is a .RAR. I can't open that. More people will be able to help you if you make it a .ZIP file.

Re: File Access gone wrong

Posted: Fri Jun 28, 2013 3:11 pm
by raidho36
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.

Re: File Access gone wrong

Posted: Fri Jun 28, 2013 5:39 pm
by bbdude95
Sorry about that. Here is my .lua main file and a .zip.

Re: File Access gone wrong

Posted: Sat Jun 29, 2013 5:59 pm
by Robin
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.