Search Unity

Creating Utility Functions Comparing Multiple Component Datas On Specific Entities

Discussion in 'Entity Component System' started by legendlazy, Mar 18, 2019.

  1. legendlazy

    legendlazy

    Joined:
    Jan 23, 2015
    Posts:
    1
    Last month I watched a GDC talk by Timothy Ford explaining some of Overwatch's ECS architecture.

    In his talk he mentions that one thing that helped their team is creating utility functions for operations that they found would be useful being shared between systems.

    The example he uses is CombatUtils.HostileTo. He says that it takes in two entities and returns a boolean which determines whether the two entities are hostile to one another. It does this using three optional components: FilterBits (Team), PetMaster (Unique key which matches each Pet) and Pet.

    It returns false if:
    Either entity doesn't have a FilterBits component.
    If their FilterBits match and they're on the same team.
    If their FilterBits are set to "AlwaysHostile" (Free-for-all) and their Pet/PetMaster pair don't match.

    Otherwise it returns true.

    My question is, with Unity's current implementation of the Entities package, is there a "clean" way of comparing multiple components on an entity like in the example above without having to loop over a bunch of entities in a ComponentGroup to find the entities passed into the function?

    The talk at the timestamp (15m02s) where he goes over their shared behaviour utility functions:
     
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    You can always create a utility method like
    bool HostileTo(Entity a, Entity b)
    . You can use
    GetComponentDataFromEntity() to get access to your needed components like FilterBits and Pet.