Search Unity

Is there a way to read AND write to a Hashmap/List in a ParallelJob?

Discussion in 'Entity Component System' started by dCalle, May 24, 2020.

  1. dCalle

    dCalle

    Joined:
    Dec 16, 2013
    Posts:
    55
    I'm curious, because I'd know if there actually was a functionality, that could do that, because it seems to me I could avoid a lot of pains...

    Code (CSharp):
    1. var translationMap = new NativeHashMap<Entity, Translation>(10, Allocator.TempJob);
    2. var translationWriter = translationMap.AsParallelWriter();
    3. var translationAccess = GetComponentDataFromEntity<Translation>(true);
    4.  
    5. var assignHandle = Entities.WithReadOnly(translationAccess).WithReadOnly(translationMap).ForEach(
    6. (ref ShieldTile tile, ref PhysicsVelocity velocity, in Translation translation, in Entity entity) =>
    7. {
    8. [...]
    9. if (!translationMap.TryGetValue(tile.targetEntity, out var targetTranslation))
    10. {
    11.     targetTranslation = translationAccess[tile.targetEntity];
    12.     translationWriter.TryAdd(tile.targetEntity, targetTranslation);
    13. }
    14. [...]
    15.  
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    No, but what you're doing (which I have no idea) does not need seem like it needs read access

    Code (CSharp):
    1. var targetTranslation = translationAccess[tile.targetEntity];
    2. translationWriter.TryAdd(tile.targetEntity, targetTranslation);
    Alternatively just pre-populate your hash map in a separate job, pretty common pattern.
     
  3. dCalle

    dCalle

    Joined:
    Dec 16, 2013
    Posts:
    55
    Well the only thing I want is to store the values I got from translationAccess, bc I don't want to call it for EVERY tile. Also, if a shieldable has enough Tiles I want it to be removed from the GetTilesCandidate list, which is seperate for every TileSpawner. The Issue here is, I don't know when the Shieldables are full, how many Shieldables are touched by the Spawner and how many Tiles a spawner has ready. I could figure it out. but that would be way more pain to implement than hoping that there might be a read AND write List/HashMap

    EDIT: Alright I think I get it. I really have to think differently. as those values are just being layed out sequentially and any change to that would corrupt the sequence. but damn, I know why people came up with OOP^^ because this is a PITA
     
    Last edited: May 25, 2020