Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I test if a DynamicBuffer IBufferElementDat exist on an entity or not?

Discussion in 'Entity Component System' started by goodnewsjimdotcom, Apr 30, 2022.

  1. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Hello,

    I am using Dynamic Buffers, and they work acceptable. I just can't test if a DynamicBuffer exists or not to do the following code:

    DynamicBuffer<TriggerIgnoreBufferElement> beA = World.DefaultGameObjectInjectionWorld.EntityManager.GetBufferfrome <TriggerIgnoreBufferElement>(entityA);

    It throws an error when TriggerIgnoreBufferElement is not on the entity, and not all entities who have triggers need things ignored, so not every entity will have this array.

    All I need is to be able to test if the Entity has a DynamicBuffer attached.

    Traditional Unity has checks like this under naming convention hasBuffer().

    Thank you,
    Jim
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    I haven’t used the latest ECS but I imagine you
    get an EntityManager and construct the EntityQueries from there
     
    goodnewsjimdotcom likes this.
  3. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    Looks like hasComponent will do the trick.

    TY tertle
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Either use HasComponent as mentioned above, or create job, which filters query with/without buffer.