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

Bug Can't use NativeHashSet of custom struct

Discussion in 'Entity Component System' started by PublicEnumE, Nov 3, 2022.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    In the following code, I create a NativeHashSet<IntWraper> (IntWrapper being a custom type).

    I then pass that NativeHashSet into a job, and attempt to add an element to it.

    This causes Burst not to compile the job, with an error:


    Burst error BC1001: Unable to access the managed method `object.GetHashCode()` from type `IntWrapper`


    This doesn't happen with base types, like int. Only my custom types, like my wrapper struct. What am I doing wrong, here? Thank you for any advice.

    Code (CSharp):
    1. public struct IntWrapper : IEquatable<IntWrapper>
    2. {
    3.     public int value;
    4.     public bool Equals(IntWrapper other)
    5.     {
    6.         return value.Equals(other.value);
    7.     }
    8. }
    9. [BurstCompile]
    10. public struct IntWrapperJob : IJob
    11. {
    12.     public NativeHashSet<IntWrapper> intWrapperMap;
    13.     public void Execute()
    14.     {
    15.         intWrapperMap.Add(new IntWrapper());
    16.     }
    17. }
    18. [BurstCompile]
    19. public struct IntWrapperSystem : ISystem
    20. {
    21.     private NativeHashSet<IntWrapper> intWrapperMap;
    22.     public void OnCreate(ref SystemState state)
    23.     {
    24.         intWrapperMap = new NativeHashSet<IntWrapper>(10, Allocator.Persistent);
    25.     }
    26.     [BurstCompile]
    27.     public void OnDestroy(ref SystemState state)
    28.     {
    29.         intWrapperMap.Dispose();
    30.     }
    31.     public void OnUpdate(ref SystemState state)
    32.     {
    33.         state.Dependency = new IntWrapperJob
    34.         {
    35.             intWrapperMap = intWrapperMap
    36.         }
    37.         .Schedule(state.Dependency);
    38.     }
    39. }
    40.  
     
  2. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    791
    Try adding this to FooData

    Code (CSharp):
    1.         public override int GetHashCode()
    2.         {
    3.             return foo.GetHashCode();
    4.         }
     
    elliotc-unity and PublicEnumE like this.
  3. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Can't believe I didn't see this. Always right after you post!

    Thank you. :)
     
  4. sinketita

    sinketita

    Joined:
    Jun 30, 2018
    Posts:
    13
    I am sorry for being late, but can you explain what is FooData here?
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    I think it is actually IntWrapper. To use NativeHashMap, your type needs to implement IEquatable AND override GetHashCode().