How to avoid references by path in custom resources stored in user folder?

Godot Version

4.5.1

Question

I create a custom resource app_state.gd in the usual way by extending Resource.

#content of app_state.gd
class_name AppState extends Resource

@export var master_volume: float
@export var player_state: PlayerState

PlayerState is defined in a similar way by extending Resource.

#content of player_state.gd
class_name PlayerState extends Resource

@export var name: String
@export var items: Dictionary[Item] #Item is a Resource

I create an instance of AppState in the usual way and save it in res://initial_app_state.tres.

If the user changes the master volume or the player state or adds an item I create a copy of the res://initial_app_state.tres using duplicate_deep(Resource.DEEP_DUPLICATE_ALL)and save the resulting copy (and all contained sub-resources) in user://app_state.tres.

So far everything works as expected. I can load and save the app state. I create user://app_state.tres by calling ResourceSaver.save(_state, SAVE_PATH)without any save flags.

The problem occurs if I move app_state.gd (or the other scripts player_state.gd, item.gd) to another location within res://. After moving this script the resource user://app_state.trescan no longer be loaded without an error.

I figured that the resource user://app_state.tres contains paths instead of uids that reference all the above mentioned scripts containing the classes AppState, PlayerState, Item.

How can I make sure that user://app_state.tresonly contains uids as references to scripts?

As explained it the ResourceSaver.save() documentation:

Note: When the project is running, any generated UID associated with the resource will not be saved as the required code is only executed in editor mode.

So it’s not possible to save the uid while running the game.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.