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

Bug IJobEntity using T from class crashes the compiler

Discussion in 'Entity Component System' started by Sylmerria, May 8, 2022.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hello there,

    Is anyone as an idea why this script in an empty project crashes the DOTS compiler ?
    I know it's linked to the use of the parent owner class generic T but I don't know if I forget to add an attribute or do something or it's a real bug.

    Code (CSharp):
    1. using System;
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5.  
    6. public partial class Test<T> : SystemBase where T : unmanaged, IEquatable<T>
    7. {
    8.     /// <inheritdoc />
    9.     protected override void OnUpdate()
    10.     { }
    11.  
    12.     [BurstCompile]
    13.     public partial struct MyJob : IJobEntity
    14.     {
    15.         /// <inheritdoc />
    16.         public void Execute(ref T item)
    17.         {
    18.             Collection.Add(item);
    19.         }
    20.  
    21.         #region Variables
    22.  
    23.         public NativeHashSet<T> Collection;
    24.  
    25.         #endregion
    26.     }
    27. }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Codegen (Entities.ForEach, IJobEntity) doesn't support generics.

    Probably shouldn't crash the editor though.
     
    MNNoxMortem likes this.
  3. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    It seems you are right, I managed to remove the compiler crash when I remove the constraint unmanaged.
    And then when I schedule the IJobEntity I obtain 2 new errors.

    error.PNG

    Code (CSharp):
    1.     public partial class Test : SystemBase //where T : struct, IComponentData, IEquatable<T>
    2.     {
    3.         protected override void OnUpdate()
    4.         {
    5.                 new MyJob<MyComponentData> ().Schedule();
    6.         }
    7.     }
    8.  
    9.     [BurstCompile]
    10.     public partial struct MyJob<T> : IJobEntity where T : MyInterface
    11.     {
    12.         /// <inheritdoc />
    13.         public void Execute(ref T item)
    14.         {
    15.             item.Update();
    16.         }
    17.     }
    18.  
    19.     public struct MyComponentData : MyInterface
    20.     {
    21.         public void Update()
    22.         {
    23.             a = (int)Math.Cos(a);
    24.         }
    25.  
    26.         public int a;
    27.     }
    28.  
    29.     public interface MyInterface : IComponentData
    30.     {
    31.         void Update();
    32.     }
    33.  

    But I'm surprised that a specific error message has not been created for that or even in the documentation I didn't seen anything.
    Do you know if this is a temporary limitation?
     
  4. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    969
    Use chunk iteration jobs if you want generics (IJobEntityBatch or IJobEntityBatchWithIndex). Check here for details.
     
    Sylmerria likes this.
  5. joepl

    joepl

    Unity Technologies

    Joined:
    Jul 6, 2017
    Posts:
    87
    IJobEntity does indeed not support generics currently. This should have an error like Entities.ForEach (I'll add one).

    Note, the ICE error above does not crash the editor. It means that an exception was thrown that we don't properly detect as an error and can provide a decent error message around. Removing the offending code will allow the compiler to continue (and the editor should continue to function).
     
    Occuros, Sylmerria and Harry-Wells like this.
  6. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    It works, thks. It has been a while since I use ComponentTypeHandle ^^

    It means it will be supported in the future then ?
    For 1.0 ?:D
     
  7. Jpritch71

    Jpritch71

    Joined:
    Mar 4, 2014
    Posts:
    11
    Paying close attention to the word "currently" here. Are the plans in the future to support generics with IJobEntity? I can think of several nifty ways to use that if that comes to pass.
     
  8. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    969
    You can already implement those nifty ways by using IJobEntityBatch or IJobEntityBatchWithIndex. Sure they're verbose, but why wait for IJobEntity generics support? They might not come in 1.0 and they might never be supported due to the nature of code gen.
     
    Harry-Wells likes this.
  9. Jpritch71

    Jpritch71

    Joined:
    Mar 4, 2014
    Posts:
    11
    That's exactly what I've been doing, just hoped for some of that extra convenience!
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Just to to add, I checked with ISystem and

    Code (CSharp):
    1. public struct MyJob<T> : IJobChunk where T : unmanaged, IComponentData
    Works.