Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Capturing variable

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Aug 26, 2020.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,694
    I seem to run into this challenge often, I want to create an immutable map to use in a lambda. Obviously the lambda requires me to capture the variable and im not sure the most performant way of doing that. I wrote a simple int key as a quick test. Is there a better way of doing this, i don't see another way of doing this. Any help would be appreciated

    var biomeTypeMappings = _biomeTypeMappings; // Capture variable !!


    Code (CSharp):
    1.  
    2.  
    3. // this wil be guid to BiomeTypeMappingsData
    4. private readonly  NativeHashMap<int, BiomeTypeMappingsData> _biomeTypeMappings = new NativeHashMap<int, BiomeTypeMappingsData>(12, Allocator.Persistent)
    5. {
    6.     #region BiomeType
    7.     {
    8.         3111138,// TropicalRainForest
    9.         new BiomeTypeMappingsData(){BiomeTypeIndex = 1}
    10.     },
    11.     {
    12.         9510956,// TropicalDryForest
    13.         new BiomeTypeMappingsData(){BiomeTypeIndex = 2}
    14.     },
    15.     {
    16.     //...
    17.     #endregion
    18. };
    19.  
    Code (CSharp):
    1.  
    2. var biomeTypeMappings = _biomeTypeMappings; // Capture variable !!
    3.  
    4. JobHandle deptBiomeFromTemperatureAndPrecipitation =
    5.     Entities
    6.         .WithReadOnly(biomeTypeMappings)
    7.         .ForEach((
    8.             Entity entity,
    9.             ref TileBiomeComponent tb,
    10.             in TileTemperatureComponent tt,
    11.             in TilePrecipitationComponent tp) =>
    12.         {
    13.  
    14.             // ....
    15.             var isFound = biomeTypeMappings.TryGetValue(key, out BiomeTypeMappingsData biomeType);
    16.             tb.IndexBiome = biomeType.BiomeTypeIndex;
    17.             tb.IndexDataRow = 0;// PlanetTextureDataTableMapping.GetDataRowIndex(key);
    18.         }).ScheduleParallel(Dependency);
    19.  
    20.  
     
    KAV2008 likes this.
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    676
    There is no performance downside on doing that.
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,694
    So no extra copying, its just getting a pointer?
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    676
    Can't tell for sure if there is no extra copying, but they behave as pointers (if you modify the copy it will modify the original). In any case, there is no way to avoid that copying, this is how you would do with IJobChunk anyway.
     
  5. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    I was wondering the same, a bit annoying to have to come up with a slightly different name or add a prefix. When you do this with a native container you're just copying the pointers and the allocator(and the safety stuff if that's enabled).
     
    Last edited: Aug 26, 2020