Hello, and I hope you guys are having a great day so far. I am currently trying to create a custom class called BasePlatform that moves around the entity attached to the script.
This is because I want to make many different platform types, and I want them to all have this base functionality to extend. However, when I do this I struggle trying to call this.entity.translate(…); within the scope of my BasePlatform class, as the this keyword is different in that scope.
Then to debug and try out different things I discovered that if I try to use this.something to reference the entity that the script is attached to anywhere but within the ScriptName.prototype functions, or as a global variable, it is instead read as undefined.
EX:
var test = this;
function e(){
test.entity.translate(...); //doesn't work
}
Platformz.prototype.initialize = function() {
test.entity.translate(...); //doesn't work
}
function func(){
this.entity.translate(...); //doesn't work
}
//All of these don't work to my understanding, apologies for any faulty testing
…etc.
My code:
class BasePlatform{
constructor(stuff, movement, time, rotation, enemy, doesLoop){
this.movement = movement;
this.rotation = rotation;
this.isEnemy = enemy;
this.timeArray = time;
this.timer = 0;
this.currentIndex = 0;
this.loops = doesLoop;
this.stuff = stuff;
}
updatePlatform(dt){
if (this.currentIndex >= this.timeArray.length){
if (this.doesLoop === true) {
this.currentIndex = 0;
}
else{
return;
}
}
if(dt == 0){
return;
}
this.timer += dt;
//console.error( this.movement[this.currentIndex].x , (dt * this.timeArray[this.currentIndex]) )
console.error(this.stuff.entity.name);
console.error(this.entity.name);
this.stuff.entity.translate(this.movement[this.currentIndex].x / (dt * this.timeArray[this.currentIndex]), this.movement[this.currentIndex].y / (dt * this.timeArray[this.currentIndex]) , this.movement[this.currentIndex].z / (dt * this.timeArray[this.currentIndex]));
if(this.timer >= this.timeArray[currentIndex]) {
this.timer = 0;
this.currentIndex++;
}
}
}
var newPlatform = new BasePlatform(this, [new pc.Vec3(3, 3, 3), new pc.Vec3(-3, -3, -3)], [3, 1], 0, 0, true);
// initialize code called once per entity
Platformz.prototype.initialize = function() {
this.entity.translate(1, 0, 0);
var e = this;
e.entity.translate(0, -10, 0);
console.error(this);
};
This results in stating that it cannot call .translate on undefined. What should I do to fix this?
Sorry for the over-complexity, and thank you for your time.