How would you save match stats in a tablular persistent storage?

Godot Version

4.4.1

Question

I am building a small offline game and want to save match stats such as matchId, date, result, score etc.
The last N matches needs to be fetched and displayed in a stats screen when needed.

Currently I have 2 approaches but neither sit right with me, looking for guidance from experienced folks!

Option 1 - FileAccess

Refering to Saving games — Godot Engine (stable) documentation in English

FileAccess.save_line()

with the json of the concluded match details seems simple enough, but I am not sure about a couple of things here.

  1. This does not seem optimized for queries (e.g. I want to display a list of top 5 scoring matches in the future)
  2. Will this be scalable once the player has 1000s of matches?

Option 2 - SQLite

Using a plugin like GitHub - 2shady4u/godot-sqlite: GDExtension wrapper for SQLite (Godot 4.x+)
This seems more optimized for the task, but for some concerns -

  1. Can I encrypt the DB so the player can’t just open and edit values unrestricted?
  2. This doesn’t seem to allow write access on some platforms - See issue: GitHub - 2shady4u/godot-sqlite: GDExtension wrapper for SQLite (Godot 4.x+)

What would be the best way to store fixed schema data in a scalable way and allow querying in your opinion?

Follow up:
If you were doing the same on an online-enabled game with cloud saves what would you do differently?

Thanks in advance.

A simple local file would probably do; you could do JSON, though personally for what you’re talking about I’d probably suggest a binary file with a relatively simple format. Maybe something like the equivalent of:

typedef struct
{
  char     match_name[32]; // Or whatever identifying info you have...
  uint8_t  side_1_uuid[16]; // Or some other method of IDing the sides.
  uint8_t  side_2_uuid[16];
  uint64_t time;           // UNIX timestamp, or some equivalent.
  uint32_t score[2];
  // Other metadata...
} MATCH_RECORD;

Godot can read/write that pretty easily. Since the records are fixed size, search should be relatively quick, though honestly even with JSON it ought to be quick until your number of matches gets well into the millions.

1 Like

I would recommend doing the same thing for both local saves and cloud saves - find a way to limit the data you are keeping.

You mentioned having a list of Top 5 scoring matches. One way to do that would be to keep just 5 slots in your data file, replacing the lowest score whenever a higher score is posted.

A similar thing can be done with the last N matches.

Ideally you only want to store the information the player will be shown.