Page 2 of 3

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

Posted: Mon Jan 30, 2023 12:02 pm
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!

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

Posted: Mon Jan 30, 2023 1:03 pm
by togFox
I'm using two libraries for something that can be done native but really - three lines (or even two) - I'm happy.

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

Posted: Mon Jan 30, 2023 4:01 pm
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

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

Posted: Mon Jan 30, 2023 5:43 pm
by BrotSagtMist
Threads mildly frustrating.

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

Posted: Tue Jan 31, 2023 1:24 am
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...

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

Posted: Tue Jan 31, 2023 1:58 am
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

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

Posted: Tue Jan 31, 2023 2:28 am
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".

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

Posted: Tue Jan 31, 2023 4:51 am
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

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

Posted: Tue Jan 31, 2023 4:09 pm
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.

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

Posted: Tue Jan 31, 2023 6:40 pm
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.