Get_var() on save file always returning null

Godot Version

4.4

Question

Why does my get_var function on a save file always return null? I’m genuinely confused as this wasn’t doing it before, but has randomly started and I cannot find any culprit

Here is how the save function works

func createNewSaveFile(index) : #index because we select the save file from a list
	var saveData = {}
	
	for playerChild in GameUtil.get_all_children(GameManager.partyMembersNode) : 
		if playerChild.has_method("OnSave") :
			saveData = playerChild.OnSave(saveData) #stores mostly simple data into the dictionary, but does also store extended Resource objects. I had this working before
			
	#save map name
	saveData["party"] = GameManager.getParty() ##gets an array of nodes, I've tried disabling this, does not change anything, I know this is sloppy, I'm planning on changing it
	saveData["map_name"] = SceneManager.currentSceneName
	saveData["player_position"] = GameManager.player.global_position
	
	var fileName = str("save_game_", index,".dat")
	
	if(index < saveFileListNames.size()) :
		fileName = saveFileListNames[index]
	
	var file = FileAccess.open("user://saves/"+fileName, FileAccess.WRITE)
	
	file.store_var(saveData,true)
	file.close()
	
	print("Game Saved")

Now the file.get_var returns null in my actual loading function, but I don’t think it’s the problem because even modifying the bottom of the above code to this will print the stored data as null.

	var begin = file.get_position()
	file.store_var(saveData,true)
	file.seek(begin)
	print(file.get_var(true))
	file.close()	

That was the only similar problem to mine that I could find online. I’ve checked in my loading function and it’s not loading the wrong file. (file_exists() returns true) When I try get_open_error() it returns 0, meaning it’s not having any errors. I’m genuinely stumped.

Things seemed to change when I added the line to store the party info and when I added code after the call to create the new save file to store the data from the Dialogic Plugin. As I mentioned about the code for storing party data, I’ve tried turning both of these off, but it doesn’t change anything. Here is the code where I actually call the function after the player has clicked on a file list item:

func _on_file_list_item_clicked(index, at_position, mouse_button_index):
	createNewSaveFile(index-1)
	Dialogic.Save.save(str(index-1))

Does anyone know what I’m missing? If you need more information let me know! I really want to get this thing working. Thank you!

Check file’s error status after each read/write operation using FileAccess::get_error()

1 Like

All returning 0 unfortunately, same with file.get_error()

var file = FileAccess.open("user://saves/"+fileName, FileAccess.WRITE)
	print(FileAccess.get_open_error())
	
	file.store_var(saveData,true)
	print(FileAccess.get_open_error())
	file.close()
	print(FileAccess.get_open_error())

Umm, didn’t you say the problem is with reading? Check the error when reading. And that should always be get_error(), not get_open_error()

Gotcha! That gave me error 14, can’t read! Thank you! Sorry I thought I mentioned, I tried get error before after reading, but I did get_open_error, which was incorrect as you said. Now I just gotta figure out why it can’t read

Found the solution, it was the function where I was trying to save whole nodes. Huge thanks to normalized for pointing out that I was using get_error wrong. Once I messed around with it a bit more, it started giving me error 18, which means end of file error. I switched from nodes to just saving string paths for the nodes and it works like a charm now!