Page 1 of 4

Implementing save data

Posted: Tue Aug 10, 2021 7:51 pm
by Josh.G
Is there any way to have progress saved in my game?
If so how?

Re: Implementing save data

Posted: Tue Aug 10, 2021 7:59 pm
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)

Re: Implementing save data

Posted: Tue Aug 10, 2021 8:12 pm
by Josh.G
Sorry lol can you rephrase? im kinda new to this :crazy:

Re: Implementing save data

Posted: Tue Aug 10, 2021 8:24 pm
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

Re: Implementing save data

Posted: Tue Aug 10, 2021 8:42 pm
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

Re: Implementing save data

Posted: Thu Aug 12, 2021 1:22 am
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

Re: Implementing save data

Posted: Thu Aug 12, 2021 10:47 am
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.

Re: Implementing save data

Posted: Thu Aug 12, 2021 7:18 pm
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.

Re: Implementing save data

Posted: Thu Aug 12, 2021 8:29 pm
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?

Re: Implementing save data

Posted: Thu Aug 12, 2021 8:46 pm
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?