Implementing save data

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.
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Implementing save data

Post by Josh.G »

Is there any way to have progress saved in my game?
If so how?
User avatar
GVovkiv
Party member
Posts: 678
Joined: Fri Jan 15, 2021 7:29 am

Re: Implementing save data

Post by GVovkiv »

You can puta ll you save related data in 1 table and then serialize it and then save
For example, you can try https://github.com/bakpakin/binser
or https://github.com/gideros/table.save
or https://github.com/BroccoliRaab/SaveData
or https://github.com/zhsso/Tserial.lua
or any other serialization (or save/load libraries)
Josh.G
Prole
Posts: 16
Joined: Thu Jul 29, 2021 6:16 pm

Re: Implementing save data

Post by Josh.G »

Sorry lol can you rephrase? im kinda new to this :crazy:
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Implementing save data

Post by BrotSagtMist »

Depends on what your data looks like.
If its just some numbers and flags then:
handle= io.open("save" , "w")
handle:write(string.char(health)..string.char(level)..tostring(option and 1 or 0))
will store a health state and level up to a value of 255 and the later will either write 1 or 0 if the option is on.

then on restart you just do:
handle,err= io.open("save" , "r")
if err then health= defaults
else health=string.byte(handle:read(1)) end
obey
User avatar
GVovkiv
Party member
Posts: 678
Joined: Fri Jan 15, 2021 7:29 am

Re: Implementing save data

Post by GVovkiv »

Josh.G wrote: Tue Aug 10, 2021 8:12 pm Sorry lol can you rephrase? im kinda new to this :crazy:
lets say
you have table with some data, which should be saved:

Code: Select all

game = {
enemy1 = {*somehting*},
item3 = {*somehting*},
level = {etc}
}
And you need to save it
Lua or love don't have built-in functions to save tables, so if you need to save data, you should somehow implement it
(and, because we talk about lua, you can)
You can try above mentioned serialization (https://en.wikipedia.org/wiki/Serialization) libraries, that serialize given data to saveable format
Or you can try libraries, that can directly parse lua tables to saveable formats, for example, to JSON (https://github.com/rxi/json.lua) or xml (https://github.com/manoelcampos/xml2lua)
If you want your own system, you can check that lua tutorial from PiL:
https://www.lua.org/pil/12.1.1.html, here explained, how you can implement your own save function
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Implementing save data

Post by ReFreezed »

On a more basic level, to save data you need to save that data to a file. The only kind of values you can save to files are strings (which are actually just arrays of bytes under the hood) and numbers (which Lua automatically converts to strings when you try to write them to files). If you want to save anything else to a file (like booleans or tables) you'll need a way of converting that value to a string. This is called serialization. When you later read the saved file (e.g. next time you start the game) and want to convert the data back then you need to do the reverse, which is called deserialization.

There are two general kinds of data you can save to a file: textual (which will let you open the file in a text editor and see its contents) and binary (which will look nonsensical in a text editor, and you'll need another kind of program to make sense of). The former can potentially be more human-friendly and the latter can be more computer friendly, but both options can work in any situation.

Look at the links GVovkiv provided and at the APIs that LÖVE and Lua provide, to create a picture of what you can do.

https://love2d.org/wiki/love.filesystem
https://love2d.org/wiki/love.data.pack
https://love2d.org/wiki/love.data.unpack
https://www.lua.org/manual/5.1/manual.html
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: Implementing save data

Post by darkfrei »

I'm making the savefile just like a lua file with text:

Code: Select all

--savefile.lua
return {
	player = {x=1, y=7, health = 48},
	enemy = {x=4, y=6, health = 3},
	coins = {{x=1,y=4}, {x=4,y=4}, {x=1,y=1}, {x=4,y=1}}
}
And just call it.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Implementing save data

Post by Xii »

Serializing to Lua like that has the benefit of "free" deserialization by the Lua parser itself, which is implemented in fast C.
The disadvantage is that the save file can execute Lua code, which might not be desirable, especially if players share save files.
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: Implementing save data

Post by darkfrei »

Xii wrote: Thu Aug 12, 2021 7:18 pm Serializing to Lua like that has the benefit of "free" deserialization by the Lua parser itself, which is implemented in fast C.
The disadvantage is that the save file can execute Lua code, which might not be desirable, especially if players share save files.
Ignore all functions in save files?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
GVovkiv
Party member
Posts: 678
Joined: Fri Jan 15, 2021 7:29 am

Re: Implementing save data

Post by GVovkiv »

darkfrei wrote: Thu Aug 12, 2021 8:29 pm
Xii wrote: Thu Aug 12, 2021 7:18 pm Serializing to Lua like that has the benefit of "free" deserialization by the Lua parser itself, which is implemented in fast C.
The disadvantage is that the save file can execute Lua code, which might not be desirable, especially if players share save files.
Ignore all functions in save files?
I guess using lua as save file may me abused with something like

Code: Select all

repeat until false
Or with something really dangerous (especially with lua jit's ffi)
Unless you sandbox it which may be painful, according to this: http://lua-users.org/wiki/SandBoxes
Maybe better to just some ini/json/etc parser which more safe and free you from dealing with sandboxing/ffi/and other stuff?
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests