Search Unity

How are people handling View with hybrid?

Discussion in 'Entity Component System' started by BanJaxe, Nov 2, 2018.

  1. BanJaxe

    BanJaxe

    Joined:
    Nov 20, 2017
    Posts:
    47
    Is there a good pattern to construct views of entities in hybrid. i.e. so you have an entity model simulation of the game and then some GameObjects that act as the view.

    GameObjectEntity is really awkward to work with when dynamically constructing gameobjects/entities at runtime, every time you add/remove/change components you have to update both gameobject and entity. You can toggle the gameobject active on and off to trigger GameObjectEntity to update but this feels like a hack?

    Is there a simple pattern I'm missing?
     
    Last edited: Nov 2, 2018
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    just handle everything manually in a system.

    eg. before FixedUpdate -> copy data to transform/collider/rigidbody and add forces, after FixedUpdate -> copy the transform back to ECS
     
  3. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    - I use a pool of gameobjects with meshRenderers, with a good old integer ID that matches the index in the pool array, + the reactive pattern so I can just destroy entities and have the ID returned to the pool automatically.

    Code (CSharp):
    1. internal class MonoView : MonoBehaviour
    2. {
    3.     public int ID;
    4.     public MeshFilter MeshFilter;
    5.     public MeshCollider MeshCollider;
    6.     public MeshRenderer MeshRenderer;
    7.     public Mesh mesh;
    On entities I have a bindingState SystemStateComponentData that links the entity to the mono view
    Code (CSharp):
    1. internal struct BindingState : ISystemStateComponentData
    2.     {
    3.         public readonly int ChunkID;
    4.         public BindingState(int iD)
    5.         {
    6.           MonoID = iD;
    7.         }
    8.     }
    I have a reactive ComponentSystem (so on main thread) that
    - initialize BindingState.MonoID to -1 when an entity is created
    - returns the ID to the pool when the entity is destroyed ( that's the reason to use ISystemStateComponentData, it allows processing destroyed entities)

    When i need to render, I take a MonoView from the pool and set the BindingState Id in the entity.
    If I want to destroy the entity, I just destroy it.

    If I want to release the MonoView but keep the entity , I set manually bindingState.Id to -1 and return the id to the pool (on main thread). I could detect changes on binding state in my reactive system but it's not worth the trouble for me as I release the ID without destroying the entity only at one place in my codebase.

    Cheers
     
  4. BanJaxe

    BanJaxe

    Joined:
    Nov 20, 2017
    Posts:
    47
    Thanks for the replies, I'll try the suggested ideas.
     
  5. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    224
    this is too frequent - i'm pusing/pulling data between ecs and gameobjects only when something has changed