Search Unity

Question Help understand lifecycle in DOTS 1.0.0

Discussion in 'Entity Component System' started by mackot, Feb 23, 2023.

  1. mackot

    mackot

    Joined:
    Mar 23, 2016
    Posts:
    14
    So for example i had data component for ISystem, which is converted by baking.
    After that i has system which spawns a lot of objects and add to them empty component tag for future system's has single responsibility and can add their needed components for work.
    After that in other system i try to find all created components with tag to add them needed components and in update run some logic with them.

    But i has a lot of problems when try to realize that, i think some of them becouse after read documentation and examples i still doesnt understood lifecycle of DOTS.

    1. I run this code in OnCreate becouse think it should work once, later i discovered what it's just not works and place this logic in OnStartRunning. You can't access baked components OnCreate? Get Singletone for ECB also not works in OnCreate. For what then i should use this Function?
    2. When i try get needed one component of data for ISystem which was created by baking i got error "No suitable code replacement generated, this is either due to generators failing, or lack of support in your current context"
      I try get it like this
      Code (CSharp):
      1.  
      2.         var spawnerHandle = SystemAPI.Query<RefRW<Spawner>>().ElementAt(0);
      Code (CSharp):
      1.  
      2.         var spawnerHandle = SystemAPI.Query<RefRW<Spawner>>().First();
      i know for sure what this Query has this component becouse it runs once for this query in foreach but in foreach you are not allowed to add or remove components becouse it's not thread-safe
      Code (CSharp):
      1.  
      2.         foreach (RefRW<Spawner> spawner in SystemAPI.Query<RefRW<Spawner>>())
      So we can use Entity Comand Buffer finally to spawn and add components and it will be multithreaded, sounds good
    3. But here we go to next problem ECB is works by his own, and when next system start run, even if you place
      Code (CSharp):
      1. [UpdateAfter(typeof(FirstSystem))]
      not a fact what all this objects will be created.
      So what you can do with that

      Place in OnUpdate some bool flag, which checks is this objects has been created, and run some logic for once. But it's bad you can lose some power if you run some hard process on this update and Burst compiler make not optimized code for if checks.

      Delete usage of ECB in first system and get component directly like this, but not sure is it good practise???
      Code (CSharp):
      1.  
      2.         EntityManager em = state.EntityManager;
      3.         var query = state.GetEntityQuery(typeof(Spawner));
      4.         var spawnerHandle = em.GetComponentData<Spawner>(query.ToEntityArray(Allocator.Temp)[0]);
      Write you Observer and create system's, or make system subscribe on callback's from job when it will be completed???
      But i not found how to get callback from them will be helpfull example or describe you own thinks how it should work.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Nope. Because subscenes take time to stream in.
    Creating EntityQueries, special entities, native containers, loading special resources like compute shaders, creating explicit type handles, stuff like that.

    The error has nothing to do with the code you shared being wrong, but the location that code exists being wrong.

    For (3), I don't understand what it is you are trying to do.
     
    elliotc-unity likes this.
  3. mackot

    mackot

    Joined:
    Mar 23, 2016
    Posts:
    14
    I tell at start of the post, 3rd part is
    > After that in other system i try to find all created components with tag to add them needed components and in update run some logic with them.

    Basicly i describe problem what i don't know when Entity comand buffer end his Job with spawning, so i can catch this moment by next system to do logic of that system. Did i need subscribe for some event, or i can plan that lifecycle, like maybe add comand's to same ECB which i somehow sharing between all systems.
    I place some weird workaround's like always catch that moment in update, or just don't use ECB, but want to listen maybe there is a better way
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    EntityCommandBufferSystem is just a system that takes a buffer of commands and does the actual operations on the main thread when it runs. Use the systems view to see when it runs relative to your own systems during a frame.
     
  5. mackot

    mackot

    Joined:
    Mar 23, 2016
    Posts:
    14