Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Archetype question

Discussion in 'Entity Component System' started by Floofloof, Nov 29, 2018.

  1. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    Say I have a bunch of worlds that might have some of the same archetypes in them. If i use one of the EntityManagers from one of the worlds to populate a static Archetype for use in other worlds would that cause any issues,or would that archetype somehow defined by the parameter of the world it was created from?

    Ex.

    public static Archtype ECSTestDataArchetype;

    ECSTestDataArchetype = entityManagerFromRandomWorld.CreateArchetype(typeof(ECSTestData), typeof(ECSTestData2));

    would ECSTestDataArchetype be usable in any/all worlds that have ECSTestData and ECSTestData2 or would I be required to define that archetype for each world?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    I don't actually know if it'll work or not, but it seems like a bad idea even if it does because there's no guarantee it'll continue to work in future.

    Archetype are created using an instance of ArchetypeManager local to EntityManage so as far as I can tell it's not shared between worlds. From a glance I think the biggest issue might be SharedComponentData.

    If you want to keep a static reference to share, why not just keep a ComponentType[] collection.
     
  3. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    had not thought of that. I think that might work better.
    maybe use those to search through all of the already created archetypes in the EnitityManagers Arch list.
     
  4. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    So i decided to do a little refactoring and make some static ComponentType[] that i can use to reference and archetype in an EntityManager. A few things to note
    1. Archetypes are created with an Entity ComponentType under the hood. So any checks in EntityArchetype.ComponentTypes should have that firts
    2. ComponentType order in EntityArchetype.ComponentTypes is not guaranteed for some reason. Some ComponentType seem to swap positions under the hood. meaning no ComponentType[].Equals(ComponentType[]) easy checks
    This was kind of frustrating at first and not really an "optimal" solution but given that the archetypes are retrieved only once on system creation its not too big of a deal for now.
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    I don't really get what you're doing. Why do you need to compare them?

    Why not just create them with the single ComponentType[].

    Code (CSharp):
    1. public static ComponentType[] sharedComponents;
    2.  
    3. var entity = EntityManager.Create(this.sharedComponents);
    Entity CreateEntity(params ComponentType[] types)


    Just creates an archetype under the hood.

    Code (CSharp):
    1.         public Entity CreateEntity(params ComponentType[] types)
    2.         {
    3.             return CreateEntity(CreateArchetype(types));
    4.         }
    and CreateArchetype checks for an existing version before creating it.

    ArchetypeManager.GetExistingArchetype(m_CachedComponentTypeInArchetypeArray, cachedComponentCount);


    So unless you're creating a hell of a lot of entities that a little extra work sorting the types and doing a hash is going to cause an issue, I don't expect just passing in a ComponentType array instead of an archetype will cause any issues*

    This all said, I'm hugely against using static data like this.

    * you'd need to convert it to an Archetype to pass to a job or an entity command buffer.
     
  6. Floofloof

    Floofloof

    Joined:
    Nov 21, 2016
    Posts:
    41
    Probably could have saved some time just looking at the code lol didnt realize you could get the same Archetype if it was already created. Sort of took the "Create" part too literally lol

    Thanks good to know!