Component Registration
Component Registration, this is how you register and change components in arch.
All components are recorded and stored in the ComponentRegistry
. It primarily stores meta data such as the size and types of the components, which are then queried at runtime as a compile-time static or manually to access the underlying arrays of Archetypes
and Chunks
and more.
These metadata are expressed as ComponentType
, a struct that you may have seen here and there before. It combines the component with its most important metadata and is used precisely for this purpose.
Register
There are some cases where you might prefer to register your components yourself because YOU simply know more than Arch and C# at that moment. For example, C# has problems with managed structs or even classes here and there. This feature is also useful for AOT.
// Problematic managed struct
public record struct Inventory(List<Item> Items);
// Register, 8 = Size in bytes of the managed struct.
var componentType = new ComponentType(ComponentRegistry.Size-1, 8);
ComponentRegistry.Add(typeof(Inventory), componentType);
Alternatively, you can also call up the generic variants such as...
ComponentRegistry.Add<Inventory>();
ArrayRegistry.Add<Inventory>();
Acess
You can not only register components but also receive, replace and remove.
// Checks whether the component is registered
var hasInventoryRegistered = ComponentRegistry.Has<Inventory>();
// Try-Get
if(hasInventoryRegistered)
{
ComponentRegistry.TryGet<Inventory>(out var componentType);
Console.WriteLine(componentType);
}
// Replaces the component with a new variant e.g. hot Hot-Loading
ComponentRegistry.Replace<Inventory, NewInventory>();
// Removes the Component from registration
ComponentRegistry.Remove<Inventory>();
Compile-Time static
So now you've registered all these wonderful components... but how does Arch get to it under the hood? And how can you do it yourself? Of course this works by accessing the ComponentRegistry
, but this is always associated with a lookup. There has to be an more efficient way!
var componentType = Component<Inventory>.ComponentType;
Using the Component<T0...T25>
is one way. This is a compile-time static class that provides all meta-data for the passed Component
directly, without any lookups! And the best, it comes with up to 25 generic overloads!
var signature = Component<Dwarf, Axe, Inventory>.Signature;
Cool, right? Now you got the tools to build almost everything you want on your own! E.g. custom librarys and extensions!
Last updated
Was this helpful?