[SOLVED] Fire event if I change the pos or rot in editor

I want to detect if an entity changes its position or rotation in the editor and execute a script (manual dragging the arrows or circles). I just need this in the preview so that I don’t need to refresh the preview. Something like this:

this.on("position", function (value) {
        if (value) {
            console.log("move");
        }
    });

Hi @Karl47,

It would be nice to have something like this, though right now those kind of events work only for script attributes. Feel free to add a feature request in engine/editor (not sure where that should live).

In the meantime you can accomplish something like that by keeping track of the last entity position in your update method, and if that has changed fire a custom event of yours:

const currentPos = this.entity.getLocalPosition();

if(this.lastPos.equals(currentPos) === false){
   // do something
}
this.lastPos.copy(currentPos);

Of course that will work only for stationary entities that aren’t moved in gameplay or by physics.

3 Likes

For some reason I can’t make it work. What I did is creating a “checksum” and compare the checksum. Now it works. Thanks a lot for helping out!

UpdatePoint.prototype.initialize = function() {
     this.lastPos = this.entity.getLocalPosition();
    this.lastChecksum = this.lastPos.x+this.lastPos.y+this.lastPos.z;

};

// update code called every frame
UpdatePoint.prototype.update = function(dt) {
    var currentPos = this.entity.getLocalPosition();
    var currentChecksum = currentPos.x+currentPos.y+currentPos.z;
   if(this.lastChecksum !== currentChecksum){
   // do something
        console.log("move");
    }
    this.lastChecksum = currentChecksum;
};
1 Like