Search Unity

Using Components as Tags

Discussion in 'Data Oriented Technology Stack' started by orionburcham, Dec 20, 2018.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    491
    Hello! Please correct me if I'm wrong, but it seems like the way Archetypes are stored in chunks is at odds with the idea of using Components as tags for Entities.

    For example, if I have 30 identical 'Enemy' Entities, they can all be stored in the same chunk (or multiple identical chunks) and benefit from prefetching, etc.

    But If I were then to frequently add and remove different tag Components from those Entities ("Stunned", "Aggressive", "EnergyResistance", "Inside", etc.) I would quickly fragment my chunk layout in a way that would remove a lot of those benefits.

    I've run into advice from Joachim and other Unity folks recommending this 'tag' Component approach. What am I missing here?

    Thanks for any advice!
     
    Last edited: Dec 20, 2018
  2. sirwhatevers

    sirwhatevers

    Joined:
    Aug 25, 2017
    Posts:
    5
  3. sirwhatevers

    sirwhatevers

    Joined:
    Aug 25, 2017
    Posts:
    5
    I'd also like to hear what the Unity devs thoughts on this are.
     
  4. jooleanlogic

    jooleanlogic

    Joined:
    Mar 1, 2018
    Posts:
    332
    No you're not wrong but it's perhaps not as big an issue as it seems.

    For small games which could be as fragmented as an entity per chunk, performance isn't an issue anyway and at scale, fragmentation dissipates as the ratio of entities to chunks increases.
    Your scenario won't look as fragmented when you have a 1000 "Aggressive" units for example. Even a dozen contiguous components packed into a cache line is mountains better than the shotgunned GameObject memory.

    That's not to say that reducing chunks isn't important or tagging thousands of entities every frame isn't a potential problem, just that it's also a potential pre-optimisation pitfall.
    Tags are an easy default design tool for grouping but there are other strategies you can use like polling or third party entities.

    The beauty of ecs is that you have total control of how you optimise your data. You might use tags for general game behaviours but not for particle systems.
    If you have a hundred unique entities, who cares about tags and chunks, but that 10k particle explosion is sure going to benefit. :)
     
    sirwhatevers likes this.
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,265