diff --git a/TestGame/Program.cs b/TestGame/Program.cs index 733b808..bebb1ff 100644 --- a/TestGame/Program.cs +++ b/TestGame/Program.cs @@ -56,14 +56,16 @@ public override void Update() public class Rotate : Script { - private readonly float speed_; + private float speed_; public Rotate(Entity entity, float speed) : base(entity) { speed_ = speed; StoreScriptData("speed",speed_);//we store the data for this script, so it persists in save file } //constructor used for deserialization - public Rotate(Entity entity, Dictionary scriptData) : base(entity, scriptData) + public Rotate(Entity entity) : base(entity){} + + protected override void AfterDeserialization() { speed_ = RetrieveScriptData("speed");//we retrieve data stored when deserializing } diff --git "a/\303\246lum/Systems/Components.cs" "b/\303\246lum/Systems/Components.cs" index b764a7c..8b09e86 100644 --- "a/\303\246lum/Systems/Components.cs" +++ "b/\303\246lum/Systems/Components.cs" @@ -43,9 +43,7 @@ public static Component CreateFromData(Entity entity, ComponentData componentDat if (componentData.typeId == ComponentTypes.Script) { ScriptTypeAndData stad = MessagePackSerializer.Deserialize(componentData.serialData); - if (stad.ScriptData != null) - return Activator.CreateInstance(Type.GetType(stad.ScriptType), entity, stad.ScriptData) as Component; - return Activator.CreateInstance(Type.GetType(stad.ScriptType), entity) as Component; + return Script.CreateFromData(entity, stad); } throw new Exception("component type couldn't be resolved; make sure to add all serializable types to this method"); diff --git "a/\303\246lum/Systems/Scripts.cs" "b/\303\246lum/Systems/Scripts.cs" index 959d8d0..5bd23d8 100644 --- "a/\303\246lum/Systems/Scripts.cs" +++ "b/\303\246lum/Systems/Scripts.cs" @@ -14,11 +14,15 @@ protected Script(Entity entity) : base(entity) } - protected Script(Entity entity, Dictionary scriptData) : this(entity) + //cereal factory + internal static Script CreateFromData(Entity entity, ScriptTypeAndData stad) { - this.scriptData = scriptData; + var newScript = Activator.CreateInstance(Type.GetType(stad.ScriptType), entity) as Script; + newScript.scriptData = stad.ScriptData; + newScript.AfterDeserialization(); + return newScript; } - + protected void StoreScriptData(string key, object data) { if (scriptData == null) scriptData = new Dictionary(); @@ -53,19 +57,15 @@ public sealed override ComponentData GetSerialData() } protected virtual void BeforeSerialization() //ovrd if you need to update script data before entity gets serialized - { - - } + {} + + protected virtual void AfterDeserialization() + {} } class PlayerController : Script { - public PlayerController(Entity entity, Dictionary scriptData) : base(entity, scriptData) - { - - } - public PlayerController(Entity entity) : base(entity) { @@ -181,12 +181,11 @@ public Rotator(Entity entity, float speed) : base(entity) StoreScriptData("spd", speed); } - public Rotator(Entity entity, Dictionary scriptData) : base(entity, scriptData) + protected override void AfterDeserialization() { speed = RetrieveScriptData("spd"); - } - + public override void Update() { entity.Rotation += speed*0.1f; @@ -217,7 +216,7 @@ public ICANHAZNAME(Entity entity, string name) : base(entity) StoreScriptData("n", name); } - public ICANHAZNAME(Entity entity, Dictionary scriptData) : base(entity, scriptData) + protected override void AfterDeserialization() { name = RetrieveScriptData("n"); }