There are several ways to handle it, but the easiest is probably to use a ConfigFile, as it has a bunch of helper functions that make things easier. Here's a simple example script that can save and load both a highscore and best time:
extends Node
var config_file
const CONFIG_FILEPATH = "user://highscores.cfg"
func _ready():
config_file = ConfigFile.new()
func save_player_data(new_score, new_time):
# Open the config file
config_file.load(CONFIG_FILEPATH)
# Get the current highscore, if it exists
var highscore = config_file.get_value("player_data", "highscore", -INF)
# see if the new score is better and if it is, save it!
if new_score >= highscore:
config_file.set_value("player_data", "highscore", new_score)
# Do the same for time
var best_time = config_file.get_value("player_data", "best_time", -INF)
if (new_time >= best_time):
config_file.set_value("player_data", "best_time", new_time)
# Save the config file
config_file.save(CONFIG_FILEPATH)
func load_player_data():
config_file.load(CONFIG_FILEPATH)
# optional: return the highscore and best time in a list
var highscore = config_file.get_value("player_data", "highscore", -INF)
var best_time = config_file.get_value("player_data", "best_time", -INF)
return [highscore, best_time]
# Then you can use the function like this:
# data = load_player_data()
# print ("highscore: ", data[0], " | best time: ", data[1])