Skip to content

Instantly share code, notes, and snippets.

@HungryProton
Last active April 10, 2025 08:39
Show Gist options
  • Save HungryProton/dc8cf8bb79bd3a62bd921e21e11509e6 to your computer and use it in GitHub Desktop.
Save HungryProton/dc8cf8bb79bd3a62bd921e21e11509e6 to your computer and use it in GitHub Desktop.
Serialize all script variables to a dictionary and back
class_name SerializableRefCounted
extends RefCounted
## Saves all user defined variables inside a dictionary
## Member variables from the built in class will not be included
func serialize() -> Dictionary:
var data := {}
for property in get_property_list():
var p_name: String = property.name
if property.usage != PROPERTY_USAGE_SCRIPT_VARIABLE:
continue
if p_name.begins_with("_"):
continue # Don't save private members
data[p_name] = get(p_name)
return data
## Restores the values
func deserialize(in_data: Dictionary) -> void:
for property_name: String in in_data.keys():
set(property_name, in_data[property_name])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment