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

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 » Thu Jun 20, 2013 7:16 am

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 » Sat Jun 22, 2013 1:39 am

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 » Sat Jun 22, 2013 7:05 am

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: 1702
Joined: Mon Jun 17, 2013 12:00 pm

Re: File Access gone wrong

Post by raidho36 » Sat Jun 22, 2013 8:12 am


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

Re: File Access gone wrong

Post by bbdude95 » Thu Jun 27, 2013 11:56 pm

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 72 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 » Fri Jun 28, 2013 12:45 pm

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: 1702
Joined: Mon Jun 17, 2013 12:00 pm

Re: File Access gone wrong

Post by raidho36 » Fri Jun 28, 2013 3:11 pm

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 » Fri Jun 28, 2013 5:39 pm

Sorry about that. Here is my .lua main file and a .zip.
Attachments
TestLua.zip
(100.47 KiB) Downloaded 71 times
main.lua
(21.34 KiB) Downloaded 72 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 » Sat Jun 29, 2013 5:59 pm

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: No registered users and 6 guests