Forum rules - please read before posting.

Custom Xbox loading failing because it takes time.

edited September 2024 in Technical Q&A

Hello. I created a custom iSaveFileHandler for Xbox, and I think everything is working except for Loading. Xbox's Load method takes some time, so when I run an ActionList with Load, it seems that there is not enough time for Xbox's Load to complete and thus the Load string is returned as null. I'm using AC version 1.74.2 and Unity 2022.3.39f1. Is there an easy way to correctly implement the loading without updating AC? I've written some overrides and updating it might be very complicated. This is the snippet of my iSaveHandler. I redacted some lines, but the main idea is there.

public string Load(SaveFile saveFile, bool doLog)
{
    string loadedData = null;
    string containerName = saveFile.fileName;

    XboxServices.savesManager.GetOrCreateContainer(containerName, =>
    {
        if (succeeded)
        {
            XboxServices.savesManager.LoadGame((blobs) =>
            {
                if (failed)
                {
                    Debug.LogError("Error when loading GameSave.");
                    return;
                }
                if (succeeded && blobs.Length > 0)
                {
                    Debug.Log("loading data succeeded");
                    loadedData = System.Text.Encoding.ASCII.GetString(blobs[0].Data);
                }
            });
        }
    });
    Debug.Log($"loadedData: {loadedData}");
    return loadedData;
}

So loadedData is returned as empty before it gets its value from loadedData = System.Text.Encoding.ASCII.GetString(blobs[0].Data);. Any help is appreciated.

Comments

  • What's the output to the Console in this situation?

    I wouldn't have expected the function to just continue on if the LoadGame function was taking too long, but AC now relies on a callback system to allow such operations to take as long as necessary. This was introduced in v1.76.1.

    I'd recommend trying out the latest release in a fresh project and see if the change causes the loading process to then work correctly. We can see about what's necessary for a potential upgrade later.

  • The output of my Debug.Log is "loadedData: ". And it appears before "loading data succeeded". I tried creating another string to debug the loaded data and it seems that it is loading correctly. Its values just doesn't get returned to loadedData.

    I'll try the new version of AC and let you know.

  • Hi. So I downloaded the newest AC version to a new project and looked through the scripts.

    Like I said before, updating AC to the newest version will surely break my entire game, so I did the following:

    1. I replaced iSaveFileHandler with the new version. This made some errors appear.
    2. I replaced the PlayerPrefs and SystemFile SaveFileHandlers to get them to use the updated interface.
    3. I got errors in SaveFileManager, SaveOperation, and SaveSystem. So I went there and updated the calls to saveFileHandler with the new versions using callbacks. I copied the new methods that were added for callbacks.
    4. I modified my SaveFileHandler_Xbox to use the callbacks.
    5. If at any point I had errors that related to some other part of AC being updated, I left that as the old version, but making sure that the order of methods for the callbacks were the same.

    In the end, my new Load method looks like this:

    public void Load(SaveFile saveFile, bool doLog, System.Action<SaveFile, string> callback)
        {
            string loadedData = string.Empty;
            string containerName = saveFile.fileName;
            XboxServices.savesManager.GetOrCreateContainer(containerName, (hresult) =>
            {
                if (failed)
                {
                    Debug.Log($"Failed to get or create container {containerName}");
                    return;
                }
                if (suceeded)
                {
                    XboxServices.savesManager.LoadGame((blobs) =>
                    {
                        if (failed)
                        {
                            Debug.LogError("Error when loading GameSave");
                            return;
                        }
    
                        if (succeeded && blobs.Length > 0)
                        {
                            Debug.Log("Loading data succeeded");
                            loadedData = System.Text.Encoding.UTF8.GetString(blobs[0].Data);
                            callback.Invoke(saveFile, loadedData);
                        }
                    });
                }
            });
        }
    

    I also made sure to update the Delete and Save methods of my SaveFileHandler to include the callbacks (I took a look at the other SaveFileHandlers to see how it's done)

    Does this sound correct? Are there any other scripts I should modify? I can provide more details if you would like to know how exactly I edited the scripts. So far, my tests have worked correctly.

  • I couldn't give a definitive answer, as it would depend on the exact changes you made to v1.74.2. Rather than copying individual methods from SaveSystem, however, I'd say it would be safer to import the one from the latest release completely, and fix any issues you then get.

    The callback implementation is correct, however.

  • I tried replacing the entire script and fixing the errors. But so many errors came up that I feel like I'm breaking more than I'm fixing. I will send you the script in a private message, if you would like to take a look at the changes I made.

  • Too much has changed between versions. Looks like the best approach - if updating fully is not possible - is to go back to your earlier attempt, and test thoroughly.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.