Save a highscore variable between builds?

No it didn’t print anything at all actually

If you don’t get a “before” printed, then you don’t actually reach that code.
You can also try setting debug break points to debug your code and variables.

can you guys help me perhaps if i send my code ? at the current state it doesnt work and sends 2 error messages:

ERROR:GAMESYS: Error when dispatching message to gui scene: -2. Message ‘game_over’ sent from main:/zombie7#tank to main:/gui#ui

ERROR:SCRIPT: /main/ui.gui_script:23: attempt to index local ‘self’ (a nil value)

here is my module

local M = {}



M.APP_NAME = "zombiegame"
M.SAVE_FILE_NAME = "gamedata"

function M.load_file()
	local highscorefile = sys.load(sys.get_save_file(M.APP_NAME, M.SAVE_FILE_NAME))

	return highscorefile.hiscore
	
end

function M.save_file(hiscore)
	local highscorefile = {hiscore = hiscore}
	sys.save(sys.get_save_file(M.APP_NAME, M.SAVE_FILE_NAME), highscorefile)
end

return M

here is my gui.ui_script

local filesave = require "main.filesave"
function init(self)
	self.score = 0                                          
	self.node_to_hide = gui.get_node("gameover")
	self.node_to_hide2 = gui.get_node("score")
	self.node_to_hide3 = gui.get_node("score2")
	self.node_to_hide4 = gui.get_node("restartgame")
	self.node_to_hide5 = gui.get_node("highscore")
	self.node_to_hide6 = gui.get_node("highscore2")
	gui.set_enabled(self.node_to_hide, false)
	gui.set_enabled(self.node_to_hide2, true)
	gui.set_enabled(self.node_to_hide3, false)
	gui.set_enabled(self.node_to_hide4, false)
	gui.set_enabled(self.node_to_hide5, false)
	gui.set_enabled(self.node_to_hide6, true)
	self.hiscore = filesave.load_file()
	self.highscore = 0
	
	
end

function savefunction (self)
	filesave.save_file(self.highscore)
	if not sys.save(sys.get_save_file("zombiegame", "gamedata"), filesave)then
		pprint("Gamedata not saved")
	end
	local r = sys.get_save_file(M.APP_NAME, M.SAVE_FILE_NAME)
	print(r)
end
function on_message(self, message_id, message, sender)
	if message_id == hash("add_score") then                 -- [2]
		self.score = self.score + message.score             -- [3]
		local scorenode = gui.get_node("score")             -- [4]
		gui.set_text(scorenode, "SCORE: " .. self.score)    -- [5]
		if self.score>self.highscore then
			self.highscore = self.score
		end
		local highscore2node = gui.get_node("highscore2")
		gui.set_text(highscore2node, "HIGHSCORE: "..self.highscore)
		
	end
	if message_id == hash("game_over") then
		
		
		gui.set_enabled(self.node_to_hide, true)
		gui.set_enabled(self.node_to_hide2, false)
		gui.set_enabled(self.node_to_hide3, true)
		gui.set_enabled(self.node_to_hide4, true)
		gui.set_enabled(self.node_to_hide5, true)
		gui.set_enabled(self.node_to_hide6, false)
		local score2node = gui.get_node("score2")
		gui.set_text(score2node, "Your score was " .. self.score)
		local highscorenode = gui.get_node("highscore")
		gui.set_text(highscorenode, "The current highscore ".. self.highscore)
		savefunction()
		
	end
	
end

i understand my code is probably not the best organized and such but i just sent it all so u guys can see it all and maybe have a solution

At the bottom of your gui script in the on_message function you have:

savefunction()

That function expects a self variable so try changing it to:

savefunction(self)

That should, at least, clear the error you mentioned.

okay, now it changed to:

ERROR:SCRIPT: /main/ui.gui_script:24: unsupported value type in table: function
stack traceback:
[C]: in function ‘save’
/main/ui.gui_script:24: in function ‘savefunction’
/main/ui.gui_script:55: in function </main/ui.gui_script:30>

on line 24-26 i have
if not sys.save(sys.get_save_file(“zombiegame”, “gamedata”), filesave)then
pprint(“Gamedata not saved”)
end

on line 55 i have
savefunction(self)

It might be worth spending some time tidying up your code before continuing. You’ve got duplicated code from the lua module now in the script, constants declared but strings being used in their place half the time and a hiscore AND highscore variables for some reason.

It’s a mess that needs sorting if you’re going to understand what you’re doing.

2 Likes

Yeah you are right, i actually dont understand my own code when it comes to the saving and loading of the highscore variable, so if u have seen that i declared constants but used strings instead please correct me : ) and i thought i would have a hiscore and highscore variable because when u helped me earlier with my code i assumed it would be like that, so i should remove one of them?

1 Like

okay i replaced highscore with hiscore now on every place and now it works ! it saves the highscore between builds and when i restart the now so thats really nice, thanks alot man, another thing i wonder is how do i reset the highscore now ? if i want it to be 0 instead of 5300 which it currently is

1 Like

You could create a button that resets the high score. When that button has been pressed you simply do self.hiscore = 0 again.

1 Like