OneLeader

Guide · Godot 4

Add an online leaderboard to a Godot game

Give your Godot game a global high-score table in about ten minutes, with no server of your own. Players don't need an account: each one is an anonymous player remembered on their device.

By the end, your game-over screen saves the score, tells the player their all-time rank and shows the top 10, with their own row highlighted. It works in desktop, mobile and web exports.

1. Set up the leaderboard

  1. Sign in to the dashboard with GitHub or Google and create an app for your game. The free plan covers 10,000 scores a month.
  2. Under API keys, create a publishable key (lb_pk_…). It's safe to ship in the game.
  3. Under Metrics, create highscore with the aggregation max, so each player keeps their best run.
  4. Under Leaderboards, create alltime on highscore, ranked highest first.
  5. Web exports only: under Allowed origins, add the origin you'll publish to, such as your itch.io or own domain. Desktop and mobile builds send no origin and need nothing here.

2. Install the addon

Download oneleader-godot and copy its addons/oneleader folder into your project's addons/ folder. There's no plugin to enable: the OneLeaderClient class is available straight away. The README lists every call.

3. Add a leaderboard autoload

Save this as res://leaderboard.gd, paste in your publishable key, and add it under Project › Project Settings › Globals › Autoload with the name Leaderboard. It creates the anonymous player on the first run and saves it to user://. If the leaderboard can't be reached (offline, over quota, rate limited) it logs a warning and returns 0 or null, so the game carries on.

extends OneLeaderClient
## Autoload this as "Leaderboard". The leaderboard is a bonus: offline, over quota or rate limited, the game carries on without it.


func _init() -> void:
	publishable_key = "lb_pk_..."


## Records a finished run. Returns the player's all-time rank (by their best score), or 0 if it couldn't be saved.
func submit_run(score: float) -> int:
	if not has_session() and not (await start_session()).ok:
		return 0
	var receipt := await submit_score("highscore", score)
	if not receipt.ok:
		push_warning("Leaderboard: %s (%d)" % [receipt.message, receipt.status])
		return 0
	for board in receipt.data.leaderboards:
		if board.key == "alltime":
			return int(board.rank)
	return 0


## The top 10, plus the player's own row in "user" even when they're further down. null if it couldn't be read.
func top_ten() -> Variant:
	var page := await get_leaderboard("alltime", {"limit": 10})
	if not page.ok:
		push_warning("Leaderboard: %s (%d)" % [page.message, page.status])
		return null
	return page.data

4. Show it on game over

Save this as res://game_over.gd. It builds its own UI, so there's no scene to set up. When a run ends, add it over your game scene:

add_child(GameOver.new(score))

The screen draws straight away, then fills in the rank and the table once they arrive. On the first run it also asks for the player's name.

class_name GameOver
extends CanvasLayer
## Shows the score, the player's rank and the top 10 over your game scene.
## Add it when a run ends: add_child(GameOver.new(score))

var score: float
var status := Label.new()
var rows := VBoxContainer.new()
var name_edit := LineEdit.new()


func _init(final_score: float) -> void:
	score = final_score


func _ready() -> void:
	var center := CenterContainer.new()
	center.set_anchors_preset(Control.PRESET_FULL_RECT)
	add_child(center)
	var box := VBoxContainer.new()
	center.add_child(box)
	box.add_child(_label("Score: %s" % _num(score), 32))
	status.text = "Saving your score…"
	box.add_child(status)
	box.add_child(rows)
	name_edit.placeholder_text = "Your name for the leaderboard"
	name_edit.max_length = 32
	name_edit.hide()
	name_edit.text_submitted.connect(_save_name)
	box.add_child(name_edit)
	var again := Button.new()
	again.text = "Play again"
	again.pressed.connect(get_tree().reload_current_scene)
	box.add_child(again)

	var rank := await Leaderboard.submit_run(score)
	status.text = "Your best is #%d all time" % rank if rank else "Leaderboard unavailable right now"
	_show_board()


func _show_board() -> void:
	var board: Variant = await Leaderboard.top_ten()
	if board == null:
		return
	for old in rows.get_children():
		old.queue_free()
	var me: Variant = board.get("user")
	for row in board.rankings:
		var label := _label("%d. %s   %s" % [row.rank, row.display_name if row.display_name else "Anonymous", _num(row.value)], 18)
		if me and row.player_id == me.player_id:
			label.modulate = Color("#30b0c7")
		rows.add_child(label)
	# First run: the player has no name yet, so ask for one.
	name_edit.visible = me != null and me.display_name == null


func _save_name(new_name: String) -> void:
	name_edit.hide()
	if new_name.strip_edges() and (await Leaderboard.set_display_name(new_name)).ok:
		_show_board()


func _label(text: String, size: int) -> Label:
	var label := Label.new()
	label.text = text
	label.add_theme_font_size_override("font_size", size)
	label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	return label


func _num(value: float) -> String: # JSON numbers are floats: show 42, not 42.0
	return str(int(value)) if value == roundf(value) else str(value)

Display names go into plain Labels, not RichTextLabels, so whatever a player types can't inject BBCode.

5. Try it

Run the game, finish a run and you should see yourself at #1. To play as a second player, delete oneleader_session.json from the user data folder (Project › Open User Data Folder). If the status says the leaderboard is unavailable, check the Output panel: a 404 means the metric or leaderboard key doesn't match, and in a web export a 403 means the page's origin isn't in Allowed origins.

Next steps