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
savegroup - Implement these functions:
get_data: Defines what data to save.receive_data: Processes loaded data.
The DataManager singleton handles all the rest.
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
| Function | Description |
|---|---|
save_game | Saves the current game data to the file path defined in Const.SAVE_FILE_BASE_PATH. |
save_level_data | Stores 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
| Function | Description |
|---|---|
load_game | Restores the full game data from the file path defined in Const.SAVE_FILE_BASE_PATH. |
load_level_data | Updates 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
savegroup for data persistence and implementget_dataandreceive_datafunctions to handle the data. - The
DataManagersingleton 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.