Updating saved data without manually deleting it

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
Zorochase
Prole
Posts: 21
Joined: Sun May 21, 2017 10:40 pm
Contact:

Updating saved data without manually deleting it

Post by Zorochase »

Hello,
I have these two methods: (LF stands for love.filesystem)

Code: Select all

function loadData(file, data)
	if not LF.exists(file) then return end
	local RDATA = LF.read(file)
	local LDATA = TSerial.unpack(RDATA)
	for i,v in pairs(LDATA) do
		data[i] = v
    end
end
and:

Code: Select all

function saveData(file, data)
    LF.write(file, TSerial.pack(data))
end
One saves data and the other loads data using TSerial.
I also have a table that defines the game's settings:

Code: Select all

config = {
    sx = 1,
    sy = 1,
    alwaysShowCursor = false,
    enableBorders = true,
    keys = {
        UP = 'W',
        DOWN = 'S',
        LEFT = 'A',
        RIGHT = 'D',
        QUIT = "escape"
}
This table is saved to a file in the default save directory. The file is edited when a setting is changed in-game, but the table remains the same in its lua file. Saving works fine EXCEPT for when I try to add a new setting. For example, when I try to add a SHOOT key to the keys table (inside config), it isn't written to the saved file when I open the game, nor when I close it. This means I have to manually delete the saved file, open the game again, and close it. Only then would the SHOOT key be added, but of course, all the other settings are set back to default. My main question here is what can I do to alter either the save or load methods so that I can add a new setting to the config table and have it, in a way, be appended to the already saved data so that I don't ever even have to touch the saved file myself.

I tried editing the save method so that it would delete the file and rewrite it, but that didn't work as intended, for it only deleted the file once and then never rewrote it, which caused an error when the game was opened it attempted to load the data again:

Code: Select all

function SaveManager:saveData(file, data)
    if LF.exists(file) then
        LF.remove(file)
    end
	LF.write(file, TSerial.pack(data))
end
Not sure where to go from here...
"I am a tomato. My favorite food is tomatoes. Tomatoes are the best. I eat them everyday. I love to hear them scream."
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Updating saved data without manually deleting it

Post by pgimeno »

Hard to say without being able to test. It might be a shallow vs deep copy problem, or it might not. I suggest you display the output of the serializer when saving, to see if the new setting is there. If it's not, try to find out why.
Zorochase wrote: Sat Mar 10, 2018 8:38 pm

Code: Select all

function saveData(file, data)
    data = TSerial.pack(data)
    print(data)
    LF.write(file, data)
end
Zorochase
Prole
Posts: 21
Joined: Sun May 21, 2017 10:40 pm
Contact:

Re: Updating saved data without manually deleting it

Post by Zorochase »

pgimeno wrote: Sat Mar 10, 2018 9:35 pm Hard to say without being able to test. It might be a shallow vs deep copy problem, or it might not. I suggest you display the output of the serializer when saving, to see if the new setting is there. If it's not, try to find out why.
Zorochase wrote: Sat Mar 10, 2018 8:38 pm

Code: Select all

function saveData(file, data)
    data = TSerial.pack(data)
    print(data)
    LF.write(file, data)
end
Seems as though it was, because with the changes you made, it's working now. Going to need to look more into understanding how that works. Thank you!
"I am a tomato. My favorite food is tomatoes. Tomatoes are the best. I eat them everyday. I love to hear them scream."
Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests