Search Unity

What's the correct 2021 way to spawn Tens of Thousands of entities via code? Also rate my plan

Discussion in 'Entity Component System' started by mailfromthewilds, Apr 10, 2021.

  1. mailfromthewilds

    mailfromthewilds

    Joined:
    Jan 31, 2020
    Posts:
    217
    I generally use ConvertToEntity component but it may not be very good idea to have 40000 models in scene (lags scene
    & increases scene size on disk
    & will be loaded into memory on game start regardless if we convert them to entities
    & and then we end with 40000 entities even though I will not need more than 6k in same time)

    So I think its better to create entities dynamically as player gets close to their positions, and DELETE them when player gets far. Thats what I did with MonoBehaviours until I faced lags, but I dont think that idea was bad. In fact id say that idea was good and it helped me run my game smoothly on 60fps with many objects even without DOTS). ive faced lags due to some other system and decided to switch some of my game stuff to DOTS (most importantly world, environment models, land, etc)

    So I think of doing something similar.
    Heres my current plan ( feel free to give opinion ):
    1. player gets close, eg 700 meters distance to planet position
    2. planet loads a simple file from disk (eg scriptable), that contains spawn data(rotations positions etc) for 4000 models (about 4k per planet)
    3. 4000 entities are made via code. entities have rotations, parent (planet), positions, scale, mesh and mesh renderer, collider. its important to me that i parent them first and then set the local pos
    4. file/scriptable is destroyed from memory since entities are already made and we dont need it
    5. player walks around a little bit and decides to walk away, entities are destroyed cus long distance to player
    6. repeat infinitely

    Thoughts?? Also looking for updated code to spawn these thousands of entities from scriptable object data during runtime. According to code monkey video (though its a bit old) i have to use entity manager and create archetype then instantiate it 4k times using nativeArray. though his video was a bit old


    // been reading docs, seems i need to use .CreateEntity, or mb something else? theres also option to supply native array, though not sure how that benefits me
     
    Last edited: Apr 10, 2021
    Haneferd likes this.
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Assuming subscenes don't solve your problem, then instantiating and destroying entities at runtime is a valid solution. A good technique is to have converted prefabs you can instantiate rather than create entities from scratch. I suggest reading the docs for each of the EntityManager overloads to understand what they all do.
     
  4. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    Serializing and Deserializing Entities is a very fast way to instantiate them, the bottleneck currently is that you still need to process all the non-serializeable things and re-create them: Materials, Meshes, ...