Skip to content

Instantly share code, notes, and snippets.

@Ryxali
Created December 9, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryxali/9e0bef07564afa9c3ffdcfe7b2791512 to your computer and use it in GitHub Desktop.
Save Ryxali/9e0bef07564afa9c3ffdcfe7b2791512 to your computer and use it in GitHub Desktop.
Novel separation of shared and non-shared state with ScriptableObjects
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