Page 1 of 1

How To Save Data In Love2d

Posted: Mon Jun 27, 2022 6:22 pm
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

Re: How To Save Data In Love2d

Posted: Mon Jun 27, 2022 11:17 pm
by togFox
See the other thread that is about exactly this thing.

Re: How To Save Data In Love2d

Posted: Tue Jun 28, 2022 11:15 am
by BoonyBuny
here is my thread: viewtopic.php?f=4&t=93323

and i posted my solution at the end, take a read!

Re: How To Save Data In Love2d

Posted: Tue Jun 28, 2022 1:19 pm
by A-lox
thanks

Re: How To Save Data In Love2d

Posted: Wed Jun 29, 2022 3:29 am
by A-lox
Still makes no sense

Re: How To Save Data In Love2d

Posted: Wed Jun 29, 2022 5:18 am
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


Re: How To Save Data In Love2d

Posted: Wed Jun 29, 2022 6:46 am
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.

Re: How To Save Data In Love2d

Posted: Wed Jun 29, 2022 8:12 am
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.

Re: How To Save Data In Love2d

Posted: Wed Jun 29, 2022 3:59 pm
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.

Re: How To Save Data In Love2d

Posted: Mon Jul 04, 2022 11:50 pm
by A-lox
thx