[SOLVED] Accessing this.entity in function declared in update?

We don’t know how to see entity within the scope of a function declared in update? Not sure if I’m asking the right question.

var WalkBehavior = pc.createScript('walkBehavior');


WalkBehavior.attributes.add ('moveSpeed', { type: 'number', default: 5 });
WalkBehavior.attributes.add ('playerEntity', { type: 'entity' });

// initialize code called once per entity
WalkBehavior.prototype.initialize = function() {
    this.playerEntity = this.entity;
    console.log(`Ay oh I'mma ${this.playerEntity.name} here`);
        
    // listen for the player:move event
    this.app.on('player:inputDirection', this.onPlayerMove);

    // remove player:move event listeners when script destroyed
    this.on('destroy', function() {
        this.app.off('player:inputDirection', this.onPlayerMove);
    });
};

// update code called every frame
WalkBehavior.prototype.update = function(deltaTime) {
    var onPlayerMove = function(inputDirection) {
        var moveVelocity = new pc.Vec3(inputDirection.x * (this.moveSpeed * deltaTime), 0, inputDirection.y * (this.moveSpeed * deltaTime));
        this.playerEntity.translateLocal(0.1, 0.1, 0.1);
    };
};

WalkBehavior.prototype.onPlayerMove = function() {
    console.log(`Ay yo I'm walking here`);
};

Hi @Aaron_B! Do you mean that you can’t access this inside your function? In that case I would try the way below.

var onPlayerMove = function(inputDirection) {
    var moveVelocity = new pc.Vec3(inputDirection.x * (this.moveSpeed * deltaTime), 0, inputDirection.y * (this.moveSpeed * deltaTime));
    this.playerEntity.translateLocal(0.1, 0.1, 0.1);
}.bind(this));
2 Likes
var WalkBehavior = pc.createScript('walkBehavior');

WalkBehavior.attributes.add ('moveSpeed', { type: 'number', default: 5.0 });  // <-- Default set

// initialize code called once per entity
WalkBehavior.prototype.initialize = function() {
    console.log(`init: ${this.moveSpeed}`); // <-- 5.0
    
    // listen for the player:move event
    this.app.on('player:inputDirection', this.onPlayerMove);

    // remove player:move event listeners when script destroyed
    this.on('destroy', function() {
        this.app.off('player:inputDirection', this.onPlayerMove);
    });
};

WalkBehavior.prototype.onPlayerMove = function(inputDirection, deltaTime) {
    console.log(`moveSpeed: ${moveSpeed}`);  // <-- undefined?
    console.log(`this.moveSpeed: ${this.moveSpeed}`);  // <-- also undefined?
    console.log(`get: ${WalkBehavior.attributes.get('moveSpeed')}`); // <-- also undefined?
};

Sorry, the question I’m trying to ask is, the moveSpeed attribute has been setup and given a default value of 5. Calling it anywhere in the script returns undefined. Why?

You need to use this. to access script data members. So this.moveSpeed for example.

@Chris We are using this.moveSpeed in multiple places. That’s why we are trying to figure out why the function onPlayerMove can’t access it.

Hi @Aaron_B,

It looks like you’re not passing on the scope when you’re subscribing to the player:inputDirection event. You will want to pass the this object along when subscribing to an event like this:

WalkBehavior.prototype.initialize = function() {
...
    this.app.on('player:inputDirection', this.onPlayerMove, this);
...
};

I hope that’s helpful!

1 Like

@eproasim Solved! Thank you, it worked!

1 Like