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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Bug ArgumentException: Key: -1 is not present in the NativeParallelHashMap

Discussion in 'Graphics for ECS' started by StefanWo, Apr 6, 2023.

  1. StefanWo


    May 24, 2015
    Happens in some cases when i had some problems initializing correct. Happens after i load my savegame, due to a bug in my loading system, due a renamed EntityManager method.

    It looks like the RenderFilterSettings was not added to some of the entities.

    Is it possible to log a warning or ignore broken entities instead of throwing these exceptions? I guess with Burst the complete game would crash away if it stays like that.

    Code (CSharp):
    1. ArgumentException: Key: -1 is not present in the NativeParallelHashMap.
    2. Unity.Collections.NativeParallelHashMap`2[TKey,TValue].ThrowKeyNotPresent (TKey key) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/NativeParallelHashMap.cs:800)
    3. Unity.Collections.NativeParallelHashMap`2[TKey,TValue].get_Item (TKey key) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/NativeParallelHashMap.cs:268)
    4. Unity.Rendering.EmitDrawCommandsJob.Execute (System.Int32 index) (at ./Packages/
    5. Unity.Jobs.IJobParallelForDeferExtensions+JobParallelForDeferProducer`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/Jobs/IJobParallelForDefer.cs:64)
  2. VincentBreysse


    Unity Technologies

    May 31, 2021
    Mhmm, a key equal to -1 is definitely unexpected here. I think it means the type wasn't found in the ECS archetype of the chunk, which shouldn't happen. Since you mentioned initialization issues then it might be related. It would be great to have a ticket with a repro case here so that we can find where the issue stems from and fix things accordingly.

    In the meantime it might help to add a simple check to early out from EmitDrawCommandsJob.Execute if the filterIndex is -1.
  3. StefanWo


    May 24, 2015
    Yes, -1 was not found, the entity didn't have the RenderFilterSettings. Usually it didn't happen, correct. Just mentioned that its maybe better to add it to the query, to ignore these entities or at least log a warning if that happened.

    I guess to reproduce you can try to just remove the RenderFilterSettings from an entity after preparing it to be rendered.

    Here my update that fixes that: (i guess a check for -1 would be also work)
    DrawCommandGenerator.cs -> EmitDrawCommandsJob
    Code (CSharp):
    1. public void Execute(int index) {
    2.             var visibilityItem = VisibilityItems.ElementAt(index);
    4.             var chunk = visibilityItem.Chunk;
    5.             var chunkVisibility = visibilityItem.Visibility;
    7.             int filterIndex = chunk.GetSharedComponentIndex(RenderFilterSettings);
    8.             if (!FilterSettings.ContainsKey(filterIndex)) {
    9.                 Debug.LogError($"No RenderFilterSettings were found ({filterIndex}). Ignoring Rendering for these entities.");
    10.                 return;
    11.             }
    12.             BatchFilterSettings filterSettings = FilterSettings[filterIndex];