Need Assist on Saving Data to Save File

Discuss modding questions and implementation details.
User avatar
TheLacus
Posts: 1305
Joined: Wed Sep 14, 2016 6:22 pm

Re: Need Assist on Saving Data to Save File

Post by TheLacus »

RestoreSaveData is the method you already wrote, you only need to change the string field to a dictionary. This is from one of your posts:

Code: Select all

public void RestoreSaveData(object saveData)
{
    var myModSaveData = (MyModSaveData)saveData;
    text = myModSaveData.Text;
}
What i was suggesting with my previous message is that the content you get when a save is loaded is what you gave when the save was created. The dictionary is stored to json file and then parsed back, no additional changes are made. ;)

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by l3lessed »

I finally figured out the issue with loading the items in the saved dictionaries.

Anytime the interface restores my previous dictionary, that contains the list of items used for the outfits, it creates new items from the template and this gives them new UID numbers; I thought it would store the UID for the item too, but it does not seem to, just template data. As a result, when my code tries to grab the items to equip, it can't find them because it is trying expecting the old UID for the items.

I can confirm this, because the UID for the items changes in my debug log between saves. How can I ensure the item is saved, including its UID, not just its template? Or, is there a way to equip items based solely off something else that isn't a UID that's in the saved template.


Found it. It's this correct?
SerializeEquipTable
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.

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by l3lessed »

So, this is getting interesting.

I have to save two separate dictionaries using the same index. One needs to store the serialized item table data including UID. The other needs to store the equip table that matches that serialized table.

Both dictionaries will be used to setup a new equip table and then relink the UIDs to the items. Lets see how this goes.

Still frustrated all this hassle over a basic oversight like saving UIDs. Just assumed they were part of the daggerfall unity item class when you saved it that way.
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.

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by l3lessed »

W00t W00t. Man, did I spend days over complicating this and making it into something way more than it was; I always tend to do this. The OOP language was simple, but I was making it crazy. Thank you so much for all the assistance everyone.

The whole issue with loading saved outfits was connected to not using and calling the UIDs. I didn't realize those needed to be saved using item collections and serialized equip tables. They are not connected to the default DaggerfallUnityItem Class. :oops: Seems so apparent now, but you know hindsight.

Long story short, mod now saves the UID in dictionary entries. On recall, it removes all items, updates armor to 0, calls DeserializeEquipTable (which checks players inventory for item UIDs), equips items from inventory that match UID, updates armor values, and resets inventory UI to show new values and outfit.

One last question, I'm still somewhat confused about how to get the save data to initialize with the very first game load. It will load just fine after the very first initiated save. This tells me, I'm not initiating my values correctly on the script/mod load; It's creating the default values, but not loading them up until a second load it done. Dig little more into this, but finally outfits have permanency between saves, so this mod can actually be useful.

Almost there. Trying to release this before Wed, as I'm going on a 2 week vacation at that point.
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.

User avatar
Hazelnut
Posts: 3016
Joined: Sat Aug 26, 2017 2:46 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by Hazelnut »

How are you planning to handle multiple item references? For instance if a player sells or drops an item that's used in one of the sets, your dictionaries will still have a reference to the item.
See my mod code for examples of how to change various aspects of DFU: https://github.com/ajrb/dfunity-mods

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by l3lessed »

It's not an issue, as I can see. First, the items are stored in a list. So each outfit is a list of the item UID's, that is than stored in a dictionary. It already handles multiple items this way.

Than on retrieval, it grabs the list of uid's, uses a deserializer equip method to find the items, and then equip them. If the player sells or drops the items, it just ignores the missing references. Their is a built in method to do all this already. If there wasn't, their are a number of easy solutions, the first being using LINQ to clear out null references from the list, leaving only items the player has. Or, you could build in a != null if then check to ignore missing items.
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.

l3lessed
Posts: 1403
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by l3lessed »

And first release of the mod is finished.

Things other's who stumble on this thread should know.

1. To ensure your data restores correctly, do not initiate the save interface anytime after StateTypes.Game. If you do this, your data won't restore on first load, as the vars have not been assigned to memory yet to store them on load.

2. To ensure items are restored correctly, you will always have to refer to their UID's. These are store in ULONG Vars, and can be grabbed individually or in an array through the SerializedEquipTable method.

I wasted the last 4 days tracking down 2 of the simplest bugs in the whole process. I was merely using the wrong item UID referencing with using equip tables, and I wasn't restoring data correctly because I had the wrong StateType initiation for the mod. Learned some lessons the hard way :lol: .

This will make future mod development much easier though, now I have a better grasp on how OOP language is used in unity and in DFU.

I'll drop Outfit Manager Beta on nexus and in the forum tonight.

:D :D :D
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.

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Need Assist on Saving Data to Save File

Post by Ralzar »

l3lessed wrote: Tue Dec 24, 2019 8:33 am And first release of the mod is finished.

Things other's who stumble on this thread should know.

1. To ensure your data restores correctly, do not initiate the save interface anytime after StateTypes.Game. If you do this, your data won't restore on first load, as the vars have not been assigned to memory yet to store them on load.

2. To ensure items are restored correctly, you will always have to refer to their UID's. These are store in ULONG Vars, and can be grabbed individually or in an array through the SerializedEquipTable method.

I wasted the last 4 days tracking down 2 of the simplest bugs in the whole process. I was merely using the wrong item UID referencing with using equip tables, and I wasn't restoring data correctly because I had the wrong StateType initiation for the mod. Learned some lessons the hard way :lol: .

This will make future mod development much easier though, now I have a better grasp on how OOP language is used in unity and in DFU.

I'll drop Outfit Manager Beta on nexus and in the forum tonight.

:D :D :D

I just discovered that Climates & Cloaks is not behaving correctly because I've not used the savedata functions. If you fell in water, and load, you are still wet :D

So I've been trying to figure out how to implement savedata. It looks mostly understandable, but at the moment I'm stuck on:

Code: Select all

        [Invoke(StateManager.StateTypes.Start, 0)]
        public static void Init(InitParams initParams)
        {

            GameObject go = new GameObject("ClimateCloaks");
            instance = go.AddComponent<ClimateCloaks>();


            mod = initParams.Mod;
            mod.SaveDataInterface = instance;
            mod.LoadSettingsCallback = (settings, _) => settings.Deserialize("Options", ref instance);
            mod.LoadSettings();


            EntityEffectBroker.OnNewMagicRound += TemperatureEffects_OnNewMagicRound;

        }
"instance" is not declared, so obviously gets marked as having an error. Is there some directory I should be using that I am not? I've looked over the examples in the thread, but can't find the code I'm missing.

User avatar
TheLacus
Posts: 1305
Joined: Wed Sep 14, 2016 6:22 pm

Re: Need Assist on Saving Data to Save File

Post by TheLacus »

You need to implement IHasModSaveData and create an instance.

User avatar
Ralzar
Posts: 2211
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Need Assist on Saving Data to Save File

Post by Ralzar »

Figured it out :)

Post Reply