How to save and load in love(easiest method I've found while trying to learn)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by fridays18 »

togFox wrote: Mon Jan 30, 2023 7:31 am Assuming you have your data in tables (who doesn't) then use bitser and nativefs:

Native method also works but this is more secure (and the nativefs is optional).

Code: Select all

local function savePersons()
    local savefile = savedir .. "persons.dat"

    local serialisedString = bitser.dumps(PERSONS)
    local success, message = nativefs.write(savefile, serialisedString)

    return success
end
That looks great thanks for showing me ill experiment with it later!
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by togFox »

I'm using two libraries for something that can be done native but really - three lines (or even two) - I'm happy.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by fridays18 »

togFox wrote: Mon Jan 30, 2023 1:03 pm I'm using two libraries for something that can be done native but really - three lines (or even two) - I'm happy.
Lol whatever works
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by BrotSagtMist »

Threads mildly frustrating.
obey
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by RNavega »

BrotSagtMist wrote: Sun Jan 29, 2023 2:09 pm easy? But this is a complicated hard way.

Code: Select all

Write:
love.filesystem.write("save",table.concat(t,"\n"))
Read:
for line in love.filesystem.lines("save") do
 t[#t+1]=line
end
Doesn't that load the data as strings? What if you want a number or table etc?

I think writing a Lua script file, to be loaded later with love.filesystem.load(), is another way.

And for anyone thinking of complaining "but that will execute arbitrary code from that Lua file, that's dangerous", think about the absurd of what you're saying -- some bad actor managed to enter the user's system and have full control, and all they think of doing is replacing the contents of a save game file that will be used within that game? By that point the system is already infected...
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by BrotSagtMist »

Am i misunderstanding how people work? Do you really try to save multiple levels of tables with several datatypes immediately after finishing your first hello world?
I just assumed stuff to be saved are like scores and health, maybe a name or two. Not an entire level map.

Anyway, yes this will be a string, simply because it cant distinguish between a string and a number when read that way.
But usually that doesnt matter as lua does not usually care if a number is a string, "1"+ 1 is perfectly valid.
It can of course be a cause of bugs, so playing save, this is the fix: tonumber(line) or line
obey
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by RNavega »

BrotSagtMist wrote: Tue Jan 31, 2023 1:58 am But usually that doesnt matter as lua does not usually care if a number is a string, "1"+ 1 is perfectly valid.
That's interesting, I didn't know that. It's like the opposite of JavaScript, where it would coerce everything to a string "11".
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by Andlac028 »

RNavega wrote: Tue Jan 31, 2023 2:28 am
BrotSagtMist wrote: Tue Jan 31, 2023 1:58 am But usually that doesnt matter as lua does not usually care if a number is a string, "1"+ 1 is perfectly valid.
That's interesting, I didn't know that. It's like the opposite of JavaScript, where it would coerce everything to a string "11".
In JS, + is for addition and also for concatenation, but in Lua, + is only for addition and .. is for concatenation, so you can just sum strings and it will work
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by fridays18 »

BrotSagtMist wrote: Tue Jan 31, 2023 1:58 am Am i misunderstanding how people work? Do you really try to save multiple levels of tables with several datatypes immediately after finishing your first hello world?
I just assumed stuff to be saved are like scores and health, maybe a name or two. Not an entire level map.

Anyway, yes this will be a string, simply because it cant distinguish between a string and a number when read that way.
But usually that doesnt matter as lua does not usually care if a number is a string, "1"+ 1 is perfectly valid.
It can of course be a cause of bugs, so playing save, this is the fix: tonumber(line) or line
The tutorial was for people who have done more than there first hello world lol, it was just a general guide for saving tables and variables using the library which once again even after this thread im still convinced is the easiest method to work with and understand.
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to save and load in love(easiest method I've found while trying to learn)

Post by pgimeno »

RNavega wrote: Tue Jan 31, 2023 1:24 am And for anyone thinking of complaining "but that will execute arbitrary code from that Lua file, that's dangerous", think about the absurd of what you're saying -- some bad actor managed to enter the user's system and have full control, and all they think of doing is replacing the contents of a save game file that will be used within that game? By that point the system is already infected...
Imagine that a game becomes popular enough that there's a webpage in some site that is dedicated to sharing stored savegames, with insufficient moderation.

Or that someone in a chat tricks a guy who doesn't even know that a savegame is executable code, to use a manipulated savegame, by asserting that it will give them advantage, or it will unlock hidden stuff, or whatever other means of social engineering.

There's also the issue of size. Lua files have a size limit; arbitrary files don't.

There are several serialization options that don't generate Lua: bitser, binser, smallfolk and even dkjson.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 20 guests