Search Unity

How would you solve this system design problem?

Discussion in 'Entity Component System' started by PublicEnumE, Jul 30, 2019.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    I've got a ecs design problem that's been stumping me for a little while. I'm trying to think of an efficient way to do the following:

    1. My game is about giving gifts to children. :p

    2. The game has entities which represent NPC characters. These NPCs are arranged in families. Some are "parents" and some are "children".

    3. Just to be clear, there is no transform child/parent relationships going on here. Parents and Children are separate entities, without any hierarchy connection.

    4. the NPCs in my game also come in different archetypes: each parent or child may have a different collection of tag Components.

    5. My game also includes "gifts". When the game grants a gift, it can be given to multiple children. The game needs to find all the children who qualify to receive each gift. Gift eligibility is determined by a combinations of tags on both the Child entity and it's Parent. For example, a "telescope" gift may be given to all "curious" children who have "cool" parents.

    6. To do that, The game creates a "Gift Command" entity. It contains a Component describing the gift (in this case, "TelescopeGiftData"). It also has a DynamicBuffer, to store all of the children who qualify for this gift. This "Gift Command" also needs to store the tag types, which are requirements of both the Child and it's Parent.

    In this example, the "Gift Command" entity would look like this:

    Telescope Gift Command Entity:
    - TelescopeGiftData <--(the gift itself)
    - DynamicBuffer<TargetChildData> <--(a collection for all qualifying children)
    - Required Child Tags: <--(a collection of required tags for the child)
    - "CuriousTag"​
    - Required Parent Tags: <--(a collection of required tags for the parent)
    - "CoolTag"​
    - - -

    Problem: I need to figure out the best way to do this filtering inside a system.

    - - -

    I believe there must be some clean way to do this filtering from inside of a single JobSystem. But I'm having trouble finding it. Rather than spell out the complicated thing I'm thinking of doing (though I would be glad to do so on request) - How would you tackle this? Many of you on this forum are much better at it than I. :)

    - - -

    A huge amount of gratitude to you for any ideas or advice. :) Thank you very much.
     
    Last edited: Jul 30, 2019
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I always start by solving this outside of ECS. What are the best data structures for the problem. Then I look at does it fit entities well or will I need to make use of other native container types also.

    Unless this is at some massive scale I would brute force it with burst. I would assign a unique integer id to parents and children, and have parent/child pairs as an actual thing. Of the top of my head a NativeMultiHashMap that has an int2 key representing that pair and the values are the tags.

    Then you just iterate over the hash map and determine if the tags for that pair result in a gift.

    At larger scale you would do something like your tag sets have a key and the values are parent/child pairs that have that tagset. Often the values would themselves be an index in this case.

    The simple approach I think fits into ECS with dynamic buffers easily also.