Search Unity

Resolved Future of Aspects

Discussion in 'DOTS Dev Blitz Day 2022 - Q&A' started by scottjdaley, Dec 8, 2022.

  1. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    163
    Hey, I had a few questions regarding the future plans for Aspects. I hope it was okay to include them all in this thread.
    1. Are there any plans to allow multiple aspects in the same query with overlapping components?

      Aspects can include a lot of components which makes it easy to run into situations where multiple aspects both include the same component type in the same job. This results in errors at runtime.

      Now, this might be a code smell and an indication of a code architecture problem, however, it is quite easy to run into even with the new TransformAspect. Specifically, the TransformAspect has a private RefRW<ParentTransform> field that it uses internally. Unlike the other fields, this one has no setter. This means that the job using the TransformAspect has no way to write to this component from another aspect or by directly using the ParentTransform in the job. This wouldn't be a problem if aspects could have overlapping components.

    2. Will SystemAPI.QueryBuilder get support for adding Aspects?

      This would be useful for the rare times that you need to use IJobChunk and manually provide the query. We can get around this by using EntityQueryDesc and IAspect.RequiredComponents, but this isn't burst compatible and requires digging through the aspect codegen to even know it exists.

    3. Will we be able to use IAspect.Lookup inside of IJobEntity?

      Transform.Lookup seems to work fine, but user-created aspects throw an error when you try to add it as a field to the job (
      SGJE0009: MySystem.MyJob contains non-value type field lookup.
      ). This makes it impossible to lookup aspects on arbitrary entities inside of an IJobEntity.

    4. Can we get IAspect.Lookup.HasAspect() and IAspect.Lookup.TryGetAspect()?

      The codegen for IAspect.Lookup provides an indexing operator to lookup the aspect for a specific entity. Can we also get methods to check if an entity has an aspect or try-get it?
    Thank you!
     
  2. StephanieRct_

    StephanieRct_

    Unity Technologies

    Joined:
    Jun 2, 2017
    Posts:
    7
    Hi Scott!
    We've fixed 1 and 2 for our next release (sometime in late January I believe) and both 3 and 4 are currently being fixed/added. I cannot guarantee 3 and 4 will make it to our next release however.

    Let me know if you have any further questions on these!
     
    scottjdaley and Kmsxkuse like this.
  3. scottjdaley

    scottjdaley

    Joined:
    Aug 1, 2013
    Posts:
    163
    Awesome, thank you!

    For 1, what does that mean exactly?
    a) Multiple aspects can be included in a query even if they have an overlapping optional component.
    b) Multiple aspects can be included in a query even if they have an overlapping required component.
    c) An aspect containing a component can be included in a query with the raw component (e.g. LocalTransform + TransformAspect).
    d) All of the above but also for EnabledRefRO/EnabledRefRW fields.
    e) Everything!

    Thanks again!
     
    Last edited: Dec 9, 2022
  4. StephanieRct_

    StephanieRct_

    Unity Technologies

    Joined:
    Jun 2, 2017
    Posts:
    7
    a) yes, if one is optional and the other is required, the component becomes required.
    b) yes. yes EntityQueryBuilder.WithAspect<T>() will not add 2 of the same component type.
    c) yes. Just make sure to call WithAspect<T>() after any WithAll<T>() on your EntityQueryBuilder as WithAll<T>() will add duplicate component type but not WithAspect<T>().
    For instance:
    Code (CSharp):
    1.  
    2. var queryBad = new EntityQueryBuilder(Allocator.Temp).WithAspect<TransformAspect>().WithAll<LocalTransform>().Build();
    3. var queryGood = new EntityQueryBuilder(Allocator.Temp).WithAll<LocalTransform>().WithAspect<TransformAspect>().Build();
    d) yes, DynamicBuffer and SharedComponent as well
    e) also yes :)

    My pleasure!
     
    scottjdaley likes this.
  5. Risho_Team17

    Risho_Team17

    Joined:
    Sep 30, 2020
    Posts:
    5
    Hi,
    Does this mean that the [Optional] attribute is also going to be supported for EnabledRefRW/EnabledRefRO in the next release?

    Thanks
     
  6. StephanieRct_

    StephanieRct_

    Unity Technologies

    Joined:
    Jun 2, 2017
    Posts:
    7
    We cannot guaranty it will make it in the next release but it is indeed in our plans :)
     
    Risho_Team17 and Greexonn like this.
  7. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    Hi. Any news on 4.?
     
    Last edited: May 2, 2023
  8. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
  9. Le_Thai_Thinh

    Le_Thai_Thinh

    Joined:
    Sep 14, 2021
    Posts:
    1
    How can i use Aspect as parameter of IJobEntity Execute method?
     
  10. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    I dont think there is anything to it. You just go
    Code (CSharp):
    1. void Execute(ref MyAspect it) { }
    . Of course you use ref or in depending on wheter you want it to be readonly or not.