Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Class properties and TypeScript

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

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    For some reason my code is not updating the class property.

    Code (JavaScript):
    1.  
    2. namespace game {
    3.  
    4. /** New System */
    5. export class MovementSystem extends ut.ComponentSystem {
    6.  
    7.      intialized:boolean = false;
    8.      currentSteering : number = 0;
    9.      OnUpdate():void {
    10.           if(!this.intialized )
    11.           {
    12.                this.Initalize();
    13.            }
    14.  
    15.           console.log("steering " + this.currentSteering);
    16.      }
    17.      Initalize()
    18.      {
    19.          let leftButton = this.world.getEntityByName("LeftButton");
    20.          ut.UIControls.UIControlsService.addOnDownCallback(this.world,leftButton, this.OnLeftDownPressed);
    21.          ut.UIControls.UIControlsService.addOnUpCallback(this.world,leftButton, this.OnSteeringReleased);
    22.          this.intialized = true;
    23.      }
    24.  
    25.     OnLeftDownPressed()
    26.     {
    27.          this.currentSteering = 30;
    28.          console.log("OnLeftDownPressed" + this.currentSteering);
    29.      }
    30.  
    31.      OnSteeringReleased()
    32.      {
    33.           console.log("OnSteeringReleased");
    34.          // this.currentSteering = 0;
    35.       }
    36.   }
    37. }
    38.  
    39.  
    Although the value for currentSteering changes when I press the button the log from the Update loop is always 0.

    What am I missing?
     
  2. v_vuk

    v_vuk

    Unity Technologies

    Joined:
    Jul 11, 2017
    Posts:
    36
    Ouch. A bug on our end, with TypeScript/JavaScript method bindings. As a workaround, try this:

    Code (JavaScript):
    1.  
    2. let leftButton = this.world.getEntityByName("LeftButton");
    3. let self = this;
    4. ut.UIControls.UIControlsService.addOnDownCallback(this.world,leftButton, function() { self.OnLeftDownPressed(); });
    5. ut.UIControls.UIControlsService.addOnUpCallback(this.world,leftButton, function() { self.OnSteeringReleased(); });
    6.  
    To preserve the proper object on which to call the pressed/released functions.
     
  3. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Great work around! Thanks