Saving a config file doesn't work with "user://filename.cfg" but it does with "res://filename.cfg"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

This code works:

func configFileSave():
	var configFile = ConfigFile.new()
	var file_to_save= "res://stats.cfg"
	var err = configFile.load(file_to_save)
	if err == OK:
		configFile.set_value("Scores","totalGamesWon",globals.totalGamesWon)
		configFile.set_value("Scores","totalGamesLost",globals.totalGamesLost)	
		configFile.set_value("Scores","totalGamesTied",globals.totalGamesTied)	
		configFile.save(file_to_save)
		print("score written to ", file_to_save)
	else:
		print("there was an error with the file")

The problem is, for it to work on Android, I need to change the var file_to_save= "res://stats.cfg" line to var file_to_save= "user://stats.cfg"

In another game I made I had an issue where my Android build wouldn’t work unless I changed it from res to user. In this game though, it’s not even working with user.

Can anyone offer any advice as to why? Thanks a tonne…

:bust_in_silhouette: Reply From: volzhs

AFAIK, res:// is read only directory for Android.
You can’t add/modify/delete in res:// which is installation directory.
If you need to save something, should use user://

Thanks for the answer. If you look at the question above you can see I have been trying to use user but having no success. Do you by chance have any idea about why it’s not working at all with user? Thanks a tonne…

Robster | 2016-12-18 08:55

:bust_in_silhouette: Reply From: Jatz

Android is built on Linux, In linux they like to restrict permissions for a lot of the folders you need to use.

Make sure that your app has the required permissions to access the user directory.

In godot, when you try exporting to android on the ‘option’ side of the export dialog box. Scroll down until you reach the section for ‘Permissions’, check ‘Read user directory’
and check ‘Write user directory’
This doc page might also help: android developer documentation for declaring variables

Hi and thanks for your answer. This is currently on OSX whilst I develop it, but it’s still failing on anything except res. Perhaps I have to swap to user when exporting for Android, and change it back to res when exporting for OSX? Maybe that would solve it. I hadn’t realised if that was the case last time. I’ll check into it once I resolve another issue with Android export I’m having. Thank you.

Robster | 2016-12-19 00:05