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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question [1.0.0Pre65] Aspect and DisabledComponent

Discussion in 'Entity Component System' started by seojihyuk, Mar 26, 2023.

  1. seojihyuk

    seojihyuk

    Joined:
    Sep 19, 2014
    Posts:
    27
    Hello, everyone,
    I want to use aspect to turn on the DisabledComponent as a part of initializing a component.
    When I use aspect inside of IJobEntity, it will automatically detect only enabledComponents.
    If I use something like IgnoreComponentEnabledState, It will detect both DisabledComponent and EnabledComponent. It's not desirable, cause DisabledComponent should be initialized only once right after being instantiated.
    For example, I tried EntityQuery to Query only DisabledComponent and give it to IJobEntity like below.
    But it didn't work.
    Is there way to detect only DisabledComponent with using Aspect?

    Code (CSharp):
    1. [BurstCompile]
    2. public void OnCreate(ref SystemState state)
    3.     {
    4.         random = new Random((uint)UnityEngine.Random.Range(0,int.MaxValue));
    5.         iniLifeQuery = new EntityQueryBuilder(Allocator.Temp)
    6.         .WithDisabled<Life>()
    7.        .Build(ref state);
    8. }
    9.  
    10. [BurstCompile]
    11. public void OnUpdate(ref SystemState state){
    12.              new InitializeLifeJob{
    13.                random = random
    14.              }.ScheduleParallel(iniLifeQuery);
    15. }
    16.  
    17. [BurstCompile]
    18. public partial struct InitializeLifeJob : IJobEntity {
    19.     public Random random;
    20.     public void Execute(ref IniLifeAspect iniLifeAspect)
    21.     {
    22.         iniLifeAspect.InitializeLife(random);
    23.     }
     
  2. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    144
    That's a good question. There have been a lot of improvements to how you can query for specific enabled states, but not sure if querying specifically for a disabled component plus an aspect that uses that enablable component is possible. I remember asking for something similar a while back here.

    Have you tried using both
    IgnoreComponentEnabledState
    AND
    WithDisabled
    ? I know that seems counterintuitive, but I think it might do what you want.

    Another option would be to add
    [Optional]
    to your
    EnabledRefRW<Life>
    field in your aspect. (I think that is supposed to work now. Or it should be supported soon according to this). Then you can just use
    WithDisabled<Life>()
    in your query.

    What I would really like to see is the ability to add a
    [WithDisabled]
    attribute to an
    EnabledRefRW
    field in the aspect so that the caller doesn't have to specific that in the query.