Question about Reading Data from a File

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
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Question about Reading Data from a File

Post by NoreoAlles »

Hey, i know the thread beneath me is about a similar topic and i already asked something like this, but i just want to know, how i can read a save file with something like this in it:

Code: Select all

12, 30
and read it with the love.filesystem stuff so i could use it like this

Code: Select all

player.x, player,y = love.filesystem.read("SaveData")
Thanks
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Question about Reading Data from a File

Post by GVovkiv »

NoreoAlles wrote: Thu May 19, 2022 7:02 pm Hey, i know the thread beneath me is about a similar topic and i already asked something like this, but i just want to know, how i can read a save file with something like this in it:

Code: Select all

12, 30
and read it with the love.filesystem stuff so i could use it like this

Code: Select all

player.x, player,y = love.filesystem.read("SaveData")
Thanks
You can read data into 1 variable and parse it via lua's string library using pattern matching.
You can place every peace of data on new line and read it with https://love2d.org/wiki/love.filesystem.lines.
You might use some file format and users made library for that.
(In your case, it seems that CSV https://en.wikipedia.org/wiki/Comma-separated_values will suit you well;
quick google search show me https://github.com/geoffleyland/lua-csv as first lua CSV lib on github, so you might try it.)
User avatar
BrotSagtMist
Party member
Posts: 612
Joined: Fri Aug 06, 2021 10:30 pm

Re: Question about Reading Data from a File

Post by BrotSagtMist »

Code: Select all

player.x, player.y = love.filesystem.read("SaveData"):match("(%d+).(%d+)")
The return is a string thus we can split it using matches.
It is also possible to read each number in the file but stop and go file reads can have a bad performance.
obey
User avatar
darkfrei
Party member
Posts: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Question about Reading Data from a File

Post by darkfrei »

The tab separated values is better than comma separated values.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
dusoft
Party member
Posts: 497
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Question about Reading Data from a File

Post by dusoft »

darkfrei wrote: Thu May 19, 2022 8:22 pm The tab separated values is better than comma separated values.
And why is that? It really does not matter as long as the input and the parser respect the same formatting rules. You can use any character for separating values such as pipe, semicolon, tabs etc. Completely up to you.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Question about Reading Data from a File

Post by NoreoAlles »

GVovkiv wrote: Thu May 19, 2022 7:33 pm
NoreoAlles wrote: Thu May 19, 2022 7:02 pm Hey, i know the thread beneath me is about a similar topic and i already asked something like this, but i just want to know, how i can read a save file with something like this in it:

Code: Select all

12, 30
and read it with the love.filesystem stuff so i could use it like this

Code: Select all

player.x, player,y = love.filesystem.read("SaveData")
Thanks
You can read data into 1 variable and parse it via lua's string library using pattern matching.
You can place every peace of data on new line and read it with https://love2d.org/wiki/love.filesystem.lines.
You might use some file format and users made library for that.
(In your case, it seems that CSV https://en.wikipedia.org/wiki/Comma-separated_values will suit you well;
quick google search show me https://github.com/geoffleyland/lua-csv as first lua CSV lib on github, so you might try it.)
Thanks, will try! :nyu:
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests