Created
May 7, 2018 09:00
-
-
Save juanpaexpedite/1e9f0e5a293e6bc235cfbfe235856143 to your computer and use it in GitHub Desktop.
GodotEngine 3.0 Settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
enum {NON_EXISTING, LOAD_SUCCESS, LOAD_ERROR_COULDNT_OPEN} | |
const SAVE_PATH = "user://config.cfg" | |
var _config_file = ConfigFile.new() | |
func _ready(): | |
var result = load_settings() | |
if result == NON_EXISTING: | |
SetDefault() | |
pass | |
func SetDefault(): | |
_config_file.set_value("Settings","music_off",false) | |
_config_file.set_value("Settings","sounds_off",false) | |
save_settings() | |
pass | |
func load_settings(): | |
var file = File.new() | |
if not file.file_exists(SAVE_PATH): | |
return NON_EXISTING | |
var error = _config_file.load(SAVE_PATH) | |
if error != OK: | |
print("Error loading the settings. Error code: %s" % error) | |
return LOAD_ERROR_COULDNT_OPEN | |
for section in _config_file.get_sections(): | |
for key in _config_file.get_section_keys(section): | |
var val = _config_file.get_value(section,key) | |
print("%s: %s" % [key, val]) | |
return LOAD_SUCCESS | |
func save_settings(): | |
_config_file.save(SAVE_PATH) | |
func Set(key,value): | |
_config_file.set_value("Settings",key,value) | |
save_settings() | |
pass | |
func Get(key): | |
return _config_file.get_value("Settings",key) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because the API is evolving, here I post a basic working class