Forum rules - please read before posting.

Can't save custom data in a persistent object?

Hello everyone. So I'm making the achievements system for my game. For that I have a persistent object with DontDestroyOnLoad() that has a list of an achievement class with name, id, and current Progress. I only want to save the current progress. For that I made this custom remember script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;

[AddComponentMenu ("Adventure Creator/Save system/Remember Achievements")]
[RequireComponent (typeof (Achievements))]
public class RememberAchievements : Remember
{
    public override string SaveData()
    {
        AchievementsData data = new AchievementsData();
        data.objectID = constantID;
        data.savePrevented = savePrevented;
        Achievements myAchievements = GetComponent<Achievements>();
        for (int i = 0; i < myAchievements.achievements.Count; i++){
            data.currentProgresses.Add(myAchievements.achievements[i].currentProgress);
        }

        return Serializer.SaveScriptData<AchievementsData>(data);
    }

    public override void LoadData(string stringData)
    {
       AchievementsData data = Serializer.LoadScriptData<AchievementsData> (stringData);
       if (data == null) return;
        SavePrevented = data.savePrevented; if (savePrevented) return;
        Achievements myAchievements = GetComponent<Achievements>();
        for (int i = 0; i < myAchievements.achievements.Count; i++){
            myAchievements.achievements[i].currentProgress = data.currentProgresses[i];
        }
    }


}

[System.Serializable]
public class AchievementsData : RememberData
{
    public List<uint> currentProgresses;
    public AchievementsData () { }

}

I play through my game and have some achievements change their progress and save the game. But when I load the game, the achievement progress isn't restored. Is it related to the object being persistent? Or is there something wrong with my script? The object is first located in my main menu, before the save file is loaded. I'm using AC v 1.74.2 and Unity 2022.3.39f1.

Comments

  • Test it in a non-persistent object - I suspet Unity may have an issue with the serialization of a uint List.

    The way AC saves such data is to compact them into a string, separated by a special character (e.g. "4;1;5;1;4"), and then use string.Split in the LoadData function to separate them again.

    See the RememberHotspot script's buttonStates as an example.

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.