Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Iterating NativeMultiHashMap in 2019

Discussion in 'Entity Component System' started by james_unity988, Apr 20, 2019.

  1. james_unity988

    james_unity988

    Joined:
    Aug 10, 2018
    Posts:
    71
    Hi all,

    I can't figure out how to iterate a NativeMultiHashMap in 2019. I've seen nothing but old posts on the topic (2011-2015) and nothing seems to work anymore. So, what's the preferred way to do this?

    It would be really nice to see examples of:

    1. Getting all values belonging to a specific key
    2. Iterating each key and corresponding array belonging to each key
    3. Iterating each combination of key and value
     
  2. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    I'm doing like this, I just hope they deliver us a cleaner way...

    Code (CSharp):
    1.  var newStorage = new NativeHashMap<int, MyComponentData>(NewStorageCapacity, Allocator.TempJob);
    2. ...
    3. // Copy changed values to Main Storage
    4. var changedKeys = newStorage.GetKeyArray(Allocator.TempJob);
    5. var changedValues = newStorage.GetValueArray(Allocator.TempJob);
    6. for (int i = 0; i < newStorage.Length; ++i)
    7. {
    8.     var hash = changedKeys[i];
    9.     if (mainStorage.TryGetValue(hash, out ParameterT<T> storedValue))
    10.         mainStorage.Remove(hash);           // TODO: Cant we just replace???
    11.     var newValue = changedValues[i];
    12.     mainStorage.TryAdd(hash, newValue);
    13. }
    14. changedKeys.Dispose();
    15. changedValues.Dispose();
    16.  
     
    Last edited: Apr 20, 2019
  3. james_unity988

    james_unity988

    Joined:
    Aug 10, 2018
    Posts:
    71
    I ended up doing this instead. I'm not sure which one is more horrible...

    Code (CSharp):
    1. for (int key=0; key<numKeys; key++) {
    2.     int value;
    3.     NativeMultiHashMapIterator<int> iterator;
    4.     if (myNativeMultiHashMap.TryGetFirstValue(key, out value, out iterator))
    5.     {
    6.         do
    7.         {
    8.             // Do stuff with value here
    9.         } while (myNativeMultiHashMap.TryGetNextValue(out value, ref iterator));
    10.     }
    11. }
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Use IJobNativeMultiHashMapVisitKeyValue, IJobNativeMultiHashMapVisitKeyMutableValue?
     
    jGate99 and Sarkahn like this.
  5. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    349
    Can you please explain how those two works? Or where can i read about it?
     
    jGate99 likes this.
  6. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    I have same question
     
  7. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Question:
    Imagine a parallel job that for each entity of a certain archetype, must do some kind of OverlapSphere around that entity and store all "interactables" in that sphere for that entity. And then another parallel job that iterates on those "interactables" and chooses the "best" one based on certain rules (proximity, priority, context, etc...)

    Basically, I see two obvious ways to implement this:
    • Parallel write the interactables in NativeMultiHashMaps per Entity, then parallel read them
    • Parallel write the interactables in DynamicBuffers per Entity, then parallel read them
    I wonder which approach would be the most efficient?
     
    Last edited: Nov 15, 2019
    jGate99 likes this.
  8. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    dynamic buffer are basically native arrays attached to entities, a NMHM has overhead due to lookup and atomics

    but if you only need the data only there, can't you directly process the result of the overlap?

    otherwise, there is also NativeStream

    try both and profile btw
     
    jGate99 likes this.
  9. mike1997

    mike1997

    Joined:
    Jul 1, 2016
    Posts:
    25
    Did anyone find any examples on using these 2 jobs, I'm also interested:

    IJobNativeMultiHashMapVisitKeyValue, IJobNativeMultiHashMapVisitKeyMutableValue