The CacheUtility module makes it possible to add caches of data to ScriptableObjects. They are setup in runtime, and discarded when play ends.
Modifying member fields in a ScriptableObject through code has inconsistent behaviour during runtime. In the editor, the set fields remain the same between test runs. In a build, the set fields are reset at launch. By assigning a cache to them, the information is set when they are first referenced, both once per app launch or per editor test run. The data is never kept between runs, and easily accessible for persistence or internet connections.
Within DuskModules, CacheUtility is mainly used by modules which rely on persisting data to and from files, or internet connections. Main examples being DataControl and ServerControl. CacheUtility has no dependencies.
The CacheObject is a ScriptableObject which keeps a serializable class instance as cache in memory during runtime. The cache class must implement the ICache interface.
The easiest way to make your own ScriptableObject with a cache is to inherit CacheObject, and assign the serializable class you want to it as type parameter. The member field 'cache' is of that type, and is publically accessible.
The first time the cache is referenced, it is null, and is automatically created and stored in memory. When this happens, the method CacheSetup() is called. Override this method to assign any variables you'd like to the cache. This always happens before the cache is actually used.
An implementation of an CacheObject could look like this:
public class TeamCache, ICache {
public int score;
}
public class TeamObject : CacheObject<TeamCache> {
public int score => cache.score;
protected override void CacheSetup() {
cache.score = 100; // set starting score
}
public void AddScore(int add) {
cache.score += add;
}
}
If you want to add a cache to a ScriptableObject which is already inheriting a class, you can implement the ICacheCarrier interface.
Extension methods are provided for any class implementing the interface. You have to pass an Action (the method to setup the cache) when getting or setting the cache. This method is called when the cache is created.
See the following example on how to expose the cache for use:
public class MyCache, ICache {
public int value;
}
public class MyCacheSO : MySO, ICacheCarrier {
public int startingValue;
public int value => cache.value;
public MyCache cache {
get => this.GetCache<MyCache>(CacheSetup);
set => this.SetCache(CacheSetup, value);
}
protected void CacheSetup() {
cache.value = startingValue;
}
}
An Action is used instead of an interface method, so CacheSetup is able to be protected or private instead of forcing it to be public.
Don't forget to add using DuskModules.CacheUtility; to any script using the module.