Page 1 of 1

Recommended way to read from and write to files?

Posted: Mon Dec 21, 2015 2:51 pm
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?

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

Posted: Mon Dec 21, 2015 3:47 pm
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)

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

Posted: Wed Dec 23, 2015 9:59 am
by garcia1000
Thank you, I will try those and I'll come back if I can't get it to work.

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

Posted: Thu Dec 24, 2015 3:08 am
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.

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

Posted: Thu Dec 24, 2015 4:29 am
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.

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

Posted: Thu Dec 24, 2015 6:21 am
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?

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

Posted: Thu Dec 24, 2015 8:30 am
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?

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

Posted: Thu Dec 24, 2015 11:41 am
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)

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

Posted: Sun Dec 27, 2015 12:56 pm
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.