Score saved and loaded on death

So I think this is what I’ve gathered from all the information:

--- Name tag for lua module
--- “shared_data.module”

local M = {
player  = {
    highscore = 0, 
    score = 0
}
}
return M

and then Score save/load added to loader script


---loader script name 
---loader 

function save_gamedata(self)
-- reset player score
shared_data.player.score = 0
-- save gamedata
if not  sys.save(save_file, shared_data) then
    pprint(“gamedata not saved.”)
end
end

function load_gamedata(self)
    -- load game data
    local gamedata = sys.load(save_file)
    if not next(gamedata) then
        pprint(“gamedata empty”)
    else
        pprint(gamedata)
        Shared_data.player.highscore = gamedata.player.highscore
    end
end

function init(self)
    msg.post(“.”, “acquire_input_focus”)
    load_gamedata(self)
    load_menu(self)
end

function on_message(self, message_id, message, sender)
…
elseif message_id == hash(“game_over”) then
    unload_main(self)
    load_menu(self)
    save_gamedata(self)
msg.post(“/mainmenu#gui”, “update_score”, { score = gamedata.player.highscore }
elseif...
end
end

and finally add a script to the main menu gui script to post the score to the main menu score screen which is where the best score or high score will be shown

function on_message(self, message_id, message, sender)
if message_id == hash(“update_score”) then
-- set the score counter to new score
local score_node = gui.get_node(“score”)
gui.set_text(score_node, “SCORE: “.. message.score)
end
end

…fingers crossed this works (will have to try when i get home)

Backend systems such as Facebook Instant Games, PlayFab etc are very specific about how they read and store data on their servers and what API functions you use to send the data to their servers.

Start with what you have now and you’ll be able to adapt it once you add the backend integration. Don’t try to do everything at once. It will be overwhelming.

1 Like

We should create some super basic tutorials/manuals explaining these kind of things.

  • How do I save a high score between play sessions?
  • How do I save player settings?
  • How do I transition between multiple screens in my game?
  • How do I pause my game?

What else? Please provide ideas of things that you need help understanding!

6 Likes
  • maybe an inventory tutorial? though I guess that’s sort of similar to how scoring is done.
  • Character selection/customisation?
  • maybe an options pause menu where you can adjust things like voume, input options(inverted controls, etc)
  • mini map and pause menu large map with character tracking?
  • Checkpoints?
  • dare i say it …fast travel …though i feel this spoils some gameplay and makes it too easy
1 Like

I guess it’s not super basic, but this problem seems to show up on the forums perhaps more often than anything else.

Just when i think i’m close… :roll_eyes: lol… Seem to be getting this error message on the main menu script: any clues?
Compilation failed: function arguments expected near '…'


function on_message(self, message_id, message, sender)
	if message_id == hash(“update_score”) then
		-- set the score counter to new score
		local score_node = gui.get_node(“score”)
		gui.set_text(score_node, “SCORE: “.. message.score)
	end
end

function on_input(self, action_id, action)
	if(action_id == hash("click") and action.released == true) then
		local textBegin = gui.get_node("textBeginGame")
		if(gui.pick_node(textBegin,action.x,action.y)) then
			msg.post("loader:/go#loader", "start_game")
		end
	end
	if(action_id == hash("click") and action.released == true) then
		local textExit = gui.get_node("textExitGame")
		if(gui.pick_node(textExit,action.x,action.y)) then
			msg.post("@system:", "exit", {code = 0})
		end
	end	

	

end

and this error message on the loader script:

Compilation failed: ‘)’ expected near the ‘#’

function save_gamedata(self)
	-- reset player score
	shared_data.player.score = 0
	-- save gamedata
	if not sys.save("save.txt", shared_data) then
		print("Gamedata not saved")
	end
end
function init(self)
	msg.post(".", "acquire_input_focus")
	load_gamedata(self)
	load_menu(self)
end
function on_message(self, message_id, message, sender)
	if message_id == hash("start_game") then
		unload_menu(self)
		load_main(self)
	end
	if message_id == hash(“game_over”) then
		unload_main(self)
		load_menu(self)
		save_gamedata(self)
		msg.post(“/mainmenu#gui”, “update_score”, { score = gamedata.player.highscore }
	elseif...
end
end

end

I don’t see the source of the first error (there should be line number in the error message) but you’re missing a closing bracket in the second script:

msg.post(“/mainmenu#gui”, “update_score”, { score = gamedata.player.highscore })
1 Like

The first one - It mentions the line at the top where “SCORE:” is … good spot on the main menu code ta! I should have known coming from unity which needs endless amounts of brackets … whoops!

Managed to solve the issue now :slight_smile: …though now for some reason when I load the loader script as the first window, I cant seem to click on any of my buttons, there is no error message showing, but the start game and exit game buttons don’t want to work now, it doesn’t register any clicks…the only message I get on the console is the "pprint(“gamedata empty”) so unless that has anything to do with it…but it’s not coming up as an error?

Did you acquire input focus in the loader as well? If the other screens are loaded via collection proxies their “parent” also needs input focus.

1 Like