Making a file and writing to it

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
RemiM
Prole
Posts: 8
Joined: Wed Oct 31, 2012 4:39 pm

Making a file and writing to it

Post by RemiM »

I've researched a lot and tried different Lua methods, and functions that are included in Love2D, but nothing seems to work for me.
I've created a level editor so I can make levels for my game, but I can't get it to write to a file.

All the data are in forms of tables, and I want to store them as tables in a .lua file, because that's how my game loads levels.
I don't have efficiency in mind right now, because I want to get it to work first. But it doesn't seem to be able to either create a file or write to it.

Here is my current code:

Code: Select all

function save()
	love.filesystem.setIdentity("Dropbox/LuaTest/Level editor/Levels/")
	love.filesystem.newFile("level.lua")
	love.filesystem.write("level.lua", "object.X = {")
	for i = 1, table.getn(object.X), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.X[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.Y = {")
	for i = 1, table.getn(object.Y), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.Y[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.W = {")
	for i = 1, table.getn(object.W), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.W[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.H = {")
	for i = 1, table.getn(object.H), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.H[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.F = {")
	for i = 1, table.getn(object.F), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.F[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.R = {")
	for i = 1, table.getn(object.R), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.R[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.G = {")
	for i = 1, table.getn(object.G), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.G[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}\n\n")
	love.filesystem.write("level.lua", "object.B = {")
	for i = 1, table.getn(object.B), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.B[i])
			love.filesystem.write("level.lua", str)
		end
	end
	love.filesystem.write("level.lua", "}")
end
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Making a file and writing to it

Post by Boolsheet »

Code: Select all

love.filesystem.setIdentity("Dropbox/LuaTest/Level editor/Levels/")
No! Like the wiki says don't set a path as the idenitity, but just one simple name. And only set it once.

love.filesystem.write offers a quick way to create a file and write data to it. This function never appends data.

You used love.filesystem.newFile, but didn't do anything with its return value. You use that object to open the file in write mode and then write to it. If you're finished, you close it. See File:open, File:write, File:close.

Code: Select all

function love.load()
	love.filesystem.setIdentity("my_game_name")
	-- You can now use love.filesystem.getSaveDirectory to see where it will save the files.
end

function save()
	local file = love.filesystem.newFile("level.lua")
	file:open("w")
	file:write("object.X = {")
	for i = 1, table.getn(object.X), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.X[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.Y = {")
	for i = 1, table.getn(object.Y), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.Y[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.W = {")
	for i = 1, table.getn(object.W), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.W[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.H = {")
	for i = 1, table.getn(object.H), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.H[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.F = {")
	for i = 1, table.getn(object.F), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.F[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.R = {")
	for i = 1, table.getn(object.R), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.R[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.G = {")
	for i = 1, table.getn(object.G), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.G[i])
			file:write(str)
		end
	end
	file:write("}\n\n")
	file:write("object.B = {")
	for i = 1, table.getn(object.B), 1 do
		if object.D[i] ~= 1 then
			str = string.format("%s, ", object.B[i])
			file:write(str)
		end
	end
	file:write("}")
	file:close()
end
Untested code.
Shallow indentations.
RemiM
Prole
Posts: 8
Joined: Wed Oct 31, 2012 4:39 pm

Re: Making a file and writing to it

Post by RemiM »

Boolsheet wrote:

Code: Select all

love.filesystem.setIdentity("Dropbox/LuaTest/Level editor/Levels/")
No! Like the wiki says don't set a path as the idenitity, but just one simple name. And only set it once.

love.filesystem.write offers a quick way to create a file and write data to it. This function never appends data.

You used love.filesystem.newFile, but didn't do anything with its return value. You use that object to open the file in write mode and then write to it. If you're finished, you close it. See File:open, File:write, File:close.
I read the wiki about the setIdentity, although I interpreted that as just using path from your user dir, not from C:

Thank you very much for the help, I'll read through what you've written up for me and try to figure it out

EDIT: I got it to save, but it saves it in a folder in AppData/Roaming/LOVE/myfolder(the one I set using setIdentity), is there a way I can change whole this path?
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Making a file and writing to it

Post by Boolsheet »

Yes and no. LÖVE is designed to save in the application data specific directories, but it does lookup a environment variable for those paths on Windows and Linux. For Windows it's APPDATA and you could change it to something else in the command prompt or in a batch file and then start LÖVE.
Shallow indentations.
RemiM
Prole
Posts: 8
Joined: Wed Oct 31, 2012 4:39 pm

Re: Making a file and writing to it

Post by RemiM »

Boolsheet wrote:Yes and no. LÖVE is designed to save in the application data specific directories, but it does lookup a environment variable for those paths on Windows and Linux. For Windows it's APPDATA and you could change it to something else in the command prompt or in a batch file and then start LÖVE.
Alright, I'll check it out. Thank you again for all your help, you probably saved me a few hours of try'n'fail ^^
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests