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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to get an Entity if you know the Index and version?

Discussion in 'Entity Component System' started by MrKsi, Sep 13, 2022.

  1. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    I need a method that allows me to get an Entity from the EntityManager knowing the Index and Version of the Entity, but I can't find it, please tell me
     
  2. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    Ok I see that there is a Getallentities method however I do not know how to set for its expression to quickly find the type of this:(x => x.Index == Index && x.Version == Version)
     
  3. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    Ok I managed to get this because all entity unique method First helped a lot
    Code (CSharp):
    1. var needEntity = entityes.First(x => x.Index == int.Parse(splitData[0]) && x.Version == int.Parse(splitData[1]));
     
  4. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    944
    Why not instantiate entity struct assign the index and version and then use the instantiated entity struct in the entity manager ?

    Entity e = new Entity() {
    Index = 1,
    Version= 1};

    Entity manager.getComponent<component>(e);
     
  5. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    For my destruction system, I need to find those entities that have the value IsChanged = true on the Fragmentesc component. this value changes when two entities collide with each other, but with CollisionEvent, I have no idea how I can pull out the entity to use it when updating the collider when the wall collapsed, because my character controller uses Rigidbody, and it will be very difficult for me to redo it for DOTS because all my mechanics implemented earlier will fly away, and with this crutch method I get the index and version from the console and search for the entity I need to update the collider.
     
  6. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    481
    Your first three posts are asking how to get a an 'Entity' value, when you already know the index and version. An Entity struct is JUST an index and version. If you know the index and version already, you already have the entity, exactly as WAYN_Games said.

    Imagine having a list of ints, and then asking "I want to retrieve an int from the list, given I already know what the int is". Your third post is equivalent to
    Code (CSharp):
    1. int neededInt = myListOfInts.First(x => x == myInt);
    Hopefully it's clear that this is pointless, because you already have it. You already have the int so...
    Code (CSharp):
    1. int neededInt = myInt
    likewise, the answer to your original question is
    Code (CSharp):
    1. Entity myEntity = new Entity() { Index = myIndex, Version = myVersion }
    Also, if you have the index and version already, why not store it as an Entity in the first place? Perhaps you have a good reason?

    Your last post is a completely different topic. CollisionEvent has EntityA and EntityB. These are the two entities that collided with each other. You need to develop your own logic to determine which one (or both) you need (e.g. by checking for components). You also need to own logic to map this to GameObjects if you need to access a related GameObject.
     
    NoPants_ and Luxxuor like this.
  7. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    1. Entity myEntity = new Entity() { Index = myIndex, Version = myVersion }
      This thing will it create a new entity with the same data or will it replace an existing entity with this data (if any)? Will there be any collisions between the existing entity and the new instance?
     
  8. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    291
    That doesn't have anything to do with entity creation. Manual creation and deletion of entities is only ever done through an EntityManager or through an EntityCommandBuffer as a proxy.

    You may be in gross misunderstanding of what the struct does - it's just an identifier, like a Guid. If you take a look at the Entity struct, you'll see that it's nothing more than a pair of ints, Index and Version. Populating a new instance of Entity as shown is just creating an identifier (similar to Guid.Parse) for use in whatever places you want to store or use an entity identifier, for example in EntityManager operations. Having the index and version of a valid entity is enough to reference that entity in EntityManager.

    How is it that you have the index / version without first having an Entity struct? In virtually every case, an entity is referenced with an instance of the Entity struct.

    Reading material:
    https://docs.unity3d.com/Packages/com.unity.entities@0.51/manual/ecs_entities.html
     
  9. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    134
    Lol so I'm not talking about it, the person says that if I know Index and version I can get an instance like this, is it really so I'm asking and in general I've already solved this question for a long time, you just don't understand me)
     
  10. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    291
     
    Mockarutan, Luxxuor and WAYNGames like this.
  11. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    944
    If your first question found a solution, please explain the solution in the thread for other futur user who may have the same issue.
    And mark the post as resolved.

    If you have another question make a new thread with a detailed description and a explicit title.
    If it's related to physics I would highly recommend posting in the dedicated DOTS Physics subforum.