Search Unity

Instancing 100,000 entities, how to set up an IJobParallelFor to set them up

Discussion in 'Entity Component System' started by lemmy101, May 4, 2018.

  1. lemmy101

    lemmy101

    Joined:
    Apr 10, 2014
    Posts:
    6
    Hey all, trying to get my head around ECS, and am using the StressTest demo and trying to piece together my own system to test. I'm creating a galaxy with 100,000 stars, which I want to place randomly as per a probability mask. I've watched all the talks too, and get the principle of ECS but am suffering a bit of mind bending when actually delving into writing code.

    So I have a prefab GameObject that contains a wrapped StarPosition IDataComponent, and I'm instancing it as a prefab entity, then instansiating that 100,000 times. Entity Debugger shows them. Great!

    Problem is the demo doesn't then call any initialize jobs on the instanced objects for me to use as example, since there are much fewer objects to deal with in a formation where the spawn code exists. Since I have such a high amount of stars to do at once, I'd like to do all my initialisation multithreaded via jobs. However if I try and create a job here to schedule it, I don't have the [Inject] struct deal thing to hunt out my entities to pass into the initializer, so I presumably have to create a new system class to look up these entities and create the job there?

    From what I can tell though, the JobComponentSystem systems etc that I could look up all the entities via the datacomponent archetypes will update every frame via OnUpdate. Unlike a system that is moving objects or processing logic on them, I literally just want to lookup a
    ComponentDataArray of all my stars, schedule the parallel job to deal with that array the once then never again, since the entities are persistent and static after that point. Is there either a way to get this, or a better way to organize my spawning so that I get this information (I can't seem to manually create one), without sacrificing instantiating speed?

    Thanks!

    Chris
     
    Last edited: May 4, 2018
  2. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Not sure if I fully understand what you want to do. But you might want to check the documentation for batch api of entity manager.

    You can initialize their components at creation if you don’t want to use a system (ie static position)
     
  3. lemmy101

    lemmy101

    Joined:
    Apr 10, 2014
    Posts:
    6
    Where is this documentation? I'm not sure this batch stuff is what I'm looking for, but I've not found a single piece of documentation outside of the github examples and the talks, so a link would be super appreciated.

    > You can initialize their components at creation if you don’t want to use a system (ie static position)

    I am doing a lot of expensive work on intitializing stars. I need to do a ton of random rolls to find a suitable place amongst a galaxy using a greyscale probability map in the shape of a spiral galaxy, throwing out rolls that appear in the void between arms etc, as well as a ton more initialization stuff to do with the star. If I do this stuff sequentially in the main thread for 100,000 stars it will take ages. The entire idea of the Jobs system is to be able to do 'jobs' on data in a multithreaded way so that's what I'm hoping to do.

    parallel jobs operating on all the IComponentData elements of every entity that has them a StarPosition needs a ComponentDataArray<StarPosition> array to work on with the index passed to them. I have no way of obtaining this array without creating a system that has the ComponentGroup struct and inject and all that to do the search. That's the way everything else seems to work, but I can't search for something that's not yet created in the system that I'm about to create IN the system, so at best this would require a second system once they are instantiated.

    And if I do that, then I have no seeming control over this system's creation and first update, meaning it may process before I've created the stars, and once its running can't stop it, so once the stars are there it'll just call OnUpdate repeatedly and initialize my 100,000 stars every frame. Unless I CAN stop it and control its processing but I've been unable to find this.

    All I want to do is:

    Create 100,000 star entities with position components
    run parallel job on those 100,000 star entities position components

    then move on to frame by frame stuff which I understand

    I just want a way to be able to call a parallel job on all the entities once for initialization which seems a fundamental thing one might want to do, so I must be missing something because I simply can't figure out how to do it without perhaps some monumental hacks and race conditions and considering the main point of doing this is to learn how to use it properly hacking around it doesn't seem like the best thing to do.

    Thanks!
     
    Last edited: May 4, 2018
  4. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    https://github.com/Unity-Technologi...master/Documentation/content/ecs_in_detail.md

    // Instantiate 500 Entities and write the resulting Entity IDs to the instances array
    var instances = new NativeArray<Entity>(500, Allocator.Temp);
    EntityManager.Instantiate(entity, instances);

    You can use jobs without ecs, you do not need a system to run a job, ie you can run the job directly after you created your entities.

    If you want to use a system to only run once, just add a tag component to the entities in question and remove it after initialization - then the system would not run again

    It is late here, maybe I did not understand you issue fully...
     
  5. lemmy101

    lemmy101

    Joined:
    Apr 10, 2014
    Posts:
    6
    > If you want to use a system to only run once, just add a tag component to the entities in question and remove it after initialization - then the system would not run again

    Thanks this may help!

    Cheers for the documentation too, sorry if I've been a little flappy been banging my head off this for a long while. :D
     
    Last edited: May 4, 2018