Quick saving question

ok, last thing i need to do now, i need to get saving working. i’ve got some sort of partially working system that at least updates highscore, and tries to load but doesn’t save. this time i did look through the booklet but… brain hurt. (and not applicable?)


i’m guessing that the file never saves from the nil value error, and i wouldn’t be surprised if i’ve still managed to use defsave wrong. the second error is from i think messages being sent repeatedly shortly after message passing.

Do you ever call defsave.load? I see defsave.get, but not defsave.load. It’s described in the usage section of DefSave:

bruh. i hate it when you miss little things like that. i’ll put it in and then see what happens from there

so. it saves, when you close down the game. it doesn’t save on initialisation which is fine i guess. it says the file is empty though??? key is still nil though.

the saved file which is the table i want to save, is still default though.

not sure if it’s i’ve miscoded the key, or empty means default or something.

does it have something to do with appname maybe?

What makes you think it’s to do with the appname? Slow down a bit and make sure you’ve studied and understood the documentation and examples available.

Download the DefSave repo and build it, it has an example included. The example script is here:

1 Like

so, i’ve got to this point now.

local defsave = require "defsave.defsave"
local scores = require "main.saving.scores"
local D = require "main.player.score"

function init(self)
	defsave.set_appname("test")
	defsave.load("scores")
	defsave.save("scores")
	
	
end



function on_message(self, message_id, message, sender)
	if message_id == hash("player death") then
		defsave.get("scores", "score")
		defsave.set("scores", "score", D.getscore())
		defsave.save("scores")
		if D.getscore() > scores.highscore then
			defsave.get("scores", "highscore")
			defsave.set("scores", "highscore", D.getscore)
		end
		defsave.save("scores")
		scores.highscore = defsave.get("scores", "highscore")
	end
	
end

this is what’s in the file that’s created
image
i can feel that i’m missing something, like the part where the data from the saved file gets written to the lua module should happen somewhere else.
I still also get these errors.


i’ve been trying to follow this btw. still confused about where some steps go

It’s looking better. You’re using defsave.get in places where it’s not needed. It just returns the saved value so no need to call it right before overwriting it with the new value.

DefSave does some obfuscation so that the save file can’t easily be tampered with. Looks like there is some data there.

Other than the warnings you mentioned, what happens? Have you actually tried to load what is saved?

so is defsave.get only needed once per key then?

i think so? gui script loads the data in scores.lua

local scores = require "main.saving.scores"
function init(self)
	
end


function update(self, dt)
	local score_location = gui.get_node("scoreboard")
	local data = "score is " .. scores.score .. " highscore is " .. scores.highscore

	gui.set_text(score_location, data)
end

function on_message(self, message_id, message, sender)
	
end

it probably doesnt work based on what you said about defsave.get, but i was trying to put the loaded data into scores.lua. it’s probably an irrelevent way of doing it now though. (file to lua module to script)
image
actually i have no idea how the score from score.lua gets into scores.lua
image

defsave.get returns the value of a variable you have saved. That’s it.

You are overcomplicating it. Take a step back and start over. Create a blank script or a blank project and only try to figure out DefSave. Don’t start integrating modules and message passing. When you understand DefSave, then you can implement it in your game. Right now it seems you are mostly guessing and confusing yourself.

1 Like
function init(self)
	defsave.set_appname("test")
	defsave.load("scores")   -- file loaded
	defsave.save("scores")	-- file saved(not changed)
	
	pprint("player death recieved")  
	defsave.set("scores", "score", D.getscore())  -- score is set
	defsave.save("scores")  --  score is saved
	if D.getscore() > scores.highscore then -- score is not higher than high score so not triggered
		pprint("new highscore")
		defsave.set("scores", "highscore", D.getscore)
	end
	defsave.save("scores") -- no change so not saved
	scores.highscore = defsave.get("scores", "highscore") -- when highscore is returned, it is a nil value:  DefSave: Warning when attempting to get a key 'highscore' of file 'scores' the key was nil
	pprint(scores.highscore) --- DEBUG:SCRIPT: nil: need a way to set this as zero

	--ERROR:SCRIPT: main/UI/scoreboard.gui_script:9: attempt to concatenate field 'highscore' (a nil value)
	--stack traceback:
	--main/UI/scoreboard.gui_script:9: in function <main/UI/scoreboard.gui_script:7>  results in error because of nil
end

-- need a way to set highscore to zero first time.

found out the problem. this might work?
image

i’m back at this error though.


it should work once message passing is fixed

oh… i think i’ve found my problem
image
yep
image

OKAY nearly there
image

done.

thanks @Alex_8BitSkull for both helping, putting up with me and also letting me figure stuff out.

3 Likes

Well done.