• We have updated our Community Code of Conduct. Please read through the new rules for the forum that are an integral part of Paradox Interactive’s User Agreement.

Zarine

Field Marshal
134 Badges
Feb 28, 2007
4.827
689
  • Europa Universalis IV: Res Publica
  • Gettysburg
  • Hearts of Iron III
  • Heir to the Throne
  • Europa Universalis III Complete
  • Knights of Pen and Paper +1 Edition
  • Leviathan: Warships
  • The Kings Crusade
  • Magicka
  • Majesty 2
  • Majesty 2 Collection
  • March of the Eagles
  • Europa Universalis III Complete
  • For the Motherland
  • Victoria: Revolutions
  • Rome Gold
  • Semper Fi
  • Sengoku
  • Ship Simulator Extremes
  • Sword of the Stars
  • Supreme Ruler: Cold War
  • The Showdown Effect
  • Victoria 2
  • Victoria 2: A House Divided
  • Victoria 2: Heart of Darkness
  • Hearts of Iron IV: No Step Back
  • Crusader Kings II: Sword of Islam
  • Arsenal of Democracy
  • Hearts of Iron II: Armageddon
  • Cities in Motion
  • Cities in Motion 2
  • Crusader Kings II
  • Crusader Kings II: Charlemagne
  • Crusader Kings II: Legacy of Rome
  • Crusader Kings II: The Old Gods
  • Crusader Kings II: Rajas of India
  • Crusader Kings II: The Republic
  • Crusader Kings II: Sons of Abraham
  • Crusader Kings II: Sunset Invasion
  • A Game of Dwarves
  • Commander: Conquest of the Americas
  • Darkest Hour
  • East India Company Collection
  • Europa Universalis III
  • Europa Universalis III Complete
  • Divine Wind
  • Europa Universalis IV
  • Europa Universalis IV: Art of War
  • Europa Universalis IV: Conquest of Paradise
  • Europa Universalis IV: Wealth of Nations
With the help of the IRC communauty, I pull out saving and loading data into/from savegame.

So to help other people who might need to save/load data, here is how I did it:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using ICities;
using UnityEngine;

namespace DifficultyOptions
{

    public class DifficultySettings : SerializableDataExtensionBase
    {
        private static int _maintenanceCostMod = 100;
        private static int _constructionCostMod = 100;

        public void setMaintenanceCost(int value) { _maintenanceCostMod = value; }
        public int getMaintenanceCost() { return _maintenanceCostMod; }

        public void setConstructionCost(int value) { _constructionCostMod = value; }
        public int getConstructionCost() { return _constructionCostMod; }

        public byte[] serialize()
        {
            IEnumerable<byte> maintenanceCostData = BitConverter.GetBytes(_maintenanceCostMod);
            IEnumerable<byte> constructionCostData = BitConverter.GetBytes(_constructionCostMod);

            IEnumerable<byte> serialized = maintenanceCostData.Concat(constructionCostData)
            return serialized.ToArray<byte>();
        }

        public void deserialize(byte[] data)
        {
            _maintenanceCostMod = BitConverter.ToInt32(data, 0);
            _constructionCostMod = BitConverter.ToInt32(data, 4);
        }

        public override void OnSaveData()
        {
            this.serializableDataManager.SaveData("ZarineDifficultyMod", this.serialize());
        }

        public override void OnLoadData()
        {
            byte[] data = this.serializableDataManager.LoadData("ZarineDifficultyMod");
            if (data == null) { return; }

            this.deserialize(data);
        }
    }


}

So at New Game and Save Game, OnLoadData() is called on all the class "SerializableDataExtensionBase".
There you need to load the data with the manager according to the name you save them under.
It seems that currently, at newgame or if the save doesn't have your data, it will return a null pointer and not an empty byte[].

Then you take the data and store it where you need it.


At Save time, it will call the OnSaveData() data, where you need to feed the manager with the name and the serialized data.
And that's it.


Note: I'm still trying to have nicer serialize/deserialize method so any idea is welcome.
 
Last edited: