Need Assist on Saving Data to Save File

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

Re: Need Assist on Saving Data to Save File

Post by TheLacus » Sat Dec 21, 2019 10:17 pm

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. ;)
Mod System documentation - Learn how to create mods for Daggerfall Unity.
Modder Discussion - General help and discussion for the mod system.
Github Issues - Submit a bug report for the game, including the mod system.

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

Re: Need Assist on Saving Data to Save File

Post by l3lessed » Mon Dec 23, 2019 3:46 am

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 Skyrim Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

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

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

Re: Need Assist on Saving Data to Save File

Post by l3lessed » Mon Dec 23, 2019 4:13 am

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 Skyrim Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

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

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

Re: Need Assist on Saving Data to Save File

Post by l3lessed » Mon Dec 23, 2019 5:29 am

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 Skyrim 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: 1996
Joined: Sat Aug 26, 2017 2:46 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by Hazelnut » Mon Dec 23, 2019 9:55 pm

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: 223
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Need Assist on Saving Data to Save File

Post by l3lessed » Tue Dec 24, 2019 12:40 am

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 Skyrim Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

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

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

Re: Need Assist on Saving Data to Save File

Post by l3lessed » 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
My Skyrim 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: 786
Joined: Mon Oct 07, 2019 4:11 pm
Location: Norway

Re: Need Assist on Saving Data to Save File

Post by Ralzar » Sun Feb 02, 2020 11:38 am

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.
My released mods

"I feel like Ralzar specifically wrote those mods for me and then said "Use them". Just so he could watch me die more."
-FuzzyBeanPlays

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

Re: Need Assist on Saving Data to Save File

Post by TheLacus » Sun Feb 02, 2020 12:22 pm

You need to implement IHasModSaveData and create an instance.
Mod System documentation - Learn how to create mods for Daggerfall Unity.
Modder Discussion - General help and discussion for the mod system.
Github Issues - Submit a bug report for the game, including the mod system.

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

Re: Need Assist on Saving Data to Save File

Post by Ralzar » Sun Feb 02, 2020 4:46 pm

Figured it out :)
My released mods

"I feel like Ralzar specifically wrote those mods for me and then said "Use them". Just so he could watch me die more."
-FuzzyBeanPlays

Post Reply