filesystem.newFile????

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.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

filesystem.newFile????

Post by pielago »

I am learning about love.filesystem.newFile() and i see something weird and i don't understand?

when i write this it works and it makes the folder and the .lua

Code: Select all

if not love.filesystem.exists("file.lua")then
	file = love.filesystem.newFile("file.lua")
	file:open("a")
	file:write("\nworks")
	file:close()
end
also if i do this it works perfect it even makes the folders

Code: Select all

love.filesystem.write("file","anything here")
but if i only write this it doesn't work at all????

Code: Select all

if not love.filesystem.exists("file.lua")then
	file = love.filesystem.newFile("file.lua")
end
the 3rd option doesn't work at all and wish to use that one (bad spelling its okay here just wanted to give the idea of my problem)
and still don't get it and i place them under love.load()
can someone explain me why it doesn't work
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: filesystem.newFile????

Post by HugoBDesigner »

I'm not sure about other solutions, but I'd use this one:

Code: Select all

love.filesystem.write("file","anything here")
You just forgot the file extension:

Code: Select all

love.filesystem.write("file.lua","anything here")
And it should pretty safely sure save it into your %appdata% folder. I hope I helped :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

see my problem is i want to see if the file exist
if it doesn't exist i want to make it
that code wont work the other 2 do...
but why???
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: filesystem.newFile????

Post by HugoBDesigner »

pielago wrote:see my problem is i want to see if the file exist
if it doesn't exist i want to make it
It should work then:

Code: Select all

if not love.filesystem.exists("file.lua") then
    love.filesystem.write("file.lua","anything here")
end
If it doesn't works, I think I'd need your .love file to check if anything else is messing up this part of your code ;)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

okay here is a .love all i want its to save the direction of the block save it as soon as I close and load it as soon I start the .love
also tried save the X and Y and nothing ...
Attachments
dpad.love
(2.09 KiB) Downloaded 209 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: filesystem.newFile????

Post by bartbes »

It's because newFile creates a file object, not a file. If you want the file to be created you need to open the file for writing too.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: filesystem.newFile????

Post by Jasoco »

newFile is meant for files that you need to write a lot to or need to write to over time. You open a file, throughout the game write data and at the end close it. Or you open it and write a lot of information (More info than love.filesystem.write can handle) and close it when finished.

Ad bartbes said, it's an object. It has no form until you open it. And it has no contents until you fill it.

Use write for small files. newFile for larger ones.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: filesystem.newFile????

Post by HugoBDesigner »

Found your problem: while reading the file, you were making this:

Code: Select all

a.movestate = tonumber(a.movestate)
But "a.movestate" is not a number variable, but a direction variable ("left", "right", "up" and "down"). If you remove the line above, it works. When you open the game, it shows the last direction you moved for.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: filesystem.newFile????

Post by pielago »

Omg thank you so much i tried to fix it and didn't know where the problem was
i am able now to save even X,Y pos
question will this work if i want to save tables? or many tables? or do i need another way ????

and thank you for your reply great forum.. :)
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: filesystem.newFile????

Post by HugoBDesigner »

pielago wrote:Omg thank you so much i tried to fix it and didn't know where the problem was
i am able now to save even X,Y pos
question will this work if i want to save tables? or many tables? or do i need another way ????

and thank you for your reply great forum.. :)
You're welcome!
Saving tables IS possible, but not easy. You'd have to do something like this:

Code: Select all

local txt = ""
local splitchar = "/"
for i, v in pairs(table) do
    txt = txt .. tostring(v) .. splitchar
end
txt = string.sub(txt, 1, -2)
But if you've got something (a string for example) in that table that contains the "splitchar" part, replace it by something else.
Also, things may go weird if you have tables inside tables. To load, you must use the "string:split()" function. I'll already let clear that it was not made by me:

Code: Select all

function string:split(delimiter) --Not by me
	local result = {}
	local from	= 1
	local delim_from, delim_to = string.find( self, delimiter, from	 )
	while delim_from do
		table.insert( result, string.sub( self, from , delim_from-1 ) )
		from = delim_to + 1
		delim_from, delim_to = string.find( self, delimiter, from  )
	end
	table.insert( result, string.sub( self, from  ) )
	return result
end
And while loading:

Code: Select all

table = {}
local t = textlaoded:split(splitchar)
for i, v in pairs(t) do
    if tonumber(v) then
        table.insert(table, tonumber(v))
    elseif v == "true" then
        table.insert(table, true)
    elseif v == "false" then
        table.insert(table, false)
    else
        table.insert(table, v)
    end
end
But that depends a lot on how your table is and what it contains. I'd not use it if you have different things on your table, or if your table have custom items (like table["string"] or table.string - which is the same thing, but still).
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 71 guests