Many copies of 1 component in 1 entity

I have

[GenerateAuthoringComponent]
public struct BodyPart : IComponentData
{
    public BodyPartType type; //byte enum
    public byte area;
    public byte mass;
    public byte armor;
};

And I’d like to specify several body parts for my entities, but I can’t, because you can’t have 2 copies of 1 component on an entity. I know I can split BodyPart into Head/Arm/Leg/Wing etc. but it would still be problematic (some monsters will have 7 heads), and I don’t want to write the code to handle hit detection and response separately for each possible BodyPart.

Can I have several components inheriting from BodyPart and iterate over all components of this entity that inherit from BodyPart? It would save me a lot of code.

Another idea I had was to make BodyPart an IBufferElementData instead of IComponentData. But then I can’t use GenerateAuthoringComponent and edit it directly from unity editor if I understand correctly?

Or maybe I’m doing this completely wrong and there’s a more natural way? I’m new to ECS.

I suggest look into DynamicBuffer.
In oppose to IComponentData, you use IBufferElementData, which allows store arrays of data in entities.

You can check docs
https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/dynamic_buffers.html
But is incomplete.

We have multiple helpful discussions on the forum on this subject. So better search forum and github for examples.

1 Like

I considered that (as I wrote in my first message), but then I cannot use GenerateAuthoringComponent if I understand correctly (or at least I tried and it doesn’t allow me to use that component on game object), so I can’t edit these components in unity editor, I will have to fill all my entities by code?

Most likely yes.
I am not sure, if there is any way of conversion workflow, for DynamicBuffers.
At least I am not aware of any.

1 Like

What about doing one Entity per BodyPart and linking them in a Buffer instead? It should be better compatible with the conversion workflow and I could see more benefits eg code iterating over body parts becomes simpler and body parts now could have individual transforms, but I don’t have alot of information what you want to achive.

1 Like

Yep that could be definitely an option. As long if game design is oriented toward such.
If heads count is just a number with some property, not and physical renderable instance (entity / GameObject), then wouldn’t be needed for entities. But if they need be rendered, then yes, entities could be potentially better solution.

Still however, I don’t see anything wrong, to keep hydra entity with buffer of heads data, instead of just references to each entity as a head. An alternative.

1 Like

You can’t generate authoring components for IBufferElementData right now but you can always write your own authoring component like the way it was done before Entities 0.2.0

3 Likes