(C#) High score script failing to write values to file

Godot Version

4.1

Question

My script for setting and getting high score values does not appear to be saving the file, what am I doing wrong, and how do I fix it?

using Godot;
using System;

public partial class HighScoreManager : Node
{
	private ConfigFile config = new ConfigFile();
    private string configPath = "user://highscore.cfg";

    public void UpdateHighScore(int playerScore, string playerName)
    {
        //Get current high score
        int currentHighScore = (int)config.GetValue("HighScore", "Score");
        if (playerScore > currentHighScore) //Compare new score to current score
        {
            //Set high score
            config.SetValue("HighScore", "Score", playerScore);
            config.SetValue("HighScore", "PlayerName", playerName);
            config.Save(configPath);
            GD.Print("Saved High Score: " + playerScore + " by " + playerName);
        }
    }

    // Getter method to retrieve high score
    public int GetHighScore()
    {
        return (int)config.GetValue("HighScore", "Score");
    }

    // Getter method to retrieve player name
    public string GetHighScorePlayerName()
    {
        return (string)config.GetValue("HighScore", "PlayerName");
    }



    

    public void UpdateHellHighScore(int playerScore, string playerName)
    {
        //Get current high score
        int currentHighScore = (int)config.GetValue("HighScore", "Score");
        if (playerScore > currentHighScore) //Compare new score to current score
        {
            //Set high score
            config.SetValue("HellHighScore", "Score", playerScore);
            config.SetValue("HellHighScore", "PlayerName", playerName);
            config.Save(configPath);
            GD.Print("Saved High Score: " + playerScore + " by " + playerName);
        }
    }

    // Getter method to retrieve hell mode high score
    public int GetHellHighScore()
    {
        return (int)config.GetValue("HellHighScore", "Score");  
    }

    // Getter method to retrieve hell mode player name
    public string GetHellHighScorePlayerName()
    {
        return (string)config.GetValue("HellHighScore", "PlayerName");
    }
}

Where is your config.Load(configPath); ? You are saving data to a file but never load it back.

That was my issue, sorry, I guess I just didn’t notice that in the documentation somehow.

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