Issue with Scene Changing and Entity Script Attributes

Description:

I have 2 different scripts in 2 different scenes each with a script attribute that references an entity in that scene. I have a base scene which controls scene swapping by using this.app.scenes.loadSceneHierarchy and then destroying the previously loaded scene hierarchy. The issue I am observing is that in the first scene I enter the script behaves normally, however upon entering the second scene I see that the script in that scene thinks the entity referenced by its script attribute is the entity from the previous room! Oddly enough, there is also an issue where if I start in one room, leave and then come back it will output the correct entity name but it will say the value is undefined. Note that both these script attributes are named differently and are the only entity script attributes in the scene. The same also occurs if the two scripts are the same.

In my eyes what appears to be happening is the first script with an entity script attribute correctly grabs a reference to the locally instanced version of that entity in the room, but then somehow forces that reference onto every script afterwards. The reason it fails after going away and then back again to the same scene you started in is because the reference was to the original scene which has now been destroyed.

Code:

The code is very straight forward.

Scene Changing:

this.currentSceneEntity.destroy();

this.app.scenes.loadSceneHierarchy(scene.url, function (err, sceneRoot) {

sceneRoot.reparent(this.animationEntityIn);
            sceneRoot.setLocalPosition(0,0,0);
            this.currentSceneEntity = sceneRoot;

}.bind(this));

Script 1

This script takes an entity reference and then updates the emissive on that entity every time you send the “ent” event. The value to set the emissive to is sent via the data of that event.

MaterialTest.attributes.add('testEntity', {
    type: 'entity'
});
MaterialTest.prototype.initialize = function() {
    
    this.app.on("ent", this.intensityViaEntity, this);
    
    this.entity.script.on('destroy', function (name, scriptInstance) {
        this.app.off("ent", this);
    }, this);
};
MaterialTest.prototype.intensityViaEntity = function(e) {
    
    if (e !== null && e.data !== undefined && e.data !== null)
    {
        this.intensity = e.data;
        console.log(this.testEntity);
        var o = this.testEntity.model.meshInstances;
        
        for (j = 0; j < o.length; j++){
            o[j].material.emissiveIntensity = this.intensity;
            o[j].material.update();
        } 
    }
    
};

Script 2

The same code as Script 1 except named MaterialTest2 and the script attribute is named testEntity2. (They were previously one script but for testing purposes I split them to see if that had any effect)

Note that the console.log(this.testEntity) is what I’m references when I say the script thinks its entity is the entity from the previous room. I get an error when trying to call this.testEntity.model.meshInstances because “Cannot read property ‘meshInstances’ of undefined”

Thanks so much for the help!

Hey I recently had the same problem there are two ways to fix it the first is the quick and easy way that can cause lag but its just simply make the entity an variable.
the second is to stop the event listeners when you change the scene.

This isn’t quite right, it should be:

this.entity.script.on('destroy', function (name, scriptInstance) {
        this.app.off("ent", this.intensityViaEntity, this);
    }, this);

That fixed my issue! Thank you!