ResourceSaver.save() causing game crash

Godot Version

4.1.1.stable.official

Question

I have the following code in an autoloader to save the player’s game in a resource:

func save_game(save_game_name: String):
	var saved_game: SavedGame = SavedGame.new()
	
	var saved_data: Array[SavedData] = []
	get_tree().call_group("game_events", "on_save_game", saved_data)
	saved_game.saved_data = saved_data
	
	var user_dir = DirAccess.open("user://")
	
	if (!user_dir.dir_exists(PlayerData.character_name)):
		user_dir.make_dir(PlayerData.character_name)
	
	ResourceSaver.save(saved_game, character_folder_path + "/" + save_game_name + ".tres")

If I call this code immediately, in the _ready() function, it works flawlessly without issue.

If however, I call the above function a few seconds later via a save menu button, it executes, hits the ResourceSaver.save() line, creates the file, but the game then immediately freezes and crashes after about 3 seconds.

The created save file additionally is empty, so clearly it doesn’t correctly populate the data.

Finally, there are no errors or issues in the console. The game window crashes without any output.

Does anyone know why ResourceSaver might be doing this? Is there a way to see why this might be crashing? Currently I am getting no error information anywhere.

I think the problem is in other place, maybe where you gather the data. ResourceSaver.save only tries to write a file and return an error constant If something fails, but is not a blocking method

I initially thought that, but the ResourceSaver.save() will succeed in _ready(), it just fails at any other moment.

I verified the data being saved and it is identical in both scenarios. Just for some reason, calling this save function after _ready() is causing a crash on the ResourceSaver.save() line.