Created
December 9, 2020 15:11
-
-
Save Ryxali/9e0bef07564afa9c3ffdcfe7b2791512 to your computer and use it in GitHub Desktop.
Novel separation of shared and non-shared state with ScriptableObjects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ScriptableInst<T> where T : ScriptableFactory<T> | |
{ | |
public T SharedData { get; internal set; } | |
public ScriptableInst() | |
{ | |
} | |
public ScriptableInst(T sharedData) | |
{ | |
SharedData = sharedData; | |
} | |
protected internal abstract void OnFactoryCreate(); | |
} | |
// Shared Data is assumed to be immutable | |
public class ScriptableFactory<T> : ScriptableObject where T : ScriptableFactory<T> { | |
public TInstance Create<TInstance>() where TInstance : ScriptableInst<T>, new() | |
{ | |
var inst = new TInstance(); | |
inst.SharedData = (T)this; | |
inst.OnFactoryCreate(); | |
return inst; | |
} | |
} | |
public class TestFac : ScriptableFactory<TestFac> | |
{ | |
public int a; | |
public class TestInst : ScriptableInst<TestFac> | |
{ | |
private int incrA; | |
public TestInst(TestFac fac, int a, int b) : base(fac) | |
{ | |
} | |
protected internal override void OnFactoryCreate() | |
{ | |
Debug.Log(SharedData.a); | |
} | |
public void Update() | |
{ | |
incrA += SharedData.a; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment