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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

What's the replacement of PostUpdateCommands in SystemBase?

Discussion in 'Entity Component System' started by davenirline, Mar 15, 2020.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    948
    I have systems that add/remove components on update using PostUpdateCommands. However, this is not available in SystemBase. How is this done now?
     
  2. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    With an entity command buffer.
    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. public class YourSystem : SystemBase
    4. {
    5.     private EndSimulationEntityCommandBufferSystem ecbSystem;
    6.  
    7.     protected override void OnCreate()
    8.     {
    9.         ecbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    10.     }
    11.  
    12.     protected override void OnUpdate()
    13.     {
    14.         var ecb = ecbSystem.CreateCommandBuffer().ToConcurrent();
    15.  
    16.         Entities.ForEach((Entity e, int entityInQueryIndex, in YourComponent data) =>
    17.         {
    18.             // Use ecb as appropriate.
    19.         }).ScheduleParallel();
    20.  
    21.         ecbSystem.AddJobHandleForProducer(Dependency);
    22.     }
    23. }
     
    Last edited: Mar 16, 2020
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    948
    I see. Thanks!
     
  4. nogard21

    nogard21

    Joined:
    Mar 20, 2015
    Posts:
    5
    But this sync point is executed at the end of the frame, if I want to playback after the some system OnUpdate() ?
     
  5. apaxnid

    apaxnid

    Joined:
    Nov 18, 2012
    Posts:
    34
    Code (CSharp):
    1. using Unity.Entities;
    2. public class YourSystem : SystemBase
    3. {
    4.  
    5.     protected override void OnUpdate()
    6.     {
    7.         var ecb = new EntityCommandBuffer(Allocator.TempJob);
    8.  
    9.         Entities.ForEach((Entity e, in YourComponent data) =>
    10.         {
    11.             // Use ecb as appropriate.
    12.         }).Run();
    13.  
    14.         ecb.Playback(EntityManager);
    15.     }
    16. }
     
    Orimay likes this.
  6. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    ECB Systems are not just executed at the end of a frame. They are just like any other system, and will be added to the update order based on their [UpdateBefore], [UpdateAfter], and [UpdateInGroup] attributes.
     
    Orimay likes this.
  7. nogard21

    nogard21

    Joined:
    Mar 20, 2015
    Posts:
    5
    Thanks!
     
  8. nogard21

    nogard21

    Joined:
    Mar 20, 2015
    Posts:
    5
    Thanks!I just need to implement my ecb system and add [UpdateAfter (typeof (xxxSystem))]?
     
  9. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Yes, that will work. If you have multiple other systems issuing commands to the ECB system, it might make more sense to position them to [UpdateBefore] the ECB. Whatever makes sense for your setup. :)
     
  10. nogard21

    nogard21

    Joined:
    Mar 20, 2015
    Posts:
    5
    Thank you so much!
     
    PublicEnumE likes this.