How to auto-save generated resources with rest of scene

Godot Version

4.3

Question

I have a tool script to generate mesh date within a MeshInstance3D. I want the data to be saved in a binary resource external to the *.tscn file. I found that setting the resource path manually in the editor would trigger this behavior, with the resource being saved when the scene is saved.

However I can’t et the same thing to work in my script code. I set a valid resource path each time I regenerate the mesh, e.g., with new parameters. I’m creating a new resource each time, so I use take_over_path() with the resource. The editor doesn’t save the resource though. I can save the resource directly using ResourceSaver, but I’d rather save the files with the rest of the scene like the editor usually does, not after every edit:

var root := get_tree().edited_scene_root
var scene_dir := root.scene_file_path.get_base_dir()
var scene_filename_no_ext := root.scene_file_path.get_file().get_basename()
var res_dir := scene_dir + "/meshres/"
var base_res_path := res_dir + scene_filename_no_ext + "_" + name
if !DirAccess.dir_exists_absolute(res_dir):
	DirAccess.make_dir_absolute(res_dir)
mesh.take_over_path(base_res_path + "_mesh.res")
mesh.emit_changed() # doesn't help
ResourceSaver.save(mesh) # works, but would like to defer

It seems like there must be a flag somewhere that informs the editor that a resource has been changed and needs saving that is missing from my code.