0 votes

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?

Godot version 3.2.3
in Engine by (45 points)

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

https://drive.google.com/file/d/1BZE64to5beHYd6rSgjPOuDft3vQSJGeP/view?usp=sharing

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

extends Control

func _ready():
    read_config()


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)
    config.save("user://Config.cfg")
    read_config()

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

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

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

1 Answer

0 votes
Best answer

Hi,

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)
    config.save("user://Config.cfg")
    read_config()

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

by (1,921 points)
selected by

ı changeed my code from

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

to

 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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.