Saving Data

Currently viewing this thread:

Cheyros

Recruit
Was helping someone in pm save data in their mod so I thought I would just make a thread and leave it here...

I will try to show you a few different things you need to know to save data which caused many crashes for me to learn xD

Code:
//SAVING DATA WHERE THE TYPE IS A CLASS YOU MADE
//YOU NEED TO DEFINE YOUR CLASS AND ANY CONTAINERS YOU MAY USE WITH YOUR CLASS


    //THIS IS MY OWN CLASS I MADE AND I USE IT IN MY SCRIPT
    //IN MY SCRIPT I ADD A BUNCH OF THESE OBJECTS I CREATE TO A LIST
    //MAKE SURE TO USE THE CORRECT ATTRIBUTES DEPENDING ON MEMBER
    //ALL LOCAL IDS NEED TO BE UNIQUE PER CLASS
    //ALL SAVEABLE CLASS IDS NEED TO BE UNIQUE BETWEEN YOUR DIFFERENT SAVEABLE CLASSES

    [SaveableClass(1)]
    internal class MapEventInfo
    {
        internal MapEventInfo(MapEvent mapEvent, bool surrendered)
        {
            Surrendered = surrendered;
            MapEvent = mapEvent;
        }
        [SaveableProperty(1)]
        internal bool Surrendered { get; set; }
        [SaveableProperty(2)]
        internal MapEvent MapEvent { get; set; }
    }

    //YOU NEED TO CREATE THIS CLASS TO DEFINE YOUR CREATED CLASSES
    public class MySaveDefiner : SaveableTypeDefiner
    {
        //USE CRC32 HASH BASED ON YOUR MOD'S NAME TO HELP AVOID CONFLICT WITH OTHER MODS
        //https://www.fileformat.info/tool/hash.htm PUT YOUR MOD'S NAME INTO THIS TOOL AND THEN SCROLL DOWN TO GET THE FIRST 6 NUMBERS OF YOUR MOD'S CRC32 HASH
        //AIValuesLife = c5e07920 -> just use first 6 digits e07920
        //thanks to tyler from the community patch team
        public MySaveDefiner() : base((0xe07920 << 8) | 123)//this will ensure you use a base id unique to your mod... people need to do this or else you will have problems saving with many mods installed
        {
        }
        protected override void DefineClassTypes()
        {
            AddClassDefinition(typeof(MapEventInfo), 1);//DEFINING YOUR CLASS TO THE GAME, USE UNIQUE ID PER SAVEABLE CLASS
        }
        protected override void DefineContainerDefinitions()
        {
            ConstructContainerDefinition(typeof(List<MapEventInfo>));//DEFINING ANY CONTAINERS YOU PLAN TO USE
        }
    }

    …

    //BEHAVIOR CLASS
        public override void SyncData(IDataStore dataStore)
        {
            try
            {
                dataStore.SyncData("mapEvents", ref mapEvents);
            }
            catch (Exception)
            {
            }
            finally
            {
            }
        }
        [SaveableField(1)]//you can save anything you like... floats bools, whatever, i just showed you the most complicated example
        private List<MapEventInfo> mapEvents = new List<MapEventInfo>();
        [SaveableField(2)]//dont repeat local id within same class
        private bool whatever;

if you need to save just some basic type property or field, you dont need to define all that stuff above just put the saveable attribute and make sure to use datastore in sync method to sync and your mod's data will be saved in the save game file. It is super easy once you get the hang of it
 
Top Bottom