Search Unity

Safely creating, destroying and altering entities at anytime

Discussion in 'Entity Component System' started by Spy-Shifty, Apr 30, 2018.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Hi,

    it would be nice to have a mechanism to safely create, destroy or alter entities at anytime from anywhere!

    Well, we have the construct of EntityCommandBuffer, but we can only use it inside of systems! (As far as I know)


    Now I have the following situation:
    I have a NetworkManage (Photon Realtime) which receives messages. To read data from buffer it requires to call an update method on the NetworkManager. The update method is called by MonoBehaviour,FixedUpdate()...

    Now, when I receive data I need to create, destroy or alter entities. But the problem is, I don't have access to an EntityCommandBuffer...
    And do this directly on the EntityManager throws me some exceptions...


    Maybe I do something wrong...
     
  2. xdissent

    xdissent

    Joined:
    Apr 6, 2018
    Posts:
    3
    You could create a system that's constrained to FixedUpdate:

    Code (CSharp):
    1. [UpdateAfter(typeof(UnityEngine.Experimental.PlayerLoop.FixedUpdate.ScriptRunBehaviourFixedUpdate))]
    2. [UpdateBefore(typeof(UnityEngine.Experimental.PlayerLoop.FixedUpdate.DirectorFixedUpdate))]
    3. public class NetworkMsgSystem : ComponentSystem
    4. {
    5.   protected override void OnUpdate()
    6.   {
    7.     // add entities to PostUpdateCommands
    8.   }
    9. }
    or with jobs and a barrier system:

    Code (CSharp):
    1. [UpdateAfter(typeof(UnityEngine.Experimental.PlayerLoop.FixedUpdate.ScriptRunBehaviourFixedUpdate))]
    2. [UpdateBefore(typeof(UnityEngine.Experimental.PlayerLoop.FixedUpdate.DirectorFixedUpdate))]
    3. public class NetworkMsgBarrier : BarrierSystem { }
    4.  
    5. [UpdateAfter(typeof(EndFrameBarrier))]
    6. [UpdateBefore(typeof(NetworkMsgBarrier))]
    7. public class NetworkMsgSystem : JobComponentSystem
    8. {
    9.   protected override void OnUpdate()
    10.   {
    11.     // add entities via job, using NetworkMsgBarrier.CreateCommandBuffer()
    12.   }
    13. }
     
  3. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I realized that the error message I get comes from my NetworkSyncSystem :rolleyes:
    I tried to access a component of an entity which doesn't exist yet... :D

    Anyway I like your idea with the barrier!
    Thank you