First of all, I want mods in my game, as stated in a previous question, I want to do that via registries. Users make content known to the game in the form of 'Registering' it, then it can be used in several other stuff. Registering a block to have it appear on the terrain is one example.
Registering is done in the form of events, where each registry has an OnEntryCollection event with a list where other classes subscribe to in order to add Entries, and then it gets invoked to gather said entries.
Basically I have a registry system, where there's a large meta-registry(The Game-registry) that has a list of smaller Registry<T>
registries, each dedicated to a type (a block
, an item
, etc). I want to be able to add to these registries via a Registry.Add(T obj)
method, but I'm not exactly sure how to do that because in order to store them I had to use an empty interface I made (IRegistry
).
This is meant to be a way for me to add Entries in through code, from specific files I parse in, while also allowing modders to add in custom content through registry events I will add later. Nothing is meant to be added in through the inspector, rather registered in through events.
My code:
Game-Registry:
[SerializeField] List<IRegistry> registries = new List<IRegistry>();
Registry :
public class Registry<T> : IRegistry where T : IRegistryEntry {
Dictionary<ResourceLocation, T> EntryDictionary = new Dictionary<ResourceLocation, T>();
public delegate void registryAdditionHandler(List<T> entries);
public event registryAdditionHandler OnRegisterAddEntries;
ResourceLocation keyName;
public Registry(ResourceLocation keyName) {
this.keyName = keyName;
}
public void Register(T entry) {
EntryDictionary.Add(entry.GetRegistryName(), entry);
}
public void Remove(ResourceLocation loc) {
EntryDictionary.Remove(loc);
}
public T Get(ResourceLocation key) {
return EntryDictionary[key];
}
public int Count() {
return EntryDictionary.Count;
}
public bool ContainsEntryKey(ResourceLocation key) {
return EntryDictionary.ContainsKey(key);
}
public bool ContainsEntryValue(T value) {
return EntryDictionary.ContainsValue(value);
}
public bool TryGetValue(ResourceLocation key, out T value) {
if (EntryDictionary.TryGetValue(key,out value)) {
return true;
}
return false;
}
public ResourceLocation getRegistryName() {
return keyName;
}
}
IRegistry interface:
public interface IRegistry {
}