Search Unity

DynamicBuffer vs Multiple Entity Approach

Discussion in 'Entity Component System' started by Vacummus, Oct 29, 2019.

  1. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I am trying to solve a problem where I want show or hide certain entities when the player enters a room. For example, lets say I have the following 2 rooms, and 3 entities that I wish to show or hide.

    Code (CSharp):
    1. // Rooms
    2. Entity1 = [Room]
    3. Entity2 = [Room]
    4.  
    5. // Entities to toggle Visbility on
    6. Entity3 = [...]
    7. Entity4 = [...]
    8. Entity5 = [...]
    I am using the entity event pattern to detect when the player enters a room. And what I want is to show Entity3 & Entity4 (and hide Entity5) when the player enters the room Entity1, and show Entity5 (and hide Entity3 & Entity4) when the player enters room Entity2.

    I am examining two different approaches for solving this problem. DynamicBuffer vs Multiple Entity Approach (data layout of these approaches shown below). And my question is, which approach would better for this kind of problem? I am trying to wrap my head around when it would be better to use one approach vs the other.

    Code (CSharp):
    1. // Dynamic Buffer Approach
    2. Entity6 = [
    3.   OnRoomEnter { roomEntity = Entity1 },
    4.   ToggleEntityVisibilityBuffer { buffer = [Entity3, Entity4]}
    5. ]
    6. Entity7 = [
    7.   OnRoomEnter { roomEntity = Entity2 },
    8.   ToggleEntityVisibilityBuffer { buffer = [Entity5]}
    9. ]
    Code (CSharp):
    1. // Multiple Entity Approach
    2. Entity6 = [
    3.   OnRoomEnter { roomEntity = Entity1 },
    4.   ToggleEntityVisibility { entity = Entity3 }
    5. ]
    6. Entity7 = [
    7.   OnRoomEnter { roomEntity = Entity1 },
    8.   ToggleEntityVisibility { entity = Entity4 }
    9. ]
    10. Entity8 = [
    11.   OnRoomEnter { roomEntity = Entity2 },
    12.   ToggleEntityVisibility { entity = Entity5 }
    13. ]
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Providing I understood your question, I would go with buffer approach, as it is easier and I think cleaner to debug.
    You can view easily buffers in inspector, so you can see all in one view. No need to flip between entities.
     
    Vacummus likes this.
  3. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    I would do it differently:
    each item has a reference to the room where it is located
    SwitchRoomSystem reacts on PlayerEntersRoom event, iterates on all items, compare item location with the active room, and changes items visibility when necessary

    or

    the room has a buffer of items it contains
    SwitchRoomSystem reacts on PlayerEntersRoom event and does 2 things:
    - gets buffer of items in previously active toom and hides them
    - gets buffer of items in the room being activated and shows them

    or

    you can parent all your items to the room, and they will get be hidden with the room automatically by the render system
     
    Last edited: Oct 30, 2019
    Vacummus likes this.
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    Thanks guys. The approach that made the most sense for me was the DynamicBuffer approach. Mainly because an item can be visible while the player is in more then one room.