Recommended way to read from and write to files?

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
garcia1000
Prole
Posts: 34
Joined: Sat Nov 28, 2015 5:54 am

Recommended way to read from and write to files?

Post by garcia1000 »

Hello everyone, here's my current game!
https://dl.dropboxusercontent.com/u/218 ... eFrog.love

I would like to add a 'replay' function to my game. So basically, at the end of each round, it would save the entire keybuffer table for that round to a file. Then, I would have a 'replay' which would load the saved file, and then play it back.

Technically, the only part I don't know how to do is to read and write the file. I think the default love2d filesystem folder is in some crazy place where I need administrator privileges, and I don't know how to make it read/write from my base directory. What is a good way to do what I want to do here?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Recommended way to read from and write to files?

Post by s-ol »

garcia1000 wrote:I think the default love2d filesystem folder is in some crazy place where I need administrator privileges, and I don't know how to make it read/write from my base directory.
What leads you to believe that?

https://www.love2d.org/wiki/love.filesystem.write
https://www.love2d.org/wiki/love.filesystem.read
https://www.love2d.org/wiki/love.filesystem.load

you may want to use an existing serialization library like Ser or serpent for example (there are tons and tons of these)

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
garcia1000
Prole
Posts: 34
Joined: Sat Nov 28, 2015 5:54 am

Re: Recommended way to read from and write to files?

Post by garcia1000 »

Thank you, I will try those and I'll come back if I can't get it to work.
garcia1000
Prole
Posts: 34
Joined: Sat Nov 28, 2015 5:54 am

Re: Recommended way to read from and write to files?

Post by garcia1000 »

I can't get this to work, what am I doing wrong?

https://dl.dropboxusercontent.com/u/218 ... eFrog.love

I have this in conf.lua:

Code: Select all

function love.conf(t)
    t.identity = "files"
end
Then, I have this in main.lua:

Code: Select all

love.filesystem.write("files/Test.txt", "Hello", all)
love.filesystem.write("Test.txt", "Hello", all)
Neither of those writes "Hello" to Test.txt in the "files" folder.

I have also tried useing love.filesystem.setIdentity to set the path, but that doesn't work either.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Recommended way to read from and write to files?

Post by arampl »

If you want to write *all* data then:

Code: Select all

love.filesystem.write("Test.txt", "Hello")
P.S. "files" folder resides in the user's home directory like "home/<user's name>/.local/share/love/files" on Linux.
garcia1000
Prole
Posts: 34
Joined: Sat Nov 28, 2015 5:54 am

Re: Recommended way to read from and write to files?

Post by garcia1000 »

Thanks, that is my problem I think. I'm using a Windows machine and I think the 'home' directory needs administrator privileges to write, or something. I can't control how it acts on different machines.

Can I set the directory to be the same path where main.lua is?
User avatar
Ortimh
Citizen
Posts: 90
Joined: Tue Sep 09, 2014 5:07 am
Location: Indonesia

Re: Recommended way to read from and write to files?

Post by Ortimh »

garcia1000 wrote:Can I set the directory to be the same path where main.lua is?
How can you add a file in the archive (.love file)? It's nearly imposibble. If it can, how can you add a file on running file archive? So the answer is no. But if you're talking about another main.lua, which path are you talking about? Source base or save directory?
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Recommended way to read from and write to files?

Post by s-ol »

garcia1000 wrote:Thanks, that is my problem I think. I'm using a Windows machine and I think the 'home' directory needs administrator privileges to write, or something. I can't control how it acts on different machines.

Can I set the directory to be the same path where main.lua is?
On windows the data is in APPDATA: https://www.love2d.org/wiki/love.filesystem (read the text at the beginning)

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
garcia1000
Prole
Posts: 34
Joined: Sat Nov 28, 2015 5:54 am

Re: Recommended way to read from and write to files?

Post by garcia1000 »

Ok, thanks, I see now!

I was trying to set a config.lua file in the same directory as main.lua. But that's impossible. Instead, it needs to create a config.lua file in the Appdata directory. Thanks.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest