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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question LocalTransform keeps getting reset before it is applied

Discussion in 'Entity Component System' started by Multiainen, Jul 2, 2023.

  1. Multiainen

    Multiainen

    Joined:
    Oct 9, 2019
    Posts:
    16
    Previously working with the Entities 1.0 beta, I had a simple, working movement script for prefab entities. After updating to Unity 2022.2.20 and Entities 1.0.11 (required by an asset package I added to my project) and updating the syntax, something's gone very awry.

    Code (CSharp):
    1.     [BurstCompile]
    2.     public void OnUpdate(ref SystemState state)
    3.     {
    4.         float time = SystemAPI.Time.DeltaTime;
    5.  
    6.         JobHandle jobHandle = new MoveJob
    7.         {
    8.             time = time
    9.         }.ScheduleParallel(state.Dependency);
    10.         jobHandle.Complete();
    11.     }
    12.  
    13.     [BurstCompile]
    14.     public partial struct MoveJob : IJobEntity
    15.     {
    16.         public float time;
    17.         public void Execute(LocalTransform t, AspectHumonMoving h)
    18.         {
    19.             h.Move(time, t);
    20.         }
    21.     }
    22.  
    23.     public void Move(float deltaTime, LocalTransform t)
    24.     {
    25.         float3 dir = math.normalize(target.ValueRO.value - t.Position);
    26.         t.Position += dir * deltaTime;
    27.     }
    Previously in place of LocalTransform I was using TransformAspect, everything else is the same. Now, the entities don't move, and debugging in Visual Studio shows that the Position value of the LocalTransform appears to be updated in the Move function, but as soon as the function completes and returns to the MoveJob, the value is reset. No other systems are modifying these LocalTransforms. The same issue also persists if I use .Translate or .FromPosition instead of just modifying the Position directly. I also tried setting the Position in the job itself, and directly in the update function of the system, but it still gets reset at some point before the next frame.

    Anyone happen to know what this might be about? I don't have physics or anything else messy in the ECS part of my project, so I'm at a complete loss as to what could be interfering at this point. It's pretty much just this movement system and another conditional one that doesn't initially trigger.
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    291
    LocalTransform is a plain struct component, not a class or an aspect or anything. Structs are passed by value if the parameter isn't decorated with "in," "out," or "ref." Modifications made inside any method e.g. your Move method there only operate on the method-local copy, and badabing badaboom you don't actually affect the component for that entity. You need to pass the LocalTransform parameters for Execute and Move by reference with the ref keyword so that you are actually referring to the entity's component in memory, and not just a copy of it.
    https://docs.unity3d.com/Packages/c...ating-data-ijobentity.html#execute-parameters
     
    Multiainen likes this.
  3. Multiainen

    Multiainen

    Joined:
    Oct 9, 2019
    Posts:
    16
    A thousand thanks, that did it. Not very good yet with ECS since everything keeps changing and, doing a solo project, most of my time goes into other aspects of development, so I keep messing up simple things like this; appreciate you taking the time to explain it.