Search Unity

Is there a way to add 'dynamic' data fields to an Entity?

Discussion in 'Entity Component System' started by Arowx, Dec 31, 2018.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    ECS is great but as the data about your game changes systems have to change, or it tightly couples data and processing.

    However if an Entity could pre-allocate space or allow additional dynamic data to be stored then this could allow for a lot more flexible and dynamic set of systems.

    The kind of systems that could become complex with ECS are probably along the line of Behavioural systems or Key game mechanics that might need to be flexible e.g. Items, Weapons, Combat, Magic, Characters things that might need types or subtypes as your game evolves and grows.

    Let's say you start making a rouguelike or rpg and set up an item system, that needs to hold evey type of item an NPC/Player can carry from food/drink/weapons/clothing/containers/scrolls/magic/torches...

    Ideally all of these items will have basic item data but some might need item specific data e.g. containers need a size and list of items they contain, consumable items will need a quantity left.

    e.g.
    Item : Name, Type, Sub-Type, Size, Weight, ...
    Magic Item : Item ... Power, Class, Uses

    So if there was some way to extend an entities data fields to allow for different types then this could save a lot of work having to re-build the core systems that deal with items.

    Note adding this additional data should not push the entity out of it's core systems.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    What is wrong with IComponentData?
    What is wrong with BufferArrays?
    Do you actually follow ECS forum?
     
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    You are describing DynamicArray? the additional data do not push the pre-existing data out of the core system. But the extended data is a bit out of the core system. I would just make a MagicItemData, normal item entity have just ItemData. Magic item has both ItemData and MagicItemData. Or if you don't want a different archetype at all, attach DynamicArray capacity 1 of bytes or something to all items and let some item use it in a way that's depending on item type. But I like more concrete compile time definition, I would go with IComponentData based way of extending data.
     
  4. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Hi @Arowx. Two quick responses:

    1. This isn’t an ECS-specific problem, which in this case is a good thing. Means more established solutions are out there. :)

    For example, any inventory system that takes up more ram as the character’s inventory grows might want to rethink its design. A common way that avoids that problem is to represent different inventory items with enum values. An inventory would be a simple array of that enum, and each character’s array is just preallocated to a max size. Instead of storing unique property data for inventory items, each possible variation of an inventory item is represented as a separate entry in the enum.

    2. Dynamic Buffers can work to this effect in ECS land. Your ‘NPC’ entity could have a dynamic buffer of type ‘IntevoryItemComponent’, which just holds a single ‘InventoryItem’ enum value.

    And there’s no need for dynamic, blackboard storage.

    That’s just one approach. But does that give you anything useful? Cheers!
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You don't hold the item, you should hold an entity. I suspect this is the answer to every other problem you can think of related to this.

    I've built a pretty good inventory system in Unity ecs. It's simply a dynamic array where each element contains an entity and an int for the count.

    Each item entity can hold different components to represent the various items.
    You're thinking too classical. The answer is just turn everything into entities.