Page 1 of 5

Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 6:15 am
by l3lessed
I have my outfit manager done. It meets the basic features I set out to create and users were requesting. However, I can't release it until I figure out how to save an integer and a dictionary variable to the save file to give permanency to player created outfits between play sessions. Can anyone provide an example script file of this? The website documentation shows the basic code, but it doesn't show it within a script file. This leaves me confused about how it works, and I rather not spend days figuring it out, if I could get an assist.

Also, is there any easy way to call a user input window in some form? The last feature I want is for players to be able to name their outfits.

Here's the current feature list
- It has an endless expanding dictionary to hold an endless amount of outfits.
- It duplicates every outfit slot exactly, including unequipped slots, and updates armor values.
- You can override outfit slots to update and replace older outfits.
- You can delete outfits to create empty slots.
- Smart controls ensure player only has 1 extra empty slot no matter how many outfits created and saved.
- pop-up UI messages to explain what is happening with the controls.
- basic debug log messages to assist in player debugging and development.


Re: Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 1:22 pm
by TheLacus
If you want to store data liked to a specific character you can make a class in your mod that implements the IHasModSaveData interface. The you can assign one instance of the class to Mod.SaveDataInterface. If you can be specific on what you don't understand i will try to help you further. :)

Re: Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 1:27 pm
by TheLacus
l3lessed wrote: Mon Dec 16, 2019 6:15 am Also, is there any easy way to call a user input window in some form? The last feature I want is for players to be able to name their outfits.
Yes, you use a DaggerfallInputMessageBox. This is an example from DaggerfallInventoryWindow:

Code: Select all

DaggerfallInputMessageBox mb = new DaggerfallInputMessageBox(uiManager, this);
mb.SetTextTokens(goldToDropTextId);
mb.TextPanelDistanceY = 0;
mb.InputDistanceX = 15;
mb.InputDistanceY = -6;
mb.TextBox.Numeric = true;
mb.TextBox.MaxCharacters = 8;
mb.TextBox.Text = "0";
mb.OnGotUserInput += DropGoldPopup_OnGotUserInput;
mb.Show();

Re: Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 6:00 pm
by l3lessed
So, this is what I am trying to do. My outfit manager, on load, creates an empty public dictionary and a public integer called index. The dictionary then stores the outfit in an array list that contains the players saved outfit.

The public integer is used to control what outfit dictionary key and paired array the current player has selected.

I need, on save open and close, for the outfit manager to load up and save the public dictionary and the public integer used to access and control that dictionary.

First off, does the IHasModSaveData interface need to be its own script file, since it is its own class storing the values?

I take it I need to declare the values in the ihasmodsavedata class first, then use the Type SaveDataType { get; } declaration to set those values to the save. So, declare the int and the dictionary, then set it using the {get;} serializer, then I use a trigger with GetSaveData() and NewSaveData() to save new data or update existing save data, and then I use void RestoreSaveData(object saveData) to restore that save data on script load. At this point, its calling the Mod.SaveDataInterface in the main mod script file to restore the data from the IHasModSaveData interface class.

The GetSaveData(), NewSaveData(), and RestoreSaveData(object saveData) will pull the values for you without any extra coding or value inputs? Merely calling the method will load up the class with the data for retrieval? They only need defined and then set within the public interface IHasModSaveData class?

I think I might be over complicating it.

Re: Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 6:06 pm
by TheLacus
It's the other way around. You need to define an implementation for the interface, meaning that you must write their content; the game will calls these methods at the appropriate times. For example when GetSaveData() is called, you must return your custom data (i.e. a class defined in your mod) casted to object. When RestoreSaveData() is called, you receive an object as argument and you can cast it back to your custom type.

Any class can implement this interface, even your main MonoBehaviour.

Re: Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 8:09 pm
by l3lessed
Okay, I think that is making more sense. I'm using this documentation as a reference: https://www.dfworkshop.net/projects/dag ... g/features. This documentation is what you're referencing, when you say the methods need called and they then return the set custom saved data, correct?

From this documentation, I can setup the public class to store the values, like in the first code block.

However, this is confusing me. mod.SaveDataInterface = instance; I can't seem to initiate a SaveDataInterface method, and I do not see from the documentation, where this method is being called from to initiate the instance. I imagine you are defining the instance in the iniParams as stated on the page, but I can't seem to get it to initiate. I understand it has to be initiated first using the initparams, but this is where I get stuck.

Based on the website documentation, I understand the process like below.

First create the public class to store the values.
Second, initiate the mod instance and connect it to the class values using the SaveDataInterface instance method.
Third, setup save datatype deserialization public type for the values.
Fourth, use the GetSaveData, RestoreSaveData, and NewSaveData to save, override, and restore the saved values.

If so, I am getting stuck on the second step. I can't figure out how to get the initparams to connect the mod interface to the mod instance so I can pull and save values.

I also am having issues with this block of code making me think I'm either using it/placing it wrong or it is merely a reference to an already setup method of the SaveDataInterface.

Code: Select all

public object NewSaveData()
{
    return new MyModSaveData
    {
        Text = "Default text",
        Items = new List<int>();
    };
}
I know this is my own dumb misunderstanding about how the code is working. That is why I tend to like to look at full script files with full coding implementation.

Thanks for the tip on the user input window too. That will make setting up outfit names much easier in the future.

Re: Need Assist on Saving Data to Save File

Posted: Mon Dec 16, 2019 11:12 pm
by l3lessed

Code: Select all

        [Invoke(StateManager.StateTypes.Game, 1)]
        public static void Init2(IHasModSaveData IHasModSaveData)
        {
            Debug.Log("init 2");
            mod.SaveDataInterface =
        }
This is where I am. I've loaded the interface using an init method. I then tie the interface to the mod using the .SavedDataInterFaceMethod.

However, when I try to tie it to a instance of my mod in the above mod.savedatainterface, it says it is not the correct type. I'll keep pounding away though. I think I'm headed down the right track now. I have the interface initiated through a init method and the SaverDataInterface method working. I think the issue is now I need to create the mod instance through an object declaration, but having issues figuring out the code for this.

Re: Need Assist on Saving Data to Save File

Posted: Tue Dec 17, 2019 12:05 am
by l3lessed
OMG, was it really this simple the whole time?

Code: Select all

        [Invoke(StateManager.StateTypes.Game, 1)]
        public static void Init(IHasModSaveData IHasModSaveData)
        {

            Debug.Log("init 2");
            var savedsettings = mod.SaveDataInterface;
            savedsettings.NewSaveData();
            savedsettings.GetSaveData();
            savedsettings.RestoreSaveData();
        }
Then use these to pull and save the values, correct? It seems off though, as the ordering is backwards for setting the instance than what is shown on the webpage.

As usual, just talking to myself as a I develop to help me understand, and if anyone has any tips, let me know. If this is flooding the forum to much, please let me know. :)

Re: Need Assist on Saving Data to Save File

Posted: Tue Dec 17, 2019 12:46 am
by l3lessed
Is this it? From re-reading everything you say, this seems to be the right coding as I understand it.

Initialize the interface using an init class.

Within the interface init class, create a mod.SaveDataInterface and assign it to the IHasModSavedData instance I wish to call/used. Then convert the values into an object to pass through the IHasModSavedData Methods.

Like This?

Code: Select all

        [Invoke(StateManager.StateTypes.Game, 1)]
        public static void Init(IHasModSaveData IHasModSaveData)
        {
            //Loads mod data from save file
            Debug.Log("init Saved Mod Data");
            //assigns interface instance to savedatainterface. If restored values are not present, create new save data.
            mod.SaveDataInterface = IHasModSaveData.RestoreSaveData();
            if(mod.SaveDataInterface == null)
            {
                mod.SaveDataInterface = IHasModSaveData.NewSaveData();
            }
        }

Re: Need Assist on Saving Data to Save File

Posted: Tue Dec 17, 2019 2:55 am
by l3lessed
Sorry, duplicate. :oops: