Saving high scores with Puggle
Storing data for your game isn’t the most exciting part of development, but it is vital. Puggle provides a data storage system to make this functionality incredibly easy.
Unlike making things follow a mouse, data storage isn’t exactly the most visual thing so for this tutorial I’ve put together a very simple ‘game’ ( for lack of a better word ) that requires you to tap a moving ball to earn points.
The smaller the ball is the faster it’ll move but the more points it’ll earn you, and if you miss at any point it’s game over. Risk and reward!
Below is the final project and you can download the sourcecode here.
If you download the code, you’ll see the vast majority of it is simply about setting up the no doubt GOTY winning masterpiece and only a few lines of it are actually about saving out the score.
So rather than recreating the whole project line by line in this tutorial I’ll just explain how the data storage part works and for the rest please just check out the fully documented code that also demonstrates a few other Puggle features to boot.
The main hi score saving is done via this simple API call, as you can see we’re setting a new hi-score only if it’s higher than whatever our current best score is.
puggle.data:data:setIfHigher( "best", newScoreValue )
If there is no current best score, i.e. this is the game’s first run, then whatever value you give it here will be saved out.
Later on in the game you can then use this line to display it somewhere:
local best = puggle.data:get( "best" )
And that’s pretty much it! If you check out the Data code in the Puggle repo here you’ll see there are lots of other useful functions such as incrementing/decrementing, deleting, and checking if a value exists or not.
Putting all of these together you can very easily deal with all the permanent data storage your game will need.