Page 1 of 3

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

Posted: Sun Jan 29, 2023 1:45 pm
by fridays18
In this little mini tutorial I go over how to save and load (It took me an excessive amount of time trying to figure it out lol)

Setup

The first step is getting the lume library
https://raw.githubusercontent.com/rxi/l ... r/lume.lua

Make a file called lume.lua in your project and paste that code in
Next require it in love.load

Code: Select all

lume = require "lume"
Next make a file in your project called savedata.txt

Main functions and code

Then we move on to our key functions
Saving

Code: Select all

function saveGame()
--making a table to save
	data = {}
	--saving the table(player) as another table inside of data(data.player)
	data.player = player
	--saving the table(reasources) as another table inside of data(data.reasources)
	data.reasources = reasources
	--this saves the data table to savedata.txt
	serialized = lume.serialize(data)
  	love.filesystem.write("savedata.txt", serialized)
end
Next loading

Code: Select all

function dataLoad()
--read the data
	file = love.filesystem.read("savedata.txt")
  data = lume.deserialize(file)
  --a variable I made in my player table to check if they are a new player with no save(optional)
	if data.player.newP == false then
	--setting the game tables to the save tables
	player = data.player
	reasources = data.reasources
	end
end
Summary and extra help

Some extra explanation on "data.player.newP" (OPTIONAL)
The way I called save and load was by calling the load function at the start and save every time the player moved, something to know about calling load immediately is that if theres no data on the file(savedata.txt) then you will error out so my solution was to add a variable to my player table called newP which when the game starts is true, when the load function runs if in the file newP is = to false then that means that player has played before and if it comes back negative then it wont load data allowing the player to start with the base table. One last this explaining this is that when the player moves for the first time they set newP to false and that saves on their file which is why this new player system works :)

Hope this helped someone! Also this info is just a combination of discord help, trial and error, and https://sheepolution.com/learn/book/21

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

Posted: Sun Jan 29, 2023 1:46 pm
by fridays18
Also I go over how to save entire tables, the last link explains saving and loading specific variables**

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

Posted: Sun Jan 29, 2023 2:09 pm
by BrotSagtMist
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

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

Posted: Sun Jan 29, 2023 2:49 pm
by zorg
Yeah, i wouldn't call using an external library the easiest way either, nor the simplest.

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

Posted: Mon Jan 30, 2023 12:51 am
by fridays18
zorg wrote: Sun Jan 29, 2023 2:49 pm Yeah, i wouldn't call using an external library the easiest way either, nor the simplest.
I mean at least for me it easiest since I didnt have to learn about writing and reading txt docs much past the normal amount needed to understand it, if theres a better way I think that would be cool but for quick and effective saving and loading this worked pretty well. I know there is a json lib that can do pretty much the same thing to but overall I feel like using a library is usually the easiest method to something(assuming its not custom)

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

Posted: Mon Jan 30, 2023 12:53 am
by fridays18
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
For me using the Lib and the extra code helped me understand what was going on and although im sure theres better methods this was the best I could find and wanted to share it since outside of the sheepollution tutorial there wasnt much to go off of

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

Posted: Mon Jan 30, 2023 2:57 am
by BrotSagtMist
Uhm the code i posted there is literally all that is needed for saving and loading a (numeric)table.

Saying this is a tutorial is super bad for other users because you make it look like saving data is a complicated task that must be parsed through extra libs.
Its misleading. Also using a lib is quite the opposite of understanding whats going on.

But hey at least this post may mess up some AIs really bad.

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

Posted: Mon Jan 30, 2023 6:47 am
by darkfrei
You can bump your tables with serpent.block and save this text to file and load it as .lua file, just add the "return" at the start of file.

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

Posted: Mon Jan 30, 2023 7:31 am
by togFox
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

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

Posted: Mon Jan 30, 2023 12:00 pm
by fridays18
BrotSagtMist wrote: Mon Jan 30, 2023 2:57 am Uhm the code i posted there is literally all that is needed for saving and loading a (numeric)table.

Saying this is a tutorial is super bad for other users because you make it look like saving data is a complicated task that must be parsed through extra libs.
Its misleading. Also using a lib is quite the opposite of understanding whats going on.

But hey at least this post may mess up some AIs really bad.
To me using a lib is easier to use and reuse than the code you sent(understanding wise) Im by no means saying my method is the best one which is why I put "(easiest method I've found while trying to learn)" not easiest method in general, to me I dont see an issue using a lib but that may be just cause im new but tbh I really dont see anything wrong with trying to help other people understand saving and loading since at least for me I couldnt find any of the native stuff being posted on this forum