Hello everyone,
One thing I find missing in DOTS learning resources is a proper explanation of using native collections in IComponentData. Can (and should?) we put native collections directly into components?
Let’s consider the following example. First, we define component with native container, for example:
public struct CollectionsTest : IComponentData
{
public NativeParallelHashMap<int, Entity> FilteredEntities;
}
Now, my idea for this component usage is simple:
- On game startup, initialize the container
- Regular game loop, fill this component with the data in the system number X, in my case it will be processed data from
ICollisionEventsjob - Regular game loop, process previously obtained data in the system number X + 10, clear the container after processing
- Repeat steps 2 and 3 in every frame
- On game exit - dispose the container
In order to achieve points 1 and 5 I need to do it in the system in runtime, as we cannot allocate native containers in authorings:
public partial struct CollectionsHandlingSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
Entity entity = state.EntityManager.CreateEntity();
state.EntityManager.AddComponentData(entity, new CollectionsTest
{
FilteredEntities = new NativeParallelHashMap<int, Entity>(2048, Allocator.Persistent)
});
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
CollectionsTest ct = SystemAPI.GetSingleton<CollectionsTest>();
ct.FilteredEntities.Dispose();
}
}
My questions:
- Is this a valid approach?
- I intend to use this only for singleton components - are there any potential issues with this method?
- What pitfalls should I be aware of if I proceed this way?
- In case that this is not the way to go, could you propose better solution for my usecase?
Thanks in advance!



