Search Unity

What's the best way to temporarily map (entityA, entityB) => value?

Discussion in 'Entity Component System' started by Vuh-Hans, Oct 22, 2019.

  1. Vuh-Hans

    Vuh-Hans

    Joined:
    Sep 24, 2013
    Posts:
    42
    Hey, quick question. When you need to map two entity references to a value, what would you say is the best approach? I used to either hash the (entityA, entityB) combination with a NativeHashMap, or if possible index both kinds of entities and use a flattened array of the form [a.index * b.count + b.index] but I was wondering if there's a better way I might be missing.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Best approach is going to be dependent on use case. What's the use case?
     
    sschoener likes this.
  3. Vuh-Hans

    Vuh-Hans

    Joined:
    Sep 24, 2013
    Posts:
    42
    It's a pre-calculation of costs for a hexgrid pathfinder loop, so the resulting map has to be as fast as I can make it when retrieving the values. The cost of an agent moving into a cell is dependent on the cell's terrain and a lot of other dynamic modifiers attached to the agent that I must assume change each frame. The resulting cost is slow to calculate and accesses many entity archetypes, however it can be done in parallel for all agents in the grid, and the results are stored concurrently in a pairings map of the form (terrainEntity, unitEntity) => cost, since each job's key ends up being (or should be, depending on the hashing function I guess) unique. The pathfinder can then quickly retrieve the cost later on using this map. Currently I'm using a NativeHashMap with a struct that holds the two entity references as key type.

    But I think the question can be extended to the general case, where you need to map entity reference pairings in the form (entityA, entityB) => C. That they are entity references gives it some interesting properties, like you know they will always be positive integers. After thinking about it I started to wonder if anyone might have more insight on what's a good way to do this mapping, with a native container or otherwise, or perhaps if anyone has insight on good ways to hash these pairings given what you can expect of entity references, perhaps in a way that avoids collisions altogether?