|
|
|
|
Reply From: |
Tybobobo |
Thank you all for the help! From the answers posted here I managed to create a saving and loading script using ConfigFiles. I usually create my own tutorials in a private site, but I am going to share what I wrote here - so that others may have use of it
Documentation:
Saving a File
# Path to save file
var file_to_save= "res://configuration.cfg"
func _ready():
# Initiate ConfigFile
var configFile = ConfigFile.new()
# Add values to file
configFile.set_value("Config","hostname","Server name")
configFile.set_value("Config","maxClients",32)
configFile.set_value("Options","difficulty","hard")
# Save file
configFile.save(file_to_save)
Saved Output
[Config]
hostname=“Server name”
maxClients=32
[Options]
difficulty=“hard”
Loading a File
var file_to_load= "res://configuration.cfg"
func _ready():
# Initiate ConfigFile
var configFile= ConfigFile.new()
# Load file
configFile.load(file_to_load)
# ----------
# METHOD-1:
# Check if "hostname" exists in Config section
if (configFile.has_section_key("Config", "hostname")):
# Get hostname value and print it
var hostName = configFile.get_value("Config", "hostname")
print("Hostname: ", hostName)
# ----------
# METHOD-2:
# Assign value without checking if it exists
var maxClients = configFile.get_value("Config", "maxclients")
# If value is not null; print out
if(maxClients != null):
print("Max Clients: ", maxClients)
# ----------
# Check if [Options] section exist
if (configFile.has_section("Options")):
print ("Options found!")
Output
Hostname: Server name
Max Clients: 32
Options found!
Personal Notes
It was recommended that I use user:// in path… which apparently is supposed to save the file in my ~/godot directory. I was unable to find any file there, and I am wondering it is because I am using Windows 7.
However I believe using res:// is a better option, as I would want the config file to be in the root directory of the game I am working on, so that people may easily change values if needed (such as server configurations). Tips & Tricks are welcome!
The file will be packed with all the resources to the .exe/linux file when you compile your project, so there will be no way to edit it afterwards if you use “res://”
edit: Tried this with early builds of godot, don’t know if this behavior changed.
brunosxs | 2016-02-26 14:36