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

Help with DynamicBuffer of Entities

Discussion in 'Entity Component System' started by Plummers, May 20, 2020.

  1. Plummers

    Plummers

    Joined:
    Oct 29, 2015
    Posts:
    35
    Hi,
    I have a Formation which moves around and then I have the units "in the formation" which move to the position of their Formation entity.

    Formation #1 << 20 units
    Formation #2 << 16 other units
    etc..

    Currently a Formation does not know its units. The units know their Formation.

    Question. Now I have a situation where a Formation needs to select a specific unit from its formation.

    Is a dynamic buffer the way to go and can I do this:
    "public DynamicBuffer<Entity> units" and have this on my formation?

    Many thanks for any advice and help, still working out how best to use DOTS.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    If you want to use dynamic buffers you need to define an IBufferElementData.
     
  3. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    66
    `ISharedComponentData` seems fit to your need.
    Create something like
    Code (CSharp):
    1. public struct Formation : ISharedComponentData
    2. {
    3.     public int Value;
    4. }
    And `SetFilter` for your query to iterate each entities which have specific `Value` of `Formation`.

    `DynamicBuffer` also works but need to more attention on lifecycle of each entity.
     
  4. Plummers

    Plummers

    Joined:
    Oct 29, 2015
    Posts:
    35

    Thanks quabug. I'm getting a burst error when I try setting this as the unit is instantiated:

    commandBuffer.AddSharedComponent(entityInQueryIndex, unit, new FormationID { Value = 1 });

    Can I set each group of formation units a new Value? So first formation every unit has shared FormationID Value = 1, 2nd formation = 2 etc...

    [Error]
    Burst error BC1001: Unable to access the managed method `object.Equals(object)` from type `FormationID`

    Thanks again
     
  5. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    66
    https://docs.unity3d.com/Packages/c.../api/Unity.Entities.ISharedComponentData.html
    "ISharedComponent implementations must implement IEquatable<T> and GetHashCode()."

    Yes, that is a shared component used for.
    https://docs.unity3d.com/Packages/com.unity.entities@0.10/manual/shared_component_data.html
    "Shared components are a special kind of data component that you can use to subdivide entities based on the specific values in the shared component (in addition to their archetype)."
     
  6. Plummers

    Plummers

    Joined:
    Oct 29, 2015
    Posts:
    35
    Thanks for all your help quabug.

    Found out the reason was you can't add sharedcomponentdata with burst enabled. So got it working now.