Search Unity

Question Per-System Entities, How do you get them?

Discussion in 'Entity Component System' started by Kamadake, Nov 8, 2022.

  1. Kamadake

    Kamadake

    Joined:
    Oct 12, 2013
    Posts:
    12
    Hey there! I have a question in regards to these per-system entities that was introduced with experimental 1.0, it does make sense to work with these system entities but we're not sure about the means of getting these entities.

    We saw there is the "UniversalQueryWithSystems" method but this looks to be heavy. I wanted to see if someone found some cleaner way to asking for these per-system entities. I saw that the entity is stored in the SystemHandle instance. And I'm also not sure of what's happening here with SystemInstance

    Code (CSharp):
    1. var systemComponent = ComponentType.ReadWrite<SystemInstance>();
    2. Entity systemEntity = self.EntityManager.CreateEntity(self.EntityManager.CreateArchetype(&systemComponent, 1));
    Thanks in advance!
     
  2. steffy2

    steffy2

    Joined:
    Oct 28, 2015
    Posts:
    5
    I also was trying to figure out how to use "system entities".

    There is a mention in entities 1.0 changelog:
    1. I can use
      EntityManager.UniversalQueryWithSystems
      but it returns a lot of unnecessary entities, not only system entities.

    2. I can create my own custom query
      new EntityQueryBuilder(Allocator.Temp).WithOptions(EntityQueryOptions.IncludeSystems)
      but it returns the same.

    3. I can get the system entity using C# reflection:
      Code (CSharp):
      1. SystemHandle handle = EntityManager.World.GetExistingSystem<MySystem>();
      2. Entity systemEntity = (Entity) typeof(SystemHandle).GetField("m_Entity", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(handle)!;
      3. Debug.Log( $"systemEntity: {systemEntity.Index}" );
      but I don't feel this is appropriate way :)
    So, how should we use it?
     
  3. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You aren't really meant to get them I guess. Instead you should kind of pretend the entity doesn't actually exist and instead you are just adding or removing data (components) to the system itself.

    As Luxxuor said you can use the system handle overloads to achieve this.
     
    redwren likes this.
  5. yifanchu183

    yifanchu183

    Joined:
    Jul 4, 2019
    Posts:
    41
    but addBuffer require an entity, systemHandle is not useful in this case.
     
  6. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    Also seems like some of the command-buffer methods dont have sytem-handle overloads, such as RemoveComponent.
     
  7. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I have stumbled upon the same question. If adding componentdata to system is preferred way of storing per system data, then we should have AddBuffer accepting the systemhandle?

    GetBuffer accepts systemhandle, but AddBuffer does not. Makes no sense?
     
    metageist likes this.