Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization Methods var_to_bytes_with_objects and bytes_to_var_with_objects Not Preserving Instance Property Values #93679

Open
MrZak-dev opened this issue Jun 27, 2024 · 3 comments

Comments

@MrZak-dev
Copy link

MrZak-dev commented Jun 27, 2024

Tested versions

4.3.beta2

System information

MacOS Sonoma, Compatibility Renderer

Issue description

It appears that the methods var_to_bytes_with_objects and bytes_to_var_with_objects do not work as expected when serializing and deserializing class instances. Specifically, properties of the instance revert to their initial values instead of retaining any changes made before serialization.

Steps to reproduce

  1. Create an instance of a class.
  2. Change a property of the instance.
  3. Serialize the instance using var_to_bytes_with_objects.
  4. Deserialize the instance using bytes_to_var_with_objects.
  5. Observe that the deserialized instance's properties have reverted to their initial values.

Example Code

func _ready():
    var instance = RandomClass.new() # create a custom class that extends `Object`
    instance.thing = "changed"
    
    var serialized = var_to_bytes_with_objects(instance)
    
    var deserialized: RandomClass = bytes_to_var_with_objects(serialized)
    
    assert(deserialized.thing == "changed") # this will fail , and the value is "hello" as the default set in the class property definition

Minimal reproduction project (MRP)

test.zip

@dalexeev
Copy link
Member

dalexeev commented Jun 28, 2024

You should export1 the property so that it is taken into account by the duplicate() method and serialization.

This is documented here, but we need to copy the note into descriptions of similar methods, see #80585.

Footnotes

  1. At least the PROPERTY_USAGE_STORAGE flag must be present, since 4.3 dev there is an annotation @export_storage.

@MrZak-dev
Copy link
Author

@dalexeev adding the @export_storage annotation works as expected , Thank you .. however i just find the documentation confusing a little bit when it says :

Note: Not all properties are included. Only properties that are configured with the @GlobalScope.PROPERTY_USAGE_STORAGE flag set will be serialized.

Then when we take a look at where this link goes :

PropertyUsageFlags PROPERTY_USAGE_STORAGE = 2
The property is serialized and saved in the scene file (default).

The way i interpreted the (default) is that this flag is already set by default for the class' properties. isn't it the case or am i missing something .

@dalexeev
Copy link
Member

PROPERTY_USAGE_DEFAULT is used by default for exported properties, in _get_property_list(), etc. But by default, script variables are not exported at all:

var a # SCRIPT_VARIABLE
@export_storage var b # STORAGE | SCRIPT_VARIABLE
@export var c: int # DEFAULT | SCRIPT_VARIABLE

We should probably clarify this in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants