Last active
April 10, 2025 08:39
-
-
Save HungryProton/dc8cf8bb79bd3a62bd921e21e11509e6 to your computer and use it in GitHub Desktop.
Serialize all script variables to a dictionary and back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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