The issue: When I go from scene A -> scene B -> scene A again, some references become undefined
Caveats / Apology: I cannot share the project due to NDA, I have not been able to make a smaller test version to illustrate the problem because I don’t know exactly what causes it, and I am also using 8thWall XR. I know this makes it harder to debug, I appreciate any insights folks have regardless.
About Scene Loading: I know that it’s additive and not really recommended, I felt I needed to use it as this (will be) a rather large project and I wanted a way to split out the parts. I am using a slightly modified version of the example scene loading project. Code below:
var SwitchScenesOnclick = pc.createScript('switchScenesOnclick');
SwitchScenesOnclick.attributes.add("sceneName", {type: "string", default: "", title: "Scene Name to Load"});
SwitchScenesOnclick.attributes.add("t_LoadingScreen", {type: "asset", assetType: "template", title: "Template for Loading Scene"});
SwitchScenesOnclick.attributes.add("b_TurnOffXR", {type: "boolean", title: "Turn off XR?"});
SwitchScenesOnclick.attributes.add("b_IsFace", {type: "boolean", title: "Turn off Face Effects?"});
SwitchScenesOnclick.attributes.add("s_EventName", {type: "string", default: "touchstart", title: "Name Of Event"});
SwitchScenesOnclick.prototype.initialize = function(dt) {
this.entity.button.on(this.s_EventName, this.loadScene.bind(this));
};
SwitchScenesOnclick.prototype.loadScene = function (e) {
e.stopPropagation();
if(this.b_TurnOffXR){
if(this.b_IsFace){
XR8.PlayCanvas.stopFaceEffects();
} else {
XR8.PlayCanvas.stopXr();
}
}
setTimeout(this.loadHelper.bind(this), 10);
};
SwitchScenesOnclick.prototype.loadHelper = function () {
// Get a reference to the scene's root object
var oldHierarchy = this.app.root.findByName ('Root');
// Get the path to the scene
var scene = this.app.scenes.find(this.sceneName);
var self = this;
// Load the scenes entity hierarchy
this.app.scenes.loadScene(scene.url, function (err, scene) {
if (!err) {
oldHierarchy.destroy();
pc.ComponentSystem.initialize(scene.root);
pc.ComponentSystem.postInitialize(scene.root);
} else {
console.error(err);
}
});
};
Errors: here are the two errors I get depending on the route I go through the game. The first two “errors” on the first screenshot are from console.error to demonstrate which part of the line is undefined.
here is the relevant code snippet:
BackgroundManager.prototype.bindedEndGame = function(n_CoinsCollected, n_CoinsTotal, b_Won) {
var self = this;
if(typeof(n_CoinsCollected) !== "number"){
console.error("Expected n_CoinsCollected to be a number but it was a " + typeof(n_CoinsCollected));
}
if(typeof(n_CoinsTotal) !== "number"){
console.error("Expected n_CoinsTotal to be a number but it was a " + typeof(n_CoinsCollected));
}
if(typeof(b_Won) !== "boolean"){
console.error("Expected b_Won to be a boolean but it was a " + typeof(b_Won));
}
if(b_Won){
//you won
self.e_WinBackground.enabled = true;
} else {
//you lost
self.e_LoseBackground.enabled = true;
}
self.e_EmptyBackground.enabled = true;
self.e_Blackground.enabled = true;
console.error("self.e_CoinsCollectedText: " + self.e_CoinsCollectedText);
console.error("self.e_CoinsCollectedText.element: " + self.e_CoinsCollectedText.element);
console.error("self.e_CoinsCollectedText.element.text: " + self.e_CoinsCollectedText.element.text);
self.e_CoinsCollectedText.element.text = n_CoinsCollected + "/" + n_CoinsTotal;
};
here is the relevant code snippet for this one, i’ve commented the line it’s erroring on:
StarHolder.prototype.setElements = function(self, delta){
self.numCollected += delta;
if(self.numCollected < 0){
self.numCollected = 0;
}
for(var i = 0; i < self.elements.length; i++){
if(i < self.numCollected){
self.elements[i].element.color = self.elements[i].script.pointNotch.onColor; //cannot read pointNotch of undefined
self.elements[i].element.opacity = self.elements[i].script.pointNotch.onColor.a;
} else {
self.elements[i].element.color = self.elements[i].script.pointNotch.offColor;
self.elements[i].element.opacity = self.elements[i].script.pointNotch.offColor.a;
}
}
};
Sorry for the long post, I wanted to make sure to be thorough.
Thanks for your help!