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?
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: