Search Unity

SystemBase and Entities.With EntityQueryDesc

Discussion in 'Entity Component System' started by manpower13, Oct 16, 2020.

  1. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    Hi everyone!

    I'm sorry if some documentation already explains this... I couldn't find it.

    I'm converting one of my
    ComponentSystem 
    systems to
    SystemBase
    . I was wondering how I can specify an 'or' statement inside the Entity query?
    In the
    OnCreate
    method, the following EntityQuery is created:
    Code (CSharp):
    1.  
    2.         var oldParents = new EntityQueryDesc
    3.         {
    4.             None = new ComponentType[] {typeof(Interactable)},
    5.             All = new ComponentType[] {typeof(Tag_HasPopup)}
    6.         };
    7.      
    8.         var deletedParents = new EntityQueryDesc
    9.         {
    10.             All = new ComponentType[] {typeof(DestroyEntity), typeof(Tag_HasPopup)}
    11.         };
    12.      
    13.         _oldPopupParentsQuery = GetEntityQuery(oldParents, deletedParents);
    The
    _oldPopupParentsQuery 
    is used inside the
    OnUpdate
    method:
    Code (CSharp):
    1. Entities.With(_oldPopupParentsQuery).ForEach(.....
    In the new SystemBase, this is not possible anymore. We cannot provide an entityquery in our Entities anymore. Most of my systems could easily be converted to SystemBase by using WithNone, WithAll, WithAny.
    How would one create this where e.g. WithNone<Interactable> is only used if it just has the component Tag_HasPopup?

    I could ofcourse create two Entities.Foreach(), but I was curious whether it would be possible in one statement.

    Thanks in advance!
     
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    No. But instead you use:
    WithStoreEntityQueryInField

    Code (CSharp):
    1.  
    2.     public class NewBehaviourSystem : SystemBase
    3.     {
    4.         EntityQuery Q;      
    5.         protected override void OnUpdate()
    6.         {
    7.             Entities
    8.             .WithStoreEntityQueryInField(ref Q)//this will generate the query before OnCreate
    9.             .ForEach((ref SomeComponent a) =>
    10.             {
    11.                 //DoBehaviour
    12.             }).ScheduleParallel();
    13.         }
    14.     }
    this will code gen Query creation in OnCreateForCompiler which is before OnCreate.
    you don't even need to override OnCreate