ConfigFile save conflict problems

Godot Version

Godot 3.5

Question

I am trying to implement a way so that my game remembers my highscore and remembers if my popup has been shown already on the first time. You see the problem is that the highscore is the first one to save and when it saves (it saves everytime I open my game) it also deletes the other contents in the configfile and only saves itself. To help show this better here is my code:

Highscore script:

extends Label

func _ready():
highscore_load()
highscore_update()
highscore_save()

func highscore_update():
if Game.points > Game.highscore:
Game.highscore = Game.points
set_text(str(Game.highscore))

func highscore_load():
var con = ConfigFile.new()
var path = “user://Player”

if con.load(path) == OK:
	Game.highscore = con.get_value("game", "highscore", 0)

func highscore_save():
var con = ConfigFile.new()
var path = “user://Player”

con.set_section("game", true)
con.set_value("game", "highscore", Game.highscore)
con.save(path)

Popup Code (important parts):

var player_data = ConfigFile.new()
var path = “user://Player”

func _ready():
if should_show_popup():
popup()

func should_show_popup():

var load_result = player_data.load(path)
print("Load Result: ", load_result)

if player_data.load(path) == OK:
	var popup_shown_exists = player_data.has_section_key("popup", "popup_shown")
	var popup_shown = player_data.get_value("popup", "popup_shown", "false")
	
	if popup_shown_exists:
		print("Popup shown: ", popup_shown)
		# If the popup_shown key exists, return its value
		return !popup_shown
	else:
		# If the popup_shown key doesn't exist, return true to show the Popup
		return true
else:
	# If the file doesn't exist, return true to show the Popup
	return true

Function to mark the Popup as shown

func mark_popup_as_shown():

if player_data.load(path) == OK:
	player_data.set_value("popup", "popup_shown", "true")
	player_data.save(path)
	
if player_data.save(path) == OK:
	print("Sucessfully seen popup")
else:
	print("Unsucessful save")


func _on_Continue_pressed():
mark_popup_as_shown()

Note that the Continue pressed function comes from a button. Please help me.

I have found how to solve this: in the highscore_save function remove the set section and replace it with (if con.load(path) == OK:). this makes the function look like this:
func highscore_save():
var con = ConfigFile.new()
var path = “user://Player”

if con.load(path) == OK:
	con.set_value("game", "highscore", Game.highscore)
	con.save(path)

The logic behind this is that it first needs to load the config file from the previous session to see if there is already an existing config file. before this edit it instead just made a new config file only remembering the highscore variable.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.