Saved Data Not Restoring When Loading First Time

Discuss modding questions and implementation details.
Post Reply
l3lessed
Posts: 1400
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Saved Data Not Restoring When Loading First Time

Post by l3lessed »

I think I am down to the last thing I need to remedy now for dropping a hopefully bug free updated version.

When I am loading a game for the first time from the start menu, the saved data is not restored. Instead, when I load the same save a second time after the first load, the save data loads just fine.

I checked Ralzars Climates mod to see if I am missing something, but I don't see it.

Heres the relevant code from my mod

Code: Select all

#region saveData
    [FullSerializer.fsObject("v1")]
    public class MyModSaveData
    {
        public Dictionary<Minimap.MarkerGroups, Color> IconGroupColors = new Dictionary<Minimap.MarkerGroups, Color>();
        public Dictionary<Minimap.MarkerGroups, float> IconGroupTransperency = new Dictionary<Minimap.MarkerGroups, float>();
        public Dictionary<Minimap.MarkerGroups, bool> IconGroupActive = new Dictionary<Minimap.MarkerGroups, bool>();
        public Dictionary<Minimap.MarkerGroups, bool> NpcFlatActive = new Dictionary<Minimap.MarkerGroups, bool>();
        public Dictionary<Minimap.MarkerGroups, float> IconSizes = new Dictionary<Minimap.MarkerGroups, float>();
        public DaggerfallUnityItem EnchantedCompass = new DaggerfallUnityItem();
        public float MinimapSizeMult = 400;
        public float OutsideViewSize = 100;
        public float InsideViewSize = 20;
        public float MinimapRotationValue = 0;
        public float MinimapCameraHeight;
        public float AlphaValue = 1f;
        public float IconSize = 1f;
        public float MinimapSensingRadius = 40f;
        public bool LabelIndicatorActive = true;
        public bool SmartViewActive = true;
        public bool IconsIndicatorActive = true;
        public bool RealDetectionEnabled = true;
        public bool CameraDetectionEnabled = false;
        public bool DoorIndicatorActive = true;
        public bool QuestIndicatorActive = false;
        public bool GeneratedStartingEquipment = false;
    }
    #endregion
        
    public class Minimap : MonoBehaviour, IHasModSaveData
    {        //classes for setup and use.
        private static Mod mod;
        public Type SaveDataType { get { return typeof(MyModSaveData); } }

        #region SaveData
        public object NewSaveData()
        {
            return new MyModSaveData
            {
                IconGroupColors = new Dictionary<MarkerGroups, Color>(),
                IconGroupTransperency = new Dictionary<MarkerGroups, float>(),
                IconGroupActive = new Dictionary<MarkerGroups, bool>(),
                NpcFlatActive = new Dictionary<MarkerGroups, bool>(),
                IconSizes = new Dictionary<MarkerGroups, float>(),
                IconSize = 1f,
                MinimapSizeMult = .25f,
                OutsideViewSize = 100f,
                InsideViewSize = 20f,
                MinimapCameraHeight = 100,
                MinimapRotationValue = 0,
                AlphaValue = 0,
                MinimapSensingRadius = 35f,
                LabelIndicatorActive = true,
                SmartViewActive = true,
                IconsIndicatorActive = true,
                RealDetectionEnabled = true,
                CameraDetectionEnabled = true,
                DoorIndicatorActive = true,
                QuestIndicatorActive = true,
                GeneratedStartingEquipment = false,
            };
        }

        public object GetSaveData()
        {
            return new MyModSaveData
            {
                IconGroupColors = iconGroupColors,
                IconGroupTransperency = iconGroupTransperency,
                IconGroupActive = iconGroupActive,
                NpcFlatActive = npcFlatActive,
                IconSizes = iconSizes,
                IconSize = minimapControls.iconSize,
                MinimapSizeMult = minimapSize,
                OutsideViewSize = outsideViewSize,
                InsideViewSize = insideViewSize,
                MinimapCameraHeight = minimapCameraHeight,
                MinimapRotationValue = minimapControls.minimapRotationValue,
                AlphaValue = minimapControls.blendValue,
                MinimapSensingRadius = minimapSensingRadius,
                LabelIndicatorActive = minimapControls.labelsActive,
                SmartViewActive = minimapControls.smartViewActive,
                IconsIndicatorActive = minimapControls.iconsActive,
                RealDetectionEnabled = minimapControls.realDetectionEnabled,
                CameraDetectionEnabled = minimapControls.cameraDetectionEnabled,
                DoorIndicatorActive = minimapControls.doorIndicatorActive,
                QuestIndicatorActive = minimapControls.questIndicatorActive,
                GeneratedStartingEquipment = generatedStartingEquipment,
            };
        }

        public void RestoreSaveData(object saveData)
        {
            Debug.Log("LOADING SAVE DATA");
            var myModSaveData = (MyModSaveData)saveData;

            //load all the previous saves dictionaries.
            iconGroupColors = myModSaveData.IconGroupColors;
            iconGroupTransperency = myModSaveData.IconGroupTransperency;
            iconGroupActive = myModSaveData.IconGroupActive;
            npcFlatActive = myModSaveData.NpcFlatActive;
            iconSizes = myModSaveData.IconSizes;

            //ERROR CHECKER: This runs through the enum checking each marker type with the below dictionaries.
            //If one of the dictionaries below doesn't have matching marker group enums saved, it will reload the default dictionary values to stop crashes
            //This enables backwards compatibility for previous saves.
            foreach (MarkerGroups marker in (MarkerGroups[])Enum.GetValues(typeof(MarkerGroups)))
            {
                if (!iconSizes.ContainsKey(marker))
                {
                    iconSizes = new Dictionary<MarkerGroups, float>()
                    {
                        {MarkerGroups.Shops, 1f},
                        {MarkerGroups.Blacksmiths, 1f},
                        {MarkerGroups.Houses, 1f},
                        {MarkerGroups.Taverns, 1f},
                        {MarkerGroups.Utilities, 1f},
                        {MarkerGroups.Government, 1f},
                        {MarkerGroups.Friendlies, 1f},
                        {MarkerGroups.Enemies, 1f},
                        {MarkerGroups.Resident, 1f},
                        {MarkerGroups.None, 1f}
                    };
                }

                if (!npcFlatActive.ContainsKey(marker))
                {
                    npcFlatActive = new Dictionary<MarkerGroups, bool>()
                    {
                        {MarkerGroups.Shops, false },
                        {MarkerGroups.Blacksmiths, false },
                        {MarkerGroups.Houses, false },
                        {MarkerGroups.Taverns, false },
                        {MarkerGroups.Utilities, false },
                        {MarkerGroups.Government, false },
                        {MarkerGroups.Friendlies, false },
                        {MarkerGroups.Enemies, false },
                        {MarkerGroups.Resident, false },
                        {MarkerGroups.None, false}
                    };
                }

                if (!iconGroupActive.ContainsKey(marker))
                {
                    iconGroupActive = new Dictionary<MarkerGroups, bool>()
                    {
                        {MarkerGroups.Shops, true },
                        {MarkerGroups.Blacksmiths, true },
                        {MarkerGroups.Houses, true },
                        {MarkerGroups.Taverns, true },
                        {MarkerGroups.Utilities, true },
                        {MarkerGroups.Government, true },
                        {MarkerGroups.Friendlies, true },
                        {MarkerGroups.Enemies, true },
                        {MarkerGroups.Resident, true },
                        {MarkerGroups.None, false}
                    };
                }

                if (!iconGroupTransperency.ContainsKey(marker))
                {
                    iconGroupTransperency = new Dictionary<MarkerGroups, float>()
                    {
                        {MarkerGroups.Shops, 1 },
                        {MarkerGroups.Blacksmiths, 1 },
                        {MarkerGroups.Houses, 1 },
                        {MarkerGroups.Taverns, 1 },
                        {MarkerGroups.Utilities, 1 },
                        {MarkerGroups.Government, 1 },
                        {MarkerGroups.Friendlies, 1 },
                        {MarkerGroups.Enemies, 1 },
                        {MarkerGroups.Resident, 1 },
                        {MarkerGroups.None, 0 }
                    };
                }

                if (!iconGroupColors.ContainsKey(marker))
                {
                    iconGroupColors = new Dictionary<MarkerGroups, Color>()
                    {
                        {MarkerGroups.Shops, new Color(1,.25f,0,1) },
                        {MarkerGroups.Blacksmiths, new Color(0,1,1,1) },
                        {MarkerGroups.Houses, new Color(.285f,.21f,.075f,1) },
                        {MarkerGroups.Taverns, new Color(0,1,0,1) },
                        {MarkerGroups.Utilities, new Color(1,1,0,1) },
                        {MarkerGroups.Government, new Color(1,0,0,1) },
                        {MarkerGroups.Friendlies, Color.green },
                        {MarkerGroups.Enemies, Color.red },
                        {MarkerGroups.Resident, Color.yellow },
                        {MarkerGroups.None, Color.black }
                    };
                }
            }

            //continue loading other default values. not dependent on a marker type, so no need to check for marker group recall errors.
            minimapSize = myModSaveData.MinimapSizeMult;
            outsideViewSize = myModSaveData.OutsideViewSize;
            insideViewSize = myModSaveData.InsideViewSize;
            minimapCameraHeight = myModSaveData.MinimapCameraHeight;
            minimapControls.minimapRotationValue = myModSaveData.MinimapRotationValue;
            minimapControls.blendValue = myModSaveData.AlphaValue;
            minimapSensingRadius = myModSaveData.MinimapSensingRadius;
            minimapControls.labelsActive = myModSaveData.LabelIndicatorActive;
            minimapControls.smartViewActive = myModSaveData.SmartViewActive;
            minimapControls.iconsActive = myModSaveData.IconsIndicatorActive;
            minimapControls.realDetectionEnabled = myModSaveData.RealDetectionEnabled;
            minimapControls.cameraDetectionEnabled = myModSaveData.CameraDetectionEnabled;
            minimapControls.iconSize = myModSaveData.IconSize;
            minimapControls.doorIndicatorActive = myModSaveData.DoorIndicatorActive;
            minimapControls.questIndicatorActive = myModSaveData.QuestIndicatorActive;
            generatedStartingEquipment = myModSaveData.GeneratedStartingEquipment;
        }
        #endregion

        //starts mod manager on game begin. Grabs mod initializing paramaters.
        //ensures SateTypes is set to .Start for proper save data restore values.
        [Invoke(StateManager.StateTypes.Game, 0)]
        public static void Init(InitParams initParams)
        {
            //sets up instance of class/script/mod.
            minimapObject = new GameObject("Minimap Mod");
            MinimapInstance = minimapObject.AddComponent<Minimap>();

            GameObject MinimapControlsObject = new GameObject("Minimap Controls");
            MinimapControlsObject.transform.SetParent(minimapObject.transform);
            minimapControls = MinimapControlsObject.AddComponent<MinimapGUI>();

            MinimapEffectsObject = new GameObject("Minimap Effects Manager");
            MinimapEffectsObject.transform.SetParent(minimapObject.transform);
            minimapEffects = MinimapEffectsObject.AddComponent<EffectManager>();

            MinimapNpcManager = new GameObject("Minimap NPC Manager");
            MinimapNpcManager.transform.SetParent(minimapObject.transform);
            minimapNpcManager = MinimapNpcManager.AddComponent<NPCManager>();

            MinimapInputManagerObject = new GameObject("Minimap Input Manager");
            MinimapInputManagerObject.transform.SetParent(minimapObject.transform);
            MinimapInputManager = MinimapInputManagerObject.AddComponent<SmartKeyManager>();

            //initiates mod paramaters for class/script.
            mod = initParams.Mod;
            //initates mod settings
            settings = mod.GetSettings();

            mod.SaveDataInterface = MinimapInstance;           
            //after finishing, set the mod's IsReady flag to true.
            mod.IsReady = true;
            Debug.Log("Minimap MOD STARTED!");
        }
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

Post Reply