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

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

Post 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
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 »

Also I go over how to save entire tables, the last link explains saving and loading specific variables**
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 »

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
obey
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post by zorg »

Yeah, i wouldn't call using an external library the easiest way either, nor the simplest.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
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 »

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)
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: 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
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 »

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.
obey
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
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 »

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
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 »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests