How to overwrite a custom resource file in tool-mode?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bluepandion

I’ve made a script, that saves custom resources. Everything works fine until I try to overwrite an already existing resource, to update it.

When overwriting, the resource file is properly saved and I can see the data is correct when I look at it in an external text editor. However, when inspecting the resource in Godot, the editor still has the old version of the resource loaded.

If I first manually delete the file (in the filesystem dock, not via script) and then save the resource it is correctly loaded.

Is there any way to avoid this hassle? How can I tell Godot to reload a resource?

My save script looks like this at the moment:

var res = create_resource()	
var filepath = path+"../"+res.name+".tres"
ResourceSaver.save(filepath, res)
res.take_over_path(filepath)
:bust_in_silhouette: Reply From: bluepandion

Answering my own question:

In tool mode, if you’re overwriting a resource and expect it’s data to refresh within the editor you need to use an EditorPlugin script to re-scan the filesystem or update the status of the file individually.

Individual update would happen in this manner

var filesystem = get_editor_interface().get_resource_filesystem()
filesystem.update_file(filepath)

and scanning all files like this:

get_editor_interface().get_resource_filesystem().scan()