How To Save Data In Love2d

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

How To Save Data In Love2d

Post by A-lox »

There are not any tutorials HOW TO SAVE DATA in love2d :|
I whant to save most global variables.
There is no tutorials online and all the other love2ders posts did not solve my problems. :cry:
Hope someone can help me
( I Will Include The Zip file And The Love File ) :3 :3 :3
Attachments
Axolotl.love
(155.87 KiB) Downloaded 213 times
Axolotl (1).zip
(155.87 KiB) Downloaded 182 times
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How To Save Data In Love2d

Post by togFox »

See the other thread that is about exactly this thing.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Turn-based PBEM horse stable (racing) management sim: https://togfox.itch.io/horse-stable-manager
https://discord.gg/HeHgwE5nsZ
User avatar
BoonyBuny
Prole
Posts: 31
Joined: Mon Jun 13, 2022 1:24 am
Location: France
Contact:

Re: How To Save Data In Love2d

Post by BoonyBuny »

here is my thread: viewtopic.php?f=4&t=93323

and i posted my solution at the end, take a read!
A coding begginer that is migrating from 8 years of Scratch... yea yikes. :emo:
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How To Save Data In Love2d

Post by A-lox »

thanks
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How To Save Data In Love2d

Post by A-lox »

Still makes no sense
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: How To Save Data In Love2d

Post by Andlac028 »

love.data.write accepts data or string, not number, so if you want to save number, you have to convert it to string and then back to number when you want to read it.

Example:

Code: Select all

function love.load()

    -- your code for loading

    -- Load saved number of clicks (the extra braces are because love.filesystem.read returns 2 values, but we only need 1st)
    Clicks = tonumber((love.filesystem.read("save.txt"))) or 0

    -- your code for loading
end

function love.quit()
    -- Save clicks when game is quited
    love.filesystem.write("save.txt", tostring(Clicks))
end

User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: How To Save Data In Love2d

Post by kicknbritt »

Saving globals is not really a thing. You want to put all of them in a table you have easy access to and then serialize that table.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: How To Save Data In Love2d

Post by BrotSagtMist »

Reminder that lua automatically calls tostring on numbers if the function expects strings.
Reminder that globals ARE in a table. Its "_G".

The other thread is pretty detailed in the basics. If he still doesnt get it, its a lost case.
obey
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: How To Save Data In Love2d

Post by milon »

BrotSagtMist wrote: Wed Jun 29, 2022 8:12 am The other thread is pretty detailed in the basics. If he still doesnt get it, its a lost case.
You're probably right, but I'll try anyway.
A-lox wrote: Wed Jun 29, 2022 3:29 am Still makes no sense
I don't believe this has been pointed out yet, but there's a big difference between memory objects, like a table, and files. You can't directly put a table into a file. A file is either binary data (a stream of hex characters) or a string of regular ASCII characters (things you can type with your keyboard).

This is why everyone is talking about serialization - it's a technique to turn a table into file-compatible string of characters (binary or string) for writing to disk (AKA saving stuff to file). Any good serialization library can also de-serialize a file stream back into one or more tables which you then assign to your internal tables (AKA loading from a file). This method is pretty much guaranteed to work once setup and requires only minimal adjusting as your project grows.

There is another method of saving & loading that is perhaps easier to understand, but harder to implement. You simply write the strings/values in a set order to a file, one value per line. When you're ready to load data, you just read each line and manually assign each string/value back to each table index. Make sure you've (re)constructed your tables first so they're ready to receive data! This method must be constantly tweaked to keep saving & loading in sync with each AND with any/all involved tables. It's much more fiddly, and not the method I'd recommend, but it's easier to understand and it might be useful to get some experience reading/writing files.

Does that help? If not, please tell us what you do understand so far and what part exactly doesn't make sense.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
A-lox
Prole
Posts: 43
Joined: Sun Apr 10, 2022 1:58 am

Re: How To Save Data In Love2d

Post by A-lox »

thx
Post Reply

Who is online

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