Code: Select all
public class Example : IHasModSaveData
{
}
Code: Select all
mod.SaveDataInterface = new Example();
Code: Select all
public class Example : IHasModSaveData
{
}
Code: Select all
mod.SaveDataInterface = new Example();
Code: Select all
namespace DaggerfallWorkshop.Game
{
public class OutfitManager : MonoBehaviour
{
public class MyModSaveData
{
public string Text;
public int Items;
}
[FullSerializer.fsObject("v1")]
public class Example : IHasModSaveData
{
public Type SaveDataType
{
get { return typeof(MyModSaveData); }
}
public object NewSaveData()
{
return new MyModSaveData
{
Text = "Default text",
Items = 5
};
}
public object GetSaveData()
{
return new MyModSaveData
{
Text = text,
Items = items
};
}
public void RestoreSaveData(object saveData)
{
var myModSaveData = (MyModSaveData)saveData;
text = myModSaveData.Text;
items = myModSaveData.Items;
}
Code: Select all
[Invoke(StateManager.StateTypes.Game, 1)]
public static void Init2(IHasModSaveData IHasModSaveData)
{
Debug.Log("Loads saved data");
mod.SaveDataInterface = new Example();
}
It can be. I'm just screwing up the OOP language, since I have very little hands on programming with OOP based language.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.
Code: Select all
public class OutfitManager : IHasModSaveData
{
Code: Select all
public class OutfitManager : MonoBehaviour, IHasModSaveData
{
public Type SaveDataType { get; }
public object NewSaveData() { return null; }
public object GetSaveData() { return null; }
public void RestoreSaveData(object saveData) {}
...
Code: Select all
namespace HarvestableCrops
{
#region Types
[FullSerializer.fsObject("v1")]
public class HarvestableCropsSaveData
{
public int Progress;
public Dictionary<HarvestedCrop, int> HarvestedCrops;
}
#endregion
/// <summary>
/// Make crops harvestable.
/// </summary>
public class HarvestableCrops : MonoBehaviour, IHasModSaveData
{
#region Fields
/// <summary>
/// Progress of harvesting skill.
/// </summary>
int progress;
/// <summary>
/// Crops which are currently harvested. Values is harvest day from zero.
/// </summary>
Dictionary<HarvestedCrop, int> harvestedCrops = new Dictionary<HarvestedCrop, int>();
#endregion
#region Properties
public Type SaveDataType
{
get { return typeof(HarvestableCropsSaveData); }
}
#endregion
#region Unity
[Invoke(StateManager.StateTypes.Start, 0)]
public static void Init(InitParams initParams)
{
GameObject go = new GameObject("HarvestableCrops");
instance = go.AddComponent<HarvestableCrops>();
mod = initParams.Mod;
mod.SaveDataInterface = instance;
mod.LoadSettingsCallback = (settings, _) => settings.Deserialize("Options", ref instance);
mod.LoadSettings();
mod.IsReady = true;
}
#endregion
#region Public Methods
public object NewSaveData()
{
return new HarvestableCropsSaveData {
Progress = 0,
HarvestedCrops = new Dictionary<HarvestedCrop, int>()
};
}
public object GetSaveData()
{
if (harvestedCrops.Count == 0)
return null;
return new HarvestableCropsSaveData {
Progress = progress,
HarvestedCrops = harvestedCrops
};
}
public void RestoreSaveData(object saveData)
{
var harvestableCropsSaveData = (HarvestableCropsSaveData)saveData;
progress = Mathf.Clamp(harvestableCropsSaveData.Progress, 0, maxProgress);
harvestedCrops = harvestableCropsSaveData.HarvestedCrops;
}
#endregion
}
}
Code: Select all
public class OutfitManager : IHasModSaveData
{
public Type SaveDataType { get { return typeof(MyModSaveData); } }
public object NewSaveData() { return null; }
public object GetSaveData() { return null; }
public void RestoreSaveData(object saveData)
{
var myModSaveData = (MyModSaveData)saveData;
text = myModSaveData.Text;
items = myModSaveData.Items;
}
Code: Select all
public object GetSaveData()
{
return new MyModSaveData
{
Text = text,
Items = items;
};
}
Code: Select all
//just an example of how to add a mono-behavior to a scene.
GameObject outfitGo = new GameObject("outfit");
OutfitManager outfit = outfitGo.AddComponent<OutfitManager>();
Severity Code Description Project File Line Suppression State
Error CS0311 The type 'DaggerfallWorkshop.Game.OutfitManager' cannot be used as type parameter 'T' in the generic type or method 'GameObject.AddComponent<T>()'. There is no implicit reference conversion from 'DaggerfallWorkshop.Game.OutfitManager' to 'UnityEngine.Component'. Assembly-CSharp C:\Games\daggerfall-unity-master 10.13\Assets\Scripts\OutfitManager.cs 130 Active