.cfg file doesnt work for android export.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unholy182000

Hello i am using .cfg file and use ConfigFile helper class to read and save values for my game. It works well enough for windows and html5 exports. BUt when i tried to export to android my values says “null” where there should be integers. My file is in the user:// directory and i gave proper permissions as *.cfg in export options. I also gave permission in read and write user dictionary in android export permissions.
What am i doing wrong?

I uploaded a vey simple program to gave an example of my problem.


you can try to export this project to android you will see the value sees only “null”

unholy182000 | 2021-01-14 20:26

extends Control

func _ready():

func read_config():
	var config = ConfigFile.new()
	var data = config.load("user://Config.cfg")
	var value = config.get_value("section","value")
	$Label.text = str(value)

func _on_Button_pressed():
	var config = ConfigFile.new()
	var data = config.load("user://Config.cfg")
	var count = config.get_value("section","value")
	count += 1
	config.set_value("section", "value", count)

unholy182000 | 2021-01-14 20:26

the code in the uploaded file.
Scene structure is very simple Control node has 2 childeren label and button when you press button label value increments by 1

unholy182000 | 2021-01-14 20:27

It works on godot play project, windows export and html5 export but not on android

unholy182000 | 2021-01-14 20:29

.cfg file is very simple also Config.cfg in user directory with
value = 1

unholy182000 | 2021-01-14 20:30

:bust_in_silhouette: Reply From: deaton64


Just from the code you uploaded, it fails on my Windows PC.

I think it’s because your code is reading the config file before it’s created and it doesn’t exist.
I suspect the file exists on your PC, but it won’t on the Android device.

Quick fix:

func _ready():
	var config = ConfigFile.new()
	config.set_value("section", "value", 0)

Obviously that will set the value to 0 when it’s first run.

ı changeed my code from

 var value = config.get_value("section","value")


 var value = config.get_value("section","value",0)

and it works now.

What you said about it uses it before creating it opened my eyes. thank you very much

unholy182000 | 2021-01-15 18:39