FileAccess not creating nor opening file

Godot Version

4.6 stable Windows 11

Question

I’m trying to save and load input bindings separately from the rest of my save data, because json strings that I use for the rest of my data can’t properly store InputEvents. This is the code:

func save_data() -> void :
	print(name,": saving input bindings...")
	var input_data : Dictionary = input_actions
	var input_file : FileAccess = FileAccess.open(OS.get_data_dir() + "/game_name/Inputs", FileAccess.WRITE)
	if not input_file == null :
		print("input file is not null")
		input_file.store_var(input_data, true)
func load_data() -> void :
	print(name, ": loading input bindings...")
	var input_file : FileAccess = FileAccess.open(OS.get_data_dir() + "/game_name/Inputs", FileAccess.READ)
	if not input_file:
		print("womp womp")
		return
	var input_data : Array = input_file.get_var(true)
	if input_data.is_empty():
		return
	print(name,": actions:", input_data[0].size()," device:",input_data[1])
	input_actions = input_data[0]

But it seems that, in the saving function, input_file returns null, and in the loading function, not input_file returns true. Why is the file not being created? This worked fine with my other save data, but not the input bindings. Any help is appreciated.

Does the file already exist?

I think FileAccess.WRITE creates a given file if it does not exist, but FileAccess.READ throws an error if the given file does not exist

I looked at the data directory and messed around a little with it, and it seems you need to add the file name you want to create to the directory. Now it no longer returns null and the file has been created, but my input data is still not being saved, somehow. This is my input dictionary:

var input_actions : Dictionary = {
	"shoot": {
		"MANK" : InputMap.action_get_events("shoot")[0],
		"GAMEPAD" : InputMap.action_get_events("shoot")[1],
	},
	"skill": {
		"MANK" : InputMap.action_get_events("skill")[0],
		"GAMEPAD" : InputMap.action_get_events("skill")[1],
		},
	"interact": {
		"MANK" : InputMap.action_get_events("interact")[0],
		"GAMEPAD" : InputMap.action_get_events("interact")[1],
		},
	"active": {
		"MANK" : InputMap.action_get_events("active")[0],
		"GAMEPAD" : InputMap.action_get_events("active")[1],
		},
	"focus": {
		"MANK" : InputMap.action_get_events("focus")[0],
		"GAMEPAD" : InputMap.action_get_events("focus")[1],
		},
	"spell": {
		"MANK" : InputMap.action_get_events("spell")[0],
		"GAMEPAD" : InputMap.action_get_events("spell")[1],
		},
} : set = _update_inputs

And this is the setter function:

func _update_inputs(new_inputs : Dictionary) -> void :
	input_actions = new_inputs
	print(input_actions)
	for action : StringName in input_actions.keys() :
		InputMap.action_erase_events(action)
		InputMap.action_add_event(action, input_actions[action]["MANK"])
		InputMap.action_add_event(action, input_actions[action]["GAMEPAD"])
	save_data()
1 Like