If after creation this data structure is meant to be immutable, you want to use the Blob API for this. The last part of this video will provide some information on it.
If after creation (even at runtime) this structure is immutable, blobs make sense. Otherwise, this is when I would break out into unsafe space and use a pointer in an ISystemStateComponentData that allocates dynamically for what it needs.
I’ve never used system components before. I’ve just read some posts about them and I cannot determine what the advantage would be. What did you have in mind?
IComponentData and ISystemStateComponentData work the same except when you destroy an entity.
When you destroy an entity with ISystemStateComponentData, all components are removed from the entity as usual but the ISystemStateComponentData still remains. This lets you have a query that lets you clean up the ISystemStateComponentData before removing it to destroy the entity.
I believe DreamingImLatios is suggesting this so you can clean up the pointer.
As I am not good at explaining things I’ll demonstrate instead.
public unsafe struct SystemState : ISystemStateComponentData
{
public void* Pointer;
}
public struct Tag : IComponentData
{
}
public class SystemStateSystem : ComponentSystem
{
protected override unsafe void OnUpdate()
{
// do some work
this.Entities.WithAll<Tag>().ForEach((ref SystemState state) =>
{
var ptr = state.Pointer;
});
// either entity was destroyed or tag component was removed, we need to cleanup pointer memory
this.Entities.WithNone<Tag>().ForEach((Entity entity, ref SystemState state) =>
{
UnsafeUtility.Free(state.Pointer, Allocator.Persistent);
// If you remove the SystemStateComponent and there are no other components left, the entity will now be deleted
this.EntityManager.RemoveComponent<SystemState>(entity);
});
}
}
If you did not use SystemState when the Entity was destroyed the memory would not be freed.