Enabling/Disabling scripts on Camera

I have two scripts on my camera entity. One is for when the player is at the menu screen and the other for when in the game screen. I am using a GameManager.js instance to toggle this scripts.

at initialize:

  • gameCamera.enabled = false, menuCamera.enabled = true

at play button press:

  • gameCamera.enabled = true, menuCamera.enabled = false
GameManager.attributes.add('camera', { type: 'entity'});
GameManager.prototype.initialize = function () {

    this.InitInstance();

    this.app.on('game:start', this.OnGameStart, this);

    this.camera.script.menuCamera.enabled = true;
    this.camera.script.gameCamera.enabled = false;

};
GameManager.prototype.OnGameStart = function () {
    this.score = 0;
    this.gameState = GameState.Gameplay;

    // disable menuCamera and enable gameCamera
    this.camera.script.menuCamera.enabled = false;
    this.camera.script.gameCamera.enabled = true;


    console.log("Game Started");
}
// MARK: MENU CAMERA SCRIPT
var MenuCamera = pc.createScript('menuCamera');

MenuCamera.attributes.add('xMoveRange', { type: 'number', title: 'xMoveRange' });
MenuCamera.attributes.add('xMoveDur', { type: 'number', title: 'xMoveDur' });

MenuCamera.prototype.initialize = function () {
    this.onEnable();
};

MenuCamera.prototype.startTweening = function () {
    const pos = this.entity.getLocalPosition();
    const targetX = this.originX + this.xMoveRange * this.sign;

    const tween = this.entity.tween(pos)
        .to(new pc.Vec3(targetX, pos.y, pos.z), this.xMoveDur, pc.Linear);

    tween.on('complete', () => {
        this.sign *= -1;       // 1 → -1 → 1 → -1 ...
        this.startTweening();
    });

    tween.start();
};

MenuCamera.prototype.update = function (dt) {
    console.log("menu");
}


MenuCamera.prototype.onEnable = function () {
    console.log("menu: ENABLED");
    
    this.app.addTweenManager();
    this.sign = 1;
    this.originX = this.entity.getLocalPosition().x;
    this.startTweening();
}

MenuCamera.prototype.onDisable = function () {
    console.log("menu: DISABLED");

    this.tween.stop();
    this.entity.setLocalPosition(this.originX, this.entity.getLocalPosition().y, this.entity.getLocalPosition().z);
};
GameCamera.prototype.initialize = function () {

    this.onEnable();

};

GameCamera.prototype.onEnable = function () {
    console.log("game: ENABLED");
}
GameCamera.prototype.onDisable = function () {
    console.log("game: DISABLED");
};

Now, the logs show:

menu: ENABLED
menu (every frame)
Clicked: PlayBtn (called from external script called ButtonInteractions.js)
game: ENABLED
Game Started (called from GameManager.js)
game (every frame)

my question: why are the logs for menuCamera and gameCamera scripts for their onEnable and onDisable scripts not logging? While toggling true/false, their update logs reflect the script enabled state correctly, yet the disabled log does not show up.

Both these scripts are assigned to the camera object in the scene.

You need to listen for the state event on the scriptType when initialize is called Script | Engine API Reference - v2.18.0

Or the enable/disable events

eg

GameCamera.prototype.initialize = function () {
    this.onEnable(); 
    this.on('enable', this.onEnable, this); 
    this.on('disable', this.OnDisable, this);
}; 

GameCamera.prototype.onEnable = function () { 
    console.log("game: ENABLED"); 
}

GameCamera.prototype.onDisable = function () { 
    console.log("game: DISABLED"); 
}; 

yes that works :white_check_mark:
Thanks for your time

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.