This guide covers how to persist placed objects across game sessions in GridBuilding 5.0.2. While the maintenance version uses component-based persistence, it establishes patterns that carry forward into future versions.
Core Concepts
Persistence in 5.0.2 relies on three key elements:
- PlaceableInstance: A runtime component attached to every valid placement. It holds the “DNA” of the object (template ID, original resource path).
- Serialization: The process of converting the node hierarchy into a flat Dictionary.
- Deserialization: Reconstructing the node tree from data, ensuring all dependencies (scripts, resources) are relinked.
The Serialization Contract
A valid save entry in 5.0.2 looks like this:
| |
PlaceableInstance Component
The PlaceableInstance script (res://addons/grid_building/placeable_instance.gd) exposes two critical methods:
save(with_custom_data: bool) -> Dictionary: serializes the node.instance_from_save(data: Dictionary, parent: Node) -> Node: static factory method to rebuild the node.
Implementation Guide
Step 1: Saving the World
To save your level, iterate through the PlaceableInstance group. This group is automatically managed by the system.
| |
Step 2: Loading the World
Loading is a two-step process: Clear the existing world, then rebuild from data.
| |
Handling Edge Cases
- Missing Resources: If a
placeable_idresource path no longer exists (e.g., you deleted the file),instance_from_savewill fail gracefully. Ensure your loading logic handles null returns. - Version Migrations: 5.0.2 does not have a built-in schema migration system. If you change your data structure, you must handle version checking in your own code.
Validated By
The save/load architecture and serialization patterns are verified by the following test suites:
- Serialization Protocol: res://addons/grid_building/test/building/placement/placement_report_unit_test.gd — Validates the serialization of placement metadata and report integrity.
- Component Implementation: res://addons/grid_building/placeable_instance.gd — Core component that manages instance lifecycle and serialization hooks.
- Metadata Integrity: res://addons/grid_building/test/utilities/data/composition_container_subresources_test.gd — Ensures that configuration data used during reconstruction is properly persisted.