A simple library for storing, loading, and inputting high score records in your PICO-8 games.
Clone this repo or download the files, then move them into the directory of the project you would like to include them in. Once you have them in your project, use #include
statements to include the files you want. highscore.lua
is always required.
After you've included the libraries you'd like to use, you'll need to call cartdata
with the name of your cart to enable persistent storage.
#include highscore.lua
#include highscore.input.lua
#include highscore.table.lua
cartdata("my_game_v1")
This library is based on the high score implementation by Paranoid Cactus in Lava Joe. It provides developers with a simple way to implement top ten score boards in arcade-like PICO-8 games.
I've tried to be concious of token usage, but also provide a friendly interface for managing high score records. The lirbary is also split into three separate files to give you the option of including just what you need. If you decide to implement your own input or table, the corresponding libraries in this project can act as example implementations.
If you see any opporutunities for token optimization, please open a pull request.
This is the core highscore
and is required if you want to also use highscore.input.lua
or highscore.table.lua
. This library provides the ability to store and load up to ten three character names and 16-bit numbers to cartdata.
This will initialize the highscore libary and load any existing records from memory. By default, highscore will use 50 bytes of data starting at address 0x5e00
. highscore.init
can be called with a specific memory address if you need to store score records in another location.
Requirement: Before you can initialize the highscore
library, you need to call cartdata
with your cart name.
#include highscore.lua
cartdata("example_game_v1")
highscore.init()
-- initialize with memory address
highscore.init(0x5e00)
Add a three character name and 16-bit interger score to the highscore table. If the value of the score is less than the lowest score in the table, this function has no effect.
highscore.add("kev", 1000)
After a record is added successfully added, all records are stored to cartdata.
Get the index where a given value would end up in the highscore table. This does not change the in memory highscore table, and it does not write to cartdata.
This method is useful for determining if a score would be added to the table so that you can accept user input, display a "new high score!" message, etc.
if highscore.index(1000) == 1 then
print "you're the best!"
elseif highscore.index(1000) then
print "new record!"
else
print "better luck next time..."
end
Reset the region of cart data used to store highscore records. This could be called in a number scenarios: you've updated your game version, under various gameplay conditions, or as a menu item.
-- basic usage
highscore.reset()
-- menu item example
menuitem(1, "reset scores", highscore.reset)
-- use with add to populate default values
highscore.reset()
for i=1,10 do
highscore.add("cpu",10)
end
Returns a table of the current highscore records. Each record is a table with a name
and score
key.
for record in all(highscore.records) do
print record.name..": "..record.score
end
Return the last updated record index from the highscore table. This is useful for showing new high scores on an end screen, for example.
highscore.records -- {}
highscore.add("kev",1000)
highscore.last_index -- 1
A simple implementation of a three character name input.
Return the current string value of the name.
highscore.input.name -- aaa
Return the current character index.
highscore.input.index -- 1
Update the highscore input. This handles accepting up, down, left, and right inputs to select the index and character of the name input.
function _update()
highscore.input.update()
end
By default this draws the input centered on the screen but you can pass x
and y
values to change the position.
function _draw()
cls()
highscore.input.draw()
end
A simple implementation of a high score table. Most developers will probably want to customize their score display, but if you just need simple scores, or want a placeholder until you customize, this should do the trick.
This table will show all the names and scores and place a star next to the last updated score.
Draw the high score table to the screen. Be default the table will be centered but the draw
function accepts x
and y
arguments for fine tuning the postition.
function _draw()
cls()
highscore.table.draw()
end
A small example tapper game is available as a demonstration of implementing all three libraries.