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. Dismiss Notice

Question Spawned entity is not executing a System

Discussion in 'Entity Component System' started by NeoKuro, Aug 16, 2023.

  1. NeoKuro

    NeoKuro

    Joined:
    Jan 30, 2015
    Posts:
    20
    I've managed to spawn an entity (basically an empty game object/prefab that contains an author component - I intend to have the Entity construct itself on spawn)

    I've confirmed that the authoring component is working correctly as I can see all the components on the entity (and the tag)

    In the Systems window, I can see 'EntityCount' for the system in question as '1'

    However when I check that entity's 'Relationships' the system is not listed. The system is also not being executed.

    If I drag the prefab into the subscene in the editor, then hit play. The system DOES execute.

    Is there a step I may be missing after calling ecb.Instantiate() on the entity?


    Here is my spawningSystem code;

    Code (CSharp):
    1.     [BurstCompile]
    2.     public void OnUpdate(ref SystemState state)
    3.     {
    4.         state.Enabled = false;
    5.         EntityCommandBuffer.ParallelWriter ecb = SystemAPI.GetSingleton<BeginInitializationEntityCommandBufferSystem.Singleton>() .CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter();
    6.  
    7.         state.Dependency = new StartingPointSpawnerJob()
    8.         {
    9.             ecb = ecb
    10.         }.ScheduleParallel(state.Dependency);
    11.     }
    12.  
    13.     public partial struct StartingPointSpawnerJob : IJobEntity
    14.     {
    15.         public EntityCommandBuffer.ParallelWriter ecb;
    16.  
    17.         private void Execute([ChunkIndexInQuery] int chunkIndex, in StartingPointProperties startingPointProperties)
    18.         {
    19.             Entity ship = ecb.Instantiate(chunkIndex, startingPointProperties.playerBaseEntity);
    20.         }
    21.     }

    This is my PlayerInitialisationSystem code;

    Code (CSharp):
    1.     [BurstCompile]
    2.     public void OnUpdate(ref SystemState state)
    3.     {
    4.         // All of this executes
    5.         EntityCommandBuffer.ParallelWriter ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter();
    6.  
    7.         state.Dependency = new InitialisePlayerJob()
    8.         {
    9.             ecb = ecb
    10.         }.Schedule(state.Dependency);
    11.     }
    12.  
    13.     [BurstCompile]
    14.     public partial struct InitialisePlayerJob : IJobEntity
    15.     {
    16.         public EntityCommandBuffer.ParallelWriter ecb;
    17.  
    18.         [BurstCompile]
    19.         public void Execute([ChunkIndexInQuery] int sortKey, PlayerClassAspect playerAspect, in PlayerInitialiseTag tag)
    20.         {
    21.             // None of this executes
    22.             if (playerAspect.CheckHasInitialised())
    23.             {
    24.                 return;
    25.             }
    26.  
    27.             // Spawn in a chosen prefab
    28.             // Parent prefab to the base entity
    29.             Entity selectedMesh = playerAspect.GetClassPrefab();
    30.             Entity shipMesh = ecb.Instantiate(sortKey, selectedMesh);
    31.             ecb.AddComponent<Parent>(sortKey, shipMesh, new Parent { Value = playerAspect.self });
    32.             playerAspect.SetHasInitialised();
    33.             ecb.RemoveComponent<PlayerInitialiseTag>(sortKey, playerAspect.self);
    34.         }
    35.     }

    This has stumped me for a few hours now, so any insight would be appreciated.

    Again to confirm, I have confirmed that the entity in question has the 'PlayerClassAspect' as well as the 'PlayerInitialiseTag' but for whatever reason the Job just won't execute
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Check Entity in Hierarchy window, see what components it has.
    Then check system's queries.

    If those match - then it could be an aspect issue. Otherwise, you're probably missing something.
    All fields [components] added to the aspect must be present on the entity in order for the job to iterate on it (unless [Optional] is added).

    I haven't used aspects yet myself, but try adding "ref" to the aspect parameter just in case.
    Or, try manually adding dependencies for the job (either as params or via [WithAll(typeof(SomeTag))] to the job).

    Also, you don't need ECB as ParallelWriter if you don't schedule jobs in parallel. Less code / typing.
     
    Last edited: Aug 17, 2023
  3. NeoKuro

    NeoKuro

    Joined:
    Jan 30, 2015
    Posts:
    20
    Someone on Discord gave the answer in the general Discussion thread. I needed to add a line;

    state.RequireForUpdate<StartingPointProperties>();



    To OnCreate and it fixed everything because I was disabling the system immediately

    Because the Subscene loaded async, the System would run the first OnUpdate (where it disabled itself) but the StartingPointProperties had not yet loaded in the subscene. By the time the subscene had loaded the Onupdate had already ran and disabled itself.

    And the reason there was a 'spawned' Entity with all the correct components and tags but it wasn't executing the Job is because it was the Prefab entity. Prefabs are spawned via 'GetEntity()' calls and are invisible and don't affect the Jobs/Systems etc.

    The above line means that the OnUpdate will wait for the StartingPointProperties to be loaded into the Subscene before it runs.
     
    Antypodish and xVergilx like this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    I am glad that you bring back response from discord into forum. As it would be lost there.

    Any chance for linking reference to discord conversation, for future readers?