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.
Hope someone can help me
( I Will Include The Zip file And The Love File )
How To Save Data In Love2d
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How To Save Data In Love2d
- Attachments
-
- Axolotl.love
- (155.87 KiB) Downloaded 229 times
-
- Axolotl (1).zip
- (155.87 KiB) Downloaded 211 times
Re: How To Save Data In Love2d
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:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Re: How To Save Data In Love2d
A coding begginer that is migrating from 8 years of Scratch... yea yikes.
Re: How To Save Data In Love2d
Still makes no sense
Re: How To Save Data In Love2d
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:
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
- kicknbritt
- Citizen
- Posts: 98
- Joined: Sat May 30, 2015 2:15 am
- Location: Chicago, IL/Anchorage,AK
Re: How To Save Data In Love2d
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*
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: How To Save Data In Love2d
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.
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
Re: How To Save Data In Love2d
You're probably right, but I'll try anyway.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.
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 9 guests