How to Save and Load Godot Game Data

In our game, we may have the current score, level, and items collected that should be stored and reloaded if the player wants to quit the game and restart it later. Also, there is the situation of single player or multi-player gaming to consider.

So, we will have to consider saving data locally, to a server, or to a cloud-based web service.

In this tutorial we will save and load data to/from the local device in a most simple way to get started, and cover the other more advanced options in later tutorials. So let's get started ...

Global Game Data

All of the Nodes/Scenes in our game should be able to access the game data concerning the current score etc. The way to facilitate this is with an Autoload script. This implements a singleton which is a single instance of a game object that cannot be duplicated like we would do with other game objects that share functionality, but have different property values such as position and damage level.

Our Autoload script makes available global variables to get/set or constants and functions that any node in our game will be able to use.

Let's follow this up with a simple example of a game data Autoload script that stores the score and level for the player of the game.

Coding Game Data

We will use a Dictionary for our Player data that easily maps to a common data format called JSON for storing as text. Here is the GDScipt code for our data.gd Autoload script. This contains the Player data and functions to load and save the data:

extends Node

const FILE_NAME = "user://game-data.json"

var player = {
	"name": "Zippy",
	"score": 0,
	"level": 1,
	"high_score": {
		"value": 0,
		"date": ""
	}
}

func save():
	var file = File.new()
	file.open(FILE_NAME, File.WRITE)
	file.store_string(to_json(player))
	file.close()

func load():
	var file = File.new()
	if file.file_exists(FILE_NAME):
		file.open(FILE_NAME, File.READ)
		var data = parse_json(file.get_as_text())
		file.close()
		if typeof(data) == TYPE_DICTIONARY:
			player = data
		else:
			printerr("Corrupted data!")
	else:
		printerr("No saved data!")

After setting up the script as an Autoload in our Project settings, any node may call data.save() to save the Player data and data.load() to load the data. And the player data is accessed as data.player.score for example.

Note: the method names of save and load are very applicable to what we want to do here but load is already defined by the engine for loading resource files, and we have done a method override here so we can no longer use the original function in this script.

Encrypting Game Data

In the above code for saving and loading game data we made no attempt to hide the details in the JSON data saved to disk. So the data file could be edited externally to say inflate the score or to add lives. To get around this, we could use the file.open_encrypted_with_pass method where we supply a password and the data is encrypted and hence incomprehensible to a casual hacker.

Support this website!Godot Engine Game Development in 24 Hours*

* As an Amazon Associate I earn from qualifying purchases.