I have this base class
public class SpellEffect {
public SpellEffect(){
}
public SpellEffect(string name,SpellEffectType type,float duration,float tickrate,int damage,int slowdown,int value,bool isharm)
{
SpellEffectName = name;
Type = type;
Duration = duration;
TickRate = TickRate;
Damage = damage;
Slowdown = slowdown;
EffectValue = value;
IsHarmful = isharm;
}
public string SpellEffectName;
public SpellEffectType Type;
public float Duration;
public float TickRate;
public int Damage;
public float Slowdown;
public int EffectValue;
public bool IsHarmful;
public virtual void OnApply (Mob mob)
{ }
public virtual void OnTick (Mob mob)
{ }
public virtual void OnCrit ()
{ }
public virtual void OnDispel ()
{ }
public virtual void OnFinish (Mob mob)
{ }
}
This is how i serialize it into xml. “EffectContainer” is just a class with a list of SpellEffects.
public void CreateItem ()
{
EffectsContainer effectContainer = new EffectsContainer ();
Type[] effectType = {typeof(SpellEffect)};
FileStream fs = new FileStream (Path.Combine (Application.streamingAssetsPath, "SpellEffects.xml"), FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(EffectsContainer),effectType);
effectContainer = (EffectsContainer)serializer.Deserialize (fs);
serializer.Serialize (fs, effectContainer);
fs.Close ();
effectContainer.SpellEffects.Add (new SpellEffect(SpellEffectName,Type,Duration,TickRate,Damage,Slowdown,EffectValue,IsHarmful));
fs = new FileStream (Path.Combine (Application.streamingAssetsPath, "SpellEffects.xml"), FileMode.Create);
serializer.Serialize (fs, effectContainer);
fs.Close ();
}
This is deserialize
public void Awake()
{
Type[] itemTypes = {typeof(SpellEffect) };
XmlSerializer serializer = new XmlSerializer (typeof(EffectsContainer), itemTypes);
TextReader textReader = new StreamReader (Application.streamingAssetsPath + "/SpellEffects.xml");
EffectsContainer = (EffectsContainer)serializer.Deserialize(textReader);
textReader.Close ();
}
My problem is, i thought i could make any subclass, lets say, damage over time spell " Burn.cs : SpellEffect "
and basically vary the OnTick() or OnDispel() Logic.
But, i am not sure how to serialize it and then deserialize into its own classes, assuming there could be n SpellEffects/Classes.