Save something your script owns
On this page
OPTIONAL NATIVE GDSCRIPT
Use this when a Godot script keeps a value of its own. Ordinary PCA variables and inventory already travel with the checkpoint.
1
Open a global GDScript record.#
Under GDScripts, open your script in the GDScript Designer, or add one. Give it a stable Script ID, such as keepsake, and tick Keep global instance alive. PCA uses that ID to match its saved data the next time the game runs.
The GDScript Designer’s record settings. Enable the global instance for this script.
2
Add these two functions to the script body.#
This script remembers a number engraved on a keepsake. On save, the first function returns its value. On load, the second reads it back. An older checkpoint with no engraving gets 0.
var engraving := 0func _pca_capture_save_state() -> Dictionary: return {"engraving": engraving}func _pca_restore_save_state(saved: Dictionary) -> void: engraving = int(saved.get("engraving", 0))3
Validate, apply and test in the running game.#
Click Validate, Apply Changes and Save Project. Let your normal game code set engraving to 7. Save, quit, relaunch and load. In the checkpoint report, look under nativeSaveData script/keepsake engraving. We verified that the room-restored event could already read 7.
What fits in the dictionary? Use numbers, text, booleans, lists and dictionaries with text keys. Store a node’s stable ID, or its position as a list of numbers. An invalid capture stops the save before it replaces the slot. Both callbacks run synchronously, so return the data directly.
For an object created by your own code, give it the same two methods. After setup call Runtime.register_native_save_provider("lens_controller", self). When removing it, call Runtime.unregister_native_save_provider("lens_controller", self). Each independent provider needs a unique, stable key.
STUDIO 0.86.3.8 / 17
