Further Errors To Be Rectified

I noted two more errors to rectify, which might be reflected in the course later on, but as for now the first is one is to check for any validations in the file name the user can specify:

public void NewGame()
{
    string userInput = textField.text;
    
    if (!string.IsNullOrEmpty(userInput))
    {
        userInput = System.Text.RegularExpressions.Regex.Replace(userInput, "[^a-zA-Z0-9_-]", "_");
        savingWrapper.value.NewGame(userInput);
    }           
}

This line of code uses a regular expression to replace any character in the text field that is not a lowercase letter either uppercase or lowercase, a digit from zero to nine, an underscore, or a hyphen, with an underscore.
The angular hat at the beginning of the characters inside the square brackets is a negation symbol, so any characters that do not match the one we have specified, will simply be replaced with an underscore.
This is good practice to restrict certain characters in file names to ensure compatibility across different operating systems

Another is in the SavingSystem.cs, where if in the worst case we have no value for the lastSceneBuildIndex, we have previously simply loaded the current active scene with SceneManager.GetActiveScene().buildIndex, which would reload the main menu scene.
To prevent this, you can define a default scene yourself:

[SerializeField] int defaultBuildIndex = 1;

public IEnumerator LoadLastScene(string saveFile)
{
    Dictionary<string, object> state = LoadFile(saveFile);

    int buildIndex = defaultBuildIndex;

    if (state.ContainsKey("lastSceneBuildIndex")) 
    {
        buildIndex = (int)state["lastSceneBuildIndex"];
    }
    // ...
}

In retrospect, I noticed yet another issue, which is that we don’t check if the save file name already exists. We’re not going to have two separate files, we’re going to work with the one that already exists. So we would be loading a new instance of the game, but when you press the “L” key or whatever you have specified to restore the state of your game you have captured, you restore the state that was stored in that same previous file.
For this you need to watch the lecture “List Files In A Directory”, then wrap the NewGame() method in a method that returns a Boolean value and simply return false if the file already exists. With the output we get from the NewGame() method, we can provide a user feedback as follows:

// SavingWrapper.cs
 public bool NewGame(string saveFileName)
 {
     foreach (string fileName in savingSystem.ListSaveFiles())
     {
         if (saveFileName.Equals(fileName)) return false;
     }

     SetCurrentSave(saveFileName);
     StartCoroutine(LoadFirstScene());

     return true;
 }

// MainMenuUI.cs

[SerializeField] TextMeshProUGUI errorText;

public void NewGame()
{
    string userInput = textField.text;
    
    if (!string.IsNullOrEmpty(userInput))
    {
        userInput = System.Text.RegularExpressions.Regex.Replace(userInput, "[^a-zA-Z0-9_-]", "_");
        if (!savingWrapper.value.NewGame(userInput))
        {
            // Give feedback
            errorText.text = "File already exists...";
        }
    }           
}

Also be sure to limit the number of characters the user can specify for the name of their save files. Below 31 is recommended and there is a thread in the same lecture that covers this:

Privacy & Terms