Search Unity

Bug in logic porting System to Behavior

Discussion in 'Project Tiny' started by vincismurf, Dec 13, 2018.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I have a simple physics update for my system

    Code (JavaScript):
    1. this.world.forEach([game.MovementData, ut.Core2D.TransformLocalPosition, ut.Core2D.TransformLocalRotation],(player, position, rotation)=>
    2.                 {
    3.                     let deltaTime = this.scheduler.deltaTime();
    4.      
    5.                     if(!this.intialized )
    6.                     {
    7.                         this.Initalize();
    8.                     }
    9.                  
    10.                     let originalPosition = position.position;
    11.  
    12.                     rotation.rotation = this.UpdateRotation(deltaTime, rotation.rotation);
    13.  
    14.                    position.position = this.UpdatePositon(deltaTime,player, position.position, rotation.rotation);
    15.    
    16.                    //update Velocity
    17.                    player.currentVelocity = game.PhysicsUtilites.VelocityVector(deltaTime,originalPosition, position.position);
    18.                 });
    Nothing fancy but note the orginialPosition is set by the position.position.

    If I use the same logic in a behavior

    Code (JavaScript):
    1.         OnEntityUpdate():void
    2.         {
    3.                 let rotation = this.data.rotation;
    4.                 let position = this.data.position;
    5.                 let player = this.data.player;
    6.  
    7.                 let deltaTime = this.scheduler.deltaTime();
    8.  
    9.                 let originalPosition = position.position;
    10.  
    11.                 rotation.rotation = this.UpdateRotation(deltaTime, rotation.rotation);
    12.  
    13.                
    14.                position.position = this.UpdatePositon(deltaTime,player, position.position, rotation.rotation);
    15.  
    16.                //update Velocity
    17.                this.data.player.currentVelocity = game.PhysicsUtilites.VelocityVector(deltaTime,originalPosition, position.position);
    18.         }
    The velocity is never updated because it originalPosition takes the values of position.position . . . after it was set. . .ie it is a reference.

    BUT if I do this

    Code (JavaScript):
    1.  
    2. OnEntityUpdate():void
    3. {
    4. let rotation = this.data.rotation;
    5. let position = this.data.position;
    6. let player = this.data.player;
    7.  
    8. let deltaTime = this.scheduler.deltaTime();
    9.  
    10. let originalPosition = new Vector3(position.position.x,position.position.y,position.position.z) ;
    11.  
    12. rotation.rotation = this.UpdateRotation(deltaTime, rotation.rotation);
    13.  
    14. position.position = this.UpdatePositon(deltaTime,player, position.position, rotation.rotation);
    15.  
    16. //update Velocity
    17. this.data.player.currentVelocity = game.PhysicsUtilites.VelocityVector(deltaTime,originalPosition, position.position);
    18.  
    19. }
    20.  
    Creating a new Vector3, initialized with the values of position.position I get my expected behavior.
     
  2. etienne_unity

    etienne_unity

    Unity Technologies

    Joined:
    Aug 31, 2017
    Posts:
    88
    Hi @vincismurf

    This is one of the many reasons why we're moving to C# :)

    In the
    ComponentSystem
    case,
    position
    is a view over data, and the
    position.position
    property (not a field) will return a copy.

    In the
    ComponentBehaviour
    case,
    position
    is not a view, and
    position.position
    will return a JavaScript object reference.

    We could make it a view on the ComponentBehaviour also, but it would be inconsistent with optional filter fields, so we decided to always getComponentData when filling the behaviour filter.

    In C#, structs and the
    ref
    keyword will make all of this easier and more intuitive.
     
    vincismurf likes this.
unityunity