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

Post 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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: File Access gone wrong

Post 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')()
bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

Re: File Access gone wrong

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: File Access gone wrong

Post 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].
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: File Access gone wrong

Post by raidho36 »

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

Re: File Access gone wrong

Post 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.
Attachments
TestLua.rar
(83.06 KiB) Downloaded 186 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: File Access gone wrong

Post 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.
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: File Access gone wrong

Post 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.
bbdude95
Prole
Posts: 10
Joined: Thu Jun 20, 2013 1:18 am

Re: File Access gone wrong

Post by bbdude95 »

Sorry about that. Here is my .lua main file and a .zip.
Attachments
TestLua.zip
(100.47 KiB) Downloaded 184 times
main.lua
(21.34 KiB) Downloaded 185 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: File Access gone wrong

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest