#Resource version migration. How do I update a saved resource to the latest version?

1 messages · Page 1 of 1 (latest)

coral girder
#

Lets assume I have a resource:

@tool
class_name SomeRes
extends Resource

@export version := 1
@export var foo: bool

Now at some point during development, I rename the field foo into bar and update the version to 2. I create a static class to manage this

@tool
class_name SomeResMigrator

static func migrate_v1_v2(old_res: SomeRes) -> SomeRes:
  var new_res := SomeRes.new()
  new_res.bar = old_res.foo
  return new_res

Now in theory what this means is that when my plugin notices that the resource being loaded is out of date, it would create a copy of the resource that is up to date with the current SomeRes class, and strip the data from the old one.

The issue is that upon loading the .tres, Godot completely discards any fields that don't match up with the current definition. The result is that when I pass a version 1 SomeRes into the migration function, it doesn't even have anything in the foo field, resulting in an error for trying to access a Nil property

How do I be sure that Godot loads the resource .tres as-is including fields that don't exist in the current SomeRes definition so that I may copy the data from such fields into a new object?

#

Also, worth noting for anybody answering, I understand an easy hack to this exact example would be to leave all of the old fields in, but renaming fields is only one of many changes you may make to a resource during developement. That solution wouldn't work for, say, changing the type of a field.

stark panther
#

Sadly i think that the only way is to re-save them.
Then the script should update the available fields. But any values in the old fields would be discarded