Search Unity

Question Error HasComponent when update ECS 0.50 to 1.0

Discussion in 'Entity Component System' started by mati2762, Jun 6, 2023.

  1. mati2762

    mati2762

    Joined:
    Dec 26, 2017
    Posts:
    27
    This is my code who work correctly in ECS 0.50

    Code (CSharp):
    1.  
    2. Entities.ForEach((Entity entity, ref LocalTransform transform, in EnemyFollow enemyFollow) => {
    3.   if (enemyFollow.entity != Entity.Null && HasComponent<LocalTransform>(enemyFollow.entity)) {
    4.         .....
    5.         transform.Rotate = newRotate
    6.  } });
    7.  
    When I update to ECS 1.0 I have this error

    Entities.ForEach cannot use component access method HasComponent with the same type LocalTransform that is used in lambda parameters with write access (as ref).

    Has anyone had this problem before and know how to fix it?
     
  2. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    287
    var translations = SystemAPI.GetComponentLookup<LocalTransform>(true);

    Entities
    .WithNativeDisableContainerSafetyRestriction(translations)
    .ForEach((Entity theUnit, ref LocalTransform trans) =>
    {


    translations.HasComponent(enemyFollow.entity)
     
    mati2762 likes this.
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Cannot read & write same location.

    Use ComponentLookup instead as mentioned, except do not put <T> into EFE query parameters.
    No need to disable safety checks in this case.
     
    mati2762 likes this.
  4. mati2762

    mati2762

    Joined:
    Dec 26, 2017
    Posts:
    27
    Thenks for help,

    I have another question. In my old code I had something like this
    Code (CSharp):
    1.  var buildPhysicWorld = Game.entityManager.World.GetExistingSystem<BuildPhysicsWorld>();
    after upgrading to 1.0, i changed this code to this
    Code (CSharp):
    1. SystemAPI.GetSingleton<PhysicsWorldSingleton>();
    This code snippet is in the MonoBehaviour component.
    I am getting this error, does anyone know how to fix it?
    You may not use the SystemAPI member `GetSingleton` outside of a system. SystemAPI members rely on setup from the containing system.
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    SystemAPI codegen outside of systems is not supported.
    Either put your method inside the system or do it the old way.