Search Unity

to "mark" hundreds of entities with a tag for .ForEach, do u use AddComponent

Discussion in 'Graphics for ECS' started by mailfromthewilds, Jun 14, 2021.

  1. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    217
    i know i can give empty tag to hundreds of entities with for loop and .AddComponent but was wondering if there is better way?
     
  2. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    867
    Add a IComponentData with a bool field and set true or false as needed.
     
  3. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    If you know your entities (i.e. can express their subset with an EntityQuery), use one of the methods in EntityManager that take an EntityQuery; or you can fill a NativeArray with them in your loop, and after the loop set the Tag on them:
    Code (CSharp):
    1. entityManager.AddComponent(EntityQuery, ComponentType)
    2. entityManager.AddComponent(NativeArray<Entity>, ComponentType)
    These methods are much faster than individually adding the components, e.g. through a CommandBuffer or inside a non-burst loop.

    All structural changes require a sync point, so using this way is the most efficient way to make use of the CPU resources here if you depend on the structural change.

    If you don't depend on the structural change, writing a boolean flag into a ComponentData can be a way to go ahead, but you will lose the innate ability of ECS to filter your Entities and only work on those you care about (meanign you need to have a conditional inside each loop testing for said flag)
     
    Last edited: Jul 28, 2021
    HeyZoos likes this.