Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why entity per tile/block is bad idea?

Discussion in 'Entity Component System' started by Zylkowski_a, Jul 10, 2020.

  1. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    Let's consider tile/block based sandbox game. Why it would be bad to use entity per tile/block approach? Everything would be so easy to implement that it seems dumb not to do it.
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    It is a bad idea when generating millions tiles, as creating that many entities will have impact in the performance that you would not have if using a simple HashMap (or something similar). But if it fits your use case and doesn't cause performance issues, then I don't see why not go for it.
     
  3. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    It really depends on your data.

    Does every tile in the game need it's own unique data? A one entity per tile approach is probably the way to go.

    There's a good chance that your sandbox game doesn't really need per tile information for everything.

    If your game has terrain that's composed of many different materials all with the same per tile properties, like sand, different kinds of rock, lava you can save a lot of memory just by having one entity per material type and just using a NativeArray of entities to map those material types to your grid.

    It really depends on what you are trying to achieve.
     
  4. Kmiecis

    Kmiecis

    Joined:
    Oct 11, 2017
    Posts:
    10
    But using Convert to Entity on everything in the scene already creates single entity for each tile that makes up the ground and also each tree, rock or a table and a vase. Otherwise we have GameObject for each of those, which i bet should be less performant.
    So is there any difference? Or should one take another approach in this case?
     
  5. yondercode

    yondercode

    Joined:
    Jun 11, 2018
    Posts:
    27
    I think it's only bad if you have a lot of computation between a block with other blocks positioned around / near it since there's definitely better data structures for those. But unless you have a lot of those kind of operations, I don't think using entity per block approach is a bad idea.
     
  6. Zeffi

    Zeffi

    Joined:
    Nov 18, 2017
    Posts:
    24
    It depends what you want. If you want to use a tile/grid based approach to merely spawn your world, cool, easy. Doing this for a Gamejam right now and it works like a charm. Couldn't be simpler and I just construct my rooms based on maps I quickly write up in Excel.

    If however each entity also needs to reference other neighbours (say to determine if the neighbour is empty so water can flow, or to see if grass should grow, whatever), it gets quickly annoying. VERY annoying.
    I've tried to do that for a falling sand style of game, and it was MAGNITUDES easier and performant to have no entities at all and just evaluate one-dimensional Nativearrays.
    You essentially need each entity to also reference the neighbours so they can properly "talk", and I'd just not recommend that approach. 2D alone already has eight neighbour references to store, and there's 26 in 3D. It's silly.
     
  7. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    If you would go for each tile being a GameObject in normal Unity, so yes, the equivalent would be to each tile being an Entity in ECS Unity.

    We don't know your entire use case, but my suggestion would be to try to keep the way that makes more sense for you and, only if it becomes an issue, then refactor your code when needed.

    The entire point of DOTS is "performance by default", so I wouldn't care with performance until it becomes an issue. Keep your code simple to you.
     
    Krajca and DanSuperGP like this.