Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Storing entities in an array

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

  1. ER_Dolleman

    ER_Dolleman

    Joined:
    Dec 10, 2018
    Posts:
    19
    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.                this.world.destroyEntity(new ut.Entity(entitiesToDestroy[i].index, entitiesToDestroy[i].version));
    3. }
    Is there a better way to do this?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    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

    etienne_unity

    Unity Technologies

    Joined:
    Aug 31, 2017
    Posts:
    102
    @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
    world.forEach
    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
    ut.Entity
    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

    Antypodish

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

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    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?