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


    Sep 19, 2014
    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. }
    10. [BurstCompile]
    11. public void OnUpdate(ref SystemState state){
    12.              new InitializeLifeJob{
    13.                random = random
    14.              }.ScheduleParallel(iniLifeQuery);
    15. }
    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


    Aug 1, 2013
    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
    ? I know that seems counterintuitive, but I think it might do what you want.

    Another option would be to add
    to your
    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
    in your query.

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