#Designing a queue system with ECS

1 messages · Page 1 of 1 (latest)

dire sail
#

Trying to make a queue system where Agents line up at a Resource and the queue has a cap.

Currently its

  • Agents find a target in a job then
  • ComponentLookup Resource has queue incremented up within that job

Dynamic buffer has issue as it needs command buffer so the queue will be overfilled before it takes effect.

Is there a better way than directly modifying via ComponentLookup?

ashen solstice
#

Why does dynamic buffer need command buffer?

dire sail
#

mmm you're right, I can directly change BufferLookup as well

dire sail
#
        private partial struct FindBestTarget : IJobEntity
        {
            public NativeParallelMultiHashMap< int , Target > targetMap ;
            public BufferLookup<ShopItemQueue> shopItemQueues;
            public ComponentLookup<ShopItem> shopItems;

            private void Execute( Entity entity, ref TargetShopItemEntity targetEntity , ref Destination destination , ref AgentBody agentBody ,
                in LocalTransform        transform , in                     DesiredShopItem desiredTask ) {
                
                    // sort by distance and queue size logic
                
                                // add to queue by directly modifying BufferLookup
                if ( shopItemQueues.HasBuffer( targetEntity.Value ) ) {
                  shopItemQueues[ targetEntity.Value ].Add( new ShopItemQueue { Customer = entity } ) ;
                }
            }
        }```
#

so this way is pretty reasonable?

#

or is it not an issue reading and writing the lookup in parallel?

maiden rock
#

Writing to a BufferLookup in parallel would only be an issue if multiple entities processed by the job could have the same TargetShopItemEntity ; you'd get a race condition. If you can guarantee that's not the case by design, then this is reasonable (though you'll have to add the [NativeDisableParallelForRestriction] on the lookup field in order to suppress a safety system error)