Search Unity

Resolved Alternatives to nested NativeList

Discussion in 'Entity Component System' started by charleshendry, Sep 22, 2020.

  1. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    98
    Hi,

    I'm writing a system that's preparing data for instantiating Prefabs at runtime. It's a procedurally generated city. This system receives city block data, e.g. location & extents, block type (commercial, industrial, etc.) which determines allowable prefab types. So it iterates over each city block with an IJobParallelFor. Ideally what I would return for each index (city block) being processed is a NativeList of Prefab IDs and corresponding location/rotation as each block is filled with many buildings.
    I think it was previously possible to use a NativeMultiHashMap that had a NativeList as the value, but I think that was a bug and was removed?
    The other option I can think of is that I only place 1 building on each iteration and update the block dimensions accordingly, i.e. it's reduced in size as it gets filled with buildings over many iterations of the same job, until it block has been "completed". The drawback here is that it's inefficient as I might be performing some calculations/filtering on every iteration which would give identical results each time (not sure I can do that in a previous job as this would resulted in another nested list as an input). And I presume I have to call Complete() on the job after each iteration to determine if more iterations are needed.
    Appreciate any advice people might have on how to best implement this.
     
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
  3. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    98
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    This sounds like a perfect use case for NativeStream.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    What you could try, is to use IJobParallelFor and multihashmap with parallel writing.
    Once job is done, grab unique keys, from the hasmap. Your keys are your unique prefabs IDs, in that particular city block.
     
  6. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Sorry I did not read the first part last time.
    But It sounds like a single MuitiHashMap is already good enough. why do you need a nested list?
    MuitiHashMap Key=[block index] and multiple Values for prefabs
    MuitiHashMap itself can have multiple values for the same key by defualt.
     
  7. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    98
    Didn't realize NativeStream could be used for this. Think I'll go with MuitiHashMap. Thanks for your help.