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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Is there a way to call EntityCommandBuffer.AddComponent(Entity, ComponentTypes) from a Burst Job?

Discussion in 'Entity Component System' started by adrodoc55, May 27, 2022.

  1. adrodoc55

    adrodoc55

    Joined:
    May 27, 2022
    Posts:
    3
    I would like to call EntityCommandBuffer.AddComponent(Entity, ComponentTypes) and EntityCommandBuffer.RemoveComponent(Entity, ComponentTypes) from a Burst job, but I can't find a way to do so. I would like to write something like this:

    cmdBuf.AddComponent(entity, new ComponentTypes(typeof(InUseTag), typeof(BaseData), typeof(HealthData)));

    The problem is that in order to create a ComponentType I first need to get the System.Type via typeof, but System.Type is a class and typeof is therefor managed code, so I can't do that with Burst.
    I can't even create a const or static variable of type ComponentType. Const does not work, because C# does not allow const structs and static does not work, because Burst complains with:
    Burst error BC1360: A static constructor on type `...` is mixing managed and unmanaged code which is not supported. In order to solve this, please move the managed code or unmanaged code to a different class/struct​

    So I am stuck with the same problem as https://forum.unity.com/threads/feature-request-ecb-setarchetype.899768/: I have to add many component types one by one to my entity changing the archetype each time instead of once for all added component types.

    Is there any way to call these two methods on EntityCommandBuffer from Burst code?
     
  2. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    You can use ComponentType.ReadWrite<T>
     
    bb8_1 and adrodoc55 like this.
  3. adrodoc55

    adrodoc55

    Joined:
    May 27, 2022
    Posts:
    3
    Thank you very much!