PersistanceKey
Type: GeneralA helper node that stores the value of a Node's property independent of the current scene.
Setup
- Add a PersistanceKey as a child of a Node that you want to store properties of.
- Add the StringName of those properties to 'persistant_properties'.
Code
Released under CC0 1.0 licence (opens in a new tab): use, change and share it in any project, without asking and without giving credit.
GDScript
@tool
class_name PersistanceKey
extends Node
## A helper node that stores the value of a [Node]'s [b]property[/b].
## The dictionnary containing all [PersistanceKey]'s values.
static var persistance_keys: Dictionary[String, Variant]
## A list of properties to store.
@export_custom(PROPERTY_HINT_TYPE_STRING, "") var persistant_properties: Array[StringName]
func _ready() -> void:
load_persistance_keys()
func _exit_tree() -> void:
save_persistance_keys()
func _validate_property(property: Dictionary) -> void:
# Add properties hints
if property.name == &"persistant_properties":
property.hint_string = "%d/%d:%s" % [TYPE_STRING_NAME, PROPERTY_HINT_ENUM_SUGGESTION, _get_parent_properties()]
# Used to get the parent Node's properties.
func _get_parent_properties() -> String:
# Proprty usages to exclude from the list
const NON_VARIABLE_USAGE: int = (
PROPERTY_USAGE_CATEGORY
| PROPERTY_USAGE_GROUP
| PROPERTY_USAGE_SUBGROUP
)
var parent: Node = get_parent()
if parent == null: return "No parent Node found."
# Get built-in proprties
var parent_properties: Array[Dictionary] = parent.get_property_list()
# Get script properties
var parent_script: Script = parent.get_script()
if parent_script:
parent_properties += parent_script.get_script_property_list()
# Get properties names
var names: PackedStringArray
for prop in parent_properties:
# Exclude properties that are Categories, Groups, or Subgroups
if (prop.usage & NON_VARIABLE_USAGE) != 0: continue
# Renames the 'script' property to '----------' to separate built-in
# properties from script's properties.
if prop.name == "script":
names.append("----------")
elif not names.has(prop.name):
names.append(prop.name)
return ",".join(names)
## Call this function to load the stored properties and update the owner's properties.
func load_persistance_keys() -> void:
for property in persistant_properties:
var property_key: String = "%s:%s" % [get_path(), property]
if persistance_keys.has(property_key):
var persistant_property_value = persistance_keys.get(property_key)
get_parent().set(property, persistant_property_value)
## Call this function to store the properties.
func save_persistance_keys() -> void:
for property in persistant_properties:
var property_key: String = "%s:%s" % [get_path(), property]
var property_value: Variant = get_parent().get(property)
persistance_keys.set(property_key, property_value)Usage example
GDScript
# In a global save script
func save_game() -> void:
var save_file := ConfigFile.new()
# [...]
for i in PersistanceKey.persistance_keys.size():
var key = PersistanceKey.persistance_keys.keys()[i]
var value = PersistanceKey.persistance_keys[key]
save_file.set_value("persistance_keys", key, value)
print(key, " ", value)
save_file.save("user://save_file.cfg")