That looks great thanks for showing me ill experiment with it later!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
How to save and load in love(easiest method I've found while trying to learn)
Re: How to save and load in love(easiest method I've found while trying to learn)
Re: How to save and load in love(easiest method I've found while trying to learn)
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
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
- BrotSagtMist
- Party member
- Posts: 613
- 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)
Threads mildly frustrating.
obey
Re: How to save and load in love(easiest method I've found while trying to learn)
Doesn't that load the data as strings? What if you want a number or table etc?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
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...
- BrotSagtMist
- Party member
- Posts: 613
- 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)
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
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
Re: How to save and load in love(easiest method I've found while trying to learn)
That's interesting, I didn't know that. It's like the opposite of JavaScript, where it would coerce everything to a string "11".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.
Re: How to save and load in love(easiest method I've found while trying to learn)
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 workRNavega wrote: ↑Tue Jan 31, 2023 2:28 amThat's interesting, I didn't know that. It's like the opposite of JavaScript, where it would coerce everything to a string "11".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.
Re: How to save and load in love(easiest method I've found while trying to learn)
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.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
Re: How to save and load in love(easiest method I've found while trying to learn)
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.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...
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.
Who is online
Users browsing this forum: Bing [Bot] and 221 guests