How to let ResourceSaver omit some values that remains default while saving Resource

Godot Version

v4.5.dev.custom_build [bc651a3de]
(Built from my bug-fixed code)

Question

I am making a plugin now, and I have defined some data class extending Resource. Here are one example of those class (I skipped the _init and other unnecessary methods):

class_name GraphNodeSlotInfo
extends Resource
## Class for storing the single GraphNode slot info
##
##
# End of class document.

enum SlotLocation
{
    left,
    right
}

## Index of the slot. Different for left/right side.
@export var index: int

## The location of the slot on the node.
@export var location: SlotLocation

const default_connection_category = 0
## `type_left`/`type_right` for the slot. By default, set to 0 in this project.
@export var connection_category: int = default_connection_category

const default_colour = Color(0.7, 0.7, 0.7, 1.0)
## Colour of the slot.
@export var colour: Color = default_colour

const default_icon_path = ""
## Icon of the slot.
@export var icon: Texture2D = null
## EditorIcon path of the slot's icon.
@export_dir var icon_path: String = default_icon_path

When I use ResourceSaver to save this, I found that some values with default are also saved:

colour = Color(0.7, 0.7, 0.7, 1)
icon_path = ""

How can I tell the ResourceSaver do not save the value that remains default (just like the behaviour of tscn files). I have tried _property_can_revert but that does not seems to work.

func _property_can_revert(property: StringName) -> bool:
    return property in self.list__revertable_properties

func _property_get_revert(property: StringName) -> Variant:
    match property:
        "connection_category": return self.default_connection_category
        "colour": return self.default_colour
        "icon_path": return self.default_icon_path
        _: return null

I don’t think it’s possible because when it writes the value to the file it gets the default_value from ClassDB which has no knowledge about class_name scripts.

This is different to what it does to save the tscn files where it uses SceneState.get_node_property_count() to get which exported properties were overriden and save them.

1 Like

Thank you for your reply ! Let me check the code after, and see if I can submit a proposal and implement that.
Thanks again :grinning_face_with_smiling_eyes:

I found there is an existing pull-request implementing this feature: Core: Do not save default values of script properties in `.tres` files by dalexeev · Pull Request #80897 · godotengine/godot · GitHub