Script attached to persistent node shows undfined

Hello everyone,

I’ve encountered an issue with a persistent node in my project. I’ve created a persistent node in the main menu, and the GameManager script is attached to it. ,
the script appears to be undefined in another scene

I’m seeking assistance with this problem. Could someone please take a look at my repository to
resolve this issue?.
repo : PlayCanvas | HTML5 Game Engine

Thank you

Hi @gouthamamin!

I’m not sure if it is correct what you currently do.

I suggest to add the line below on line 2 of your script.

var persistentNode = null;

And define it on line 10 of your script.

persistentNode = this.entity.clone();

You probably can also instantiate a template instead of cloning an entity of the hierarchy.

Hi @Albertos

var GameManager = pc.createScript('gameManager');

var persistentNode = null;
// initialize code called once per entity
GameManager.prototype.initialize = function () {
    this.sound = false;
    console.log(this.sound)
};

GameManager.prototype.postInitialize = function () {
    persistentNode = this.entity.clone();
    this.app.persistentNode = persistentNode;
    console.log("Game Manager", this.app.persistentNode.script.gameManager)
}

Here this.sound which is set to false is not logged in this case

Sorry, I dont really understand your logic.

What about something like this?

var GameManager = pc.createScript('gameManager');
var persistentNode = null;

// initialize code called once per entity
GameManager.prototype.initialize = function () {
    if (!persistentNode) {
        persistentNode = this.entity.clone();
    }
    
    this.sound = false;

    console.log(this.sound);
    console.log(persistentNode.script.gameManager);
};

I am actualy trying to create one node which is called persistent Node in main Menu scene. Also gameManager script is attched to it with initial this.sound=false; when I load another scene i.e. gameScene. I want to use this persistent node to fetch this.sound and play sound based on its value. I am stuck at this.

var GameManager = pc.createScript('gameManager');

var persistentNode = null;
// initialize code called once per entity
GameManager.prototype.initialize = function () {
    this.sound = false;
    console.log(this.entity.script.gameManager.sound)// false
    console.log(this.entity.clone().script.gameManager.sound); //undefined
};

confused with these 2 logs

You should not use the log of line 8.

I’m more confused about the logs below.

console.log(persistentNode.script.gameManager); // exist
console.log(persistentNode.script.gameManager.sound); // undefined

yes why it is undefined. how can I make it work

Do you want to use it only for sounds?

var GameManager = pc.createScript('gameManager');

var persistentNode = null;

// Method to initialize properties
GameManager.prototype.initProperties = function () {
    this.sound = false;
};

// initialize code called once per entity
GameManager.prototype.initialize = function () {
    // Initialize properties
    this.initProperties();
};

GameManager.prototype.postInitialize = function () {
    if (!persistentNode) {
        persistentNode = this.entity.clone();
        persistentNode.script.gameManager.initProperties(); // Initialize properties of the cloned entity
        this.app.persistentNode = persistentNode;
    }

    // console.log(this.app.persistentNode.script.gameManager)
};

@Albertos this code seems to be working fine.

1 Like