3 min read

Save/Load System

Table of Contents

Game Data Management

The template includes a simple yet powerful data management system that manages saving and loading operations for nodes that require data persistence. This functionality is orchestrated by the DataManager singleton.

The system already processes data for StateMachine, CharacterEntity, and PlayerEntity classes, covering most common use cases. You can extend it to any additional class, as described below.

Data Handling Functions

Nodes requiring data persistence must:

  • Belong to the save group
  • Implement these functions:
    • get_data: Defines what data to save.
    • receive_data: Processes loaded data.

The DataManager singleton handles all the rest.

The save group

The classes that already have the get_data and receive_data functions implemented will process these data:

  • StateMachine:

    • The index of the current state.
  • CharacterEntity:

    • Position
    • Facing direction
  • PlayerEntity:

    • Position
    • Facing direction
    • Current HP
    • Max HP
    • Inventory
    • Equipped weapon ID

Modify the get_data and receive_data functions to customize the data being saved and loaded.
You can assign the save group and implement these functions in any script to define exactly what data should be stored and retrieved.

Save Data

FunctionDescription
save_gameSaves the current game data to the file path defined in Const.SAVE_FILE_BASE_PATH.
save_level_dataStores the data of all nodes in the save group present in the current level that implement the get_data function.
ℹ️

The save_level_data function is called automatically by any Transfer node when leaving the current level.

A typical workflow will call save_level_data first and save_game a later time to save the game data to a file.

⚠️

Game data will be lost if the game is closed without calling the save_game function.

Load Data

FunctionDescription
load_gameRestores the full game data from the file path defined in Const.SAVE_FILE_BASE_PATH.
load_level_dataUpdates the data of all nodes in the save group present in the current level that implement the receive_data function.
ℹ️

The load_level_data function is called automatically by any Level node when entering a level.

A typical workflow will call load_game first and load_level_data a later time to load the level data from a file.

Summary

  • Add nodes to the save group for data persistence and implement get_data and receive_data functions to handle the data.
  • The DataManager singleton handles the saving and loading operations, you just need to call the appropriate functions.
  • The data management system ensures that saved data persists between levels and across game sessions.
ℹ️

You decide when to call the save and load functions based on the requirements of your game.

Is something unclear, or do you have suggestions to improve a part of this document? Feel free to share your thoughts leaving a comment below!