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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Storing entities in an array

Discussion in 'Project Tiny' started by ER_Dolleman, Dec 13, 2018.

  1. ER_Dolleman


    Dec 10, 2018
    I was trying to store entities in an array to destroy them later but after some debugging, I found out that their index and version sometimes changes. I either destroy a different entity or get an error because the entity is invalid.

    I did manage to get around the problem by storing the entity data like this:
    Code (JavaScript):
    1. entitiesToDestroy.push({index: entity.index, version: entity.version});
    And later I destroy them like this:
    Code (JavaScript):
    1. for (let i = 0; i < entitiesToDestroy.length; i++) {
    2.       ut.Entity(entitiesToDestroy[i].index, entitiesToDestroy[i].version));
    3. }
    Is there a better way to do this?
  2. Antypodish


    Apr 29, 2014
    You can store in NativeArray, or in entitiy, with BufferArray.
    Entities don't change their indexes / versions, unless you create them.

    So you need take special care, when storing entities references, that if you delete entity itself, you clear that entity from the array.

    I personally would use Native array, with prelocated size (capacity).
    Then when storing entities inside, I just use int, to indicate, how many elements has been used.

    After deleting, set int to 0. Non need to dispose / clear array (unless you do only once in a game), or resize array (unless is too small).
    demo_0 and ER_Dolleman like this.
  3. etienne_unity


    Unity Technologies

    Aug 31, 2017
    @Antypodish Tiny is a different ECS API at the moment. There are no NativeArray or BufferArray utilities. We're working on unifying Tiny and Unity's ECS.

    @elroydolleman_unity When creating entities within a
    callback, you actually create what we call deferred entities. Deferred entities have a temporary negative index.

    You can operate on deferred entities within the outermost forEach scope (add/remove/update component data, take references to them, destroy them, set their name, etc.), but you can't keep a deferred
    object and use it after the outermost forEach scope ends.

    References (EntityReference fields) to deferred entities are "fixed up" to the right indices when they're integrated in the world, when the outermost forEach scope ends.
    Antypodish and ER_Dolleman like this.
  4. Antypodish


    Apr 29, 2014
    My apology. I thought is more similar to ECS. Also, I will try to keep eye more careful on the forum section ;)
  5. deus0


    May 12, 2015
    Hi guys, is there any similar method to translate the entity so I can store it in memory in the parent? Similar to the tiny ecs method translateDeferredEntity?