MV Darken battle background during spell

Hey everyone,

I've been playing around in MV again, trying some things out. This request is inspired by SNES games like Chrono Trigger and FF6 when a character casts a spell, or uses a skill, the battle background darkens with what's like a 50% black transparent overlay or is just tinted darker in a similar way. The darkening fades in just prior to the spell animation, and fades back to default tinting when the spell animation is over. It only affects the background, not the sprites or any other graphical elements on the screen.

One idea for implementation would be that this effect could be applied to skills in the database that have the <isDarkened> tag. It's an idea anyway. I know PixiJS does a lot of graphical things, so maybe using that could help?

I HAVE tried using ChatGPT to help me out (don't judge me), as I'm not a programmer. There's been some mild success after hours of tweaking, but only in applying a darkly tinted effect to the background (is just there at the beginning of battle and persists).

If anyone could help me out or point me in the right direction, it would be much appreciated. Thanks!

For reference, here's a thing ChatGPT came up for me:

JavaScript:
Scene_Battle.prototype.createSpriteset = function() {
    this._spriteset = new Spriteset_Battle();
    this.addChild(this._spriteset);

    // Create the darkened background filter
    this._darkenedBackgroundFilter = new PIXI.filters.ColorMatrixFilter();
    this._darkenedBackgroundFilter.matrix = [
        0.5, 0, 0, 0, 0, // Red
        0, 0.5, 0, 0, 0, // Green
        0, 0, 0.5, 0, 0, // Blue
        0, 0, 0, 1, 0    // Alpha
    ];

    // Apply the filter to the battle background
    this._spriteset._battleField.children[0].filters = [this._darkenedBackgroundFilter];

    // Add a property to control fading
    this._darkenedBackgroundFade = {
        targetBrightness: 0.5,
        currentBrightness: 0.5,
        fadeSpeed: 0.02
    };
};

// Add this function at the end of Scene_Battle
Scene_Battle.prototype.updateDarkenedBackground = function() {
    if (this._darkenedBackgroundFilter.enabled) {
        if (this._darkenedBackgroundFade.currentBrightness < this._darkenedBackgroundFade.targetBrightness) {
            this._darkenedBackgroundFade.currentBrightness += this._darkenedBackgroundFade.fadeSpeed;
        } else if (this._darkenedBackgroundFade.currentBrightness > this._darkenedBackgroundFade.targetBrightness) {
            this._darkenedBackgroundFade.currentBrightness -= this._darkenedBackgroundFade.fadeSpeed;
        }

        this._darkenedBackgroundFilter.brightness(this._darkenedBackgroundFade.currentBrightness);
    }
};

// Call this function at the end of updateMain in Scene_Battle
Scene_Battle.prototype.updateDarkenedBackground();

// Modify startAnimation in Sprite_Animation
Sprite_Animation.prototype.startAnimation = function() {
    this._darkenedBackgroundFilter.enabled = true;
    this.isPlaying = true;
};

// Modify update in Sprite_Animation
Sprite_Animation.prototype.update = function() {
    Sprite.prototype.update.call(this);

    if (this.isPlaying && !this.isAnimationPlaying()) {
        this._darkenedBackgroundFilter.enabled = false;
        this.isPlaying = false;
    }
};
ChatGPT isn't a programmer, either.

MV-style animation frames have a built-in "Flash Screen" option. Does that work for your purposes?

Alternatively there's the Tint Screen event command, or corresponding script call. Ideas for timing:
  1. A plugin like YEP Skill Core has notetags to run script calls at certain points during an action, e.g. (untested):

    <Before Eval> $gameScreen.startTint([-128, -128, -128, 0], 30); </Before Eval> <After Eval> $gameScreen.startTint([0, 0, 0, 0], 30); </After Eval>

  2. A plugin like YEP Battle Engine Core has action sequences that can run common events mid-action.
I HAVE tried using ChatGPT to help me out (don't judge me), as I'm not a programmer.
I'm not going to get into heavy judgment, but I don't see how it makes sense to ask ChatGPT (which doesn't really know anything) versus simply posting here in the first place and asking actual people who do know. 😛

I would second caethyril's advice of Yanfly's plugins. The generic Skill Core notetags she provided can be copied and pasted into anything you like, and if you want to do any more advanced effects you'll find value in learning how to use Action Sequences.
Thank you both for the reply! Yeah perhaps ChatGPT telling me it understood JS was enough for me to give it a try, haha.

I believe Tint Screen tints the entire screen, and the Flash Screen is covers the enemy sprites and animations on the screen. I'm looking to have just the battle background be affected.

The script I have in my original post does indeed tint the background itself, but it's persistent; the battle starts with the background tinted and remains so. I'm looking to have the battle background tint dark only when a battle animation begins and reverts to its original non-tinted self when the animation ends, as is done with Robo's Electrocute/Shock tech, seen here.
Ah, I see.

On that action sequences page I linked, there's a BATTLEBACK id OPACITY: n% line documented. Looks like it's added by YEP Improved Battlebacks (paid).

Alternatively, you could try something like this:
Spoiler: plugin code
JavaScript:
/*:
 * @target MV
 * @plugindesc Darkens battlefield during tagged skills.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/165329/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (function() {
'use strict';

    const TAG_NAME = "isDarkened";

    const DIM_OPACITY = 128;

    const DIM_RATE_DOWN = 2;

    const DIM_RATE_UP = DIM_RATE_DOWN;

    const SPRITE_NAME = "_battleFieldDimmer";

    const FLAG_NAME = "_dimBattleField";

    const getTag = function(action) {
        const item = action && action.item();
        return item ? item.meta[TAG_NAME] : null;
    };

    const updateDimmer = function(dimmer) {
        if (BattleManager[FLAG_NAME]) {
            if (dimmer.opacity < DIM_OPACITY)
                dimmer.opacity = Math.min(DIM_OPACITY, dimmer.opacity + DIM_RATE_UP);
        } else if (dimmer.opacity > 0)
            dimmer.opacity -= DIM_RATE_DOWN;
    };

    void (function(alias) {
        BattleManager.startAction = function() {
            alias.apply(this, arguments);
            this[FLAG_NAME] = !!getTag(this._action);
        };
    })(BattleManager.startAction);

    void (function(alias) {
        BattleManager.endAction = function() {
            alias.apply(this, arguments);
            delete this[FLAG_NAME];
        };
    })(BattleManager.endAction);

    void (function(alias) {
        Spriteset_Battle.prototype.createBattleback = function() {
            alias.apply(this, arguments);
            this._battleField.addChild(
                this[SPRITE_NAME] = new ScreenSprite()
            );
        };
    })(Spriteset_Battle.prototype.createBattleback);

    void (function(alias) {
        Spriteset_Battle.prototype.update = function() {
            alias.apply(this, arguments);
            updateDimmer(this[SPRITE_NAME]);
        };
    })(Spriteset_Battle.prototype.update);

})();
Ah, I see.

On that action sequences page I linked, there's a BATTLEBACK id OPACITY: n% line documented. Looks like it's added by YEP Improved Battlebacks (paid).

Alternatively, you could try something like this:
Spoiler: plugin code
JavaScript:
/*:
 * @target MV
 * @plugindesc Darkens battlefield during tagged skills.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/165329/
 * @help Free to use and/or modify for any project, no credit required.
 */
;void (function() {
'use strict';

    const TAG_NAME = "isDarkened";

    const DIM_OPACITY = 128;

    const DIM_RATE_DOWN = 2;

    const DIM_RATE_UP = DIM_RATE_DOWN;

    const SPRITE_NAME = "_battleFieldDimmer";

    const FLAG_NAME = "_dimBattleField";

    const getTag = function(action) {
        const item = action && action.item();
        return item ? item.meta[TAG_NAME] : null;
    };

    const updateDimmer = function(dimmer) {
        if (BattleManager[FLAG_NAME]) {
            if (dimmer.opacity < DIM_OPACITY)
                dimmer.opacity = Math.min(DIM_OPACITY, dimmer.opacity + DIM_RATE_UP);
        } else if (dimmer.opacity > 0)
            dimmer.opacity -= DIM_RATE_DOWN;
    };

    void (function(alias) {
        BattleManager.startAction = function() {
            alias.apply(this, arguments);
            this[FLAG_NAME] = !!getTag(this._action);
        };
    })(BattleManager.startAction);

    void (function(alias) {
        BattleManager.endAction = function() {
            alias.apply(this, arguments);
            delete this[FLAG_NAME];
        };
    })(BattleManager.endAction);

    void (function(alias) {
        Spriteset_Battle.prototype.createBattleback = function() {
            alias.apply(this, arguments);
            this._battleField.addChild(
                this[SPRITE_NAME] = new ScreenSprite()
            );
        };
    })(Spriteset_Battle.prototype.createBattleback);

    void (function(alias) {
        Spriteset_Battle.prototype.update = function() {
            alias.apply(this, arguments);
            updateDimmer(this[SPRITE_NAME]);
        };
    })(Spriteset_Battle.prototype.update);

})();
Hey alright! That works nicely! Thanks so much for taking the time to make this. Very much appreciated! Ah, and the link took me to a non-existent page, so unfortunately I didn't see that detail. (EDIT: Seems like it was a Chrome issue for me). But I'll look into it further, as well!

EDIT: I see this adjusts the opacity rather than tint. Is tinting a battle background quite a process? As I was thinking, it would be great to adjust the tint of the background for various spells rather than a universal darkening. Could use a tag that contains a hex code like <FFFFFF>, or a rgb values like <20,50,255> for the plugin to reference so that certain spells could get specific background tinting, like a nice blue for ice spells, etc.

I'm definitely NOT looking a gift horse in the mouth, here, just spitting out ideas!
(Yanfly's site exists, but doesn't support HTTPS.)

You're welcome! 👋

Edit: I used a ScreenSprite overlay (like Flash Screen) for the dimmer because that's easier to implement "properly", i.e. for both of MV's rendering modes.

You should be able to set the sprite's colour by adding a line like this, e.g. to the update method patch:
JavaScript:
this[SPRITE_NAME].setColor(red, green, blue);
Obviously you'd also need to get the colour values (0~255) from somewhere, e.g. the notetag.
(Yanfly's site exists, but doesn't support HTTPS.)

You're welcome! 👋

Edit: I used a ScreenSprite overlay (like Flash Screen) for the dimmer because that's easier to implement "properly", i.e. for both of MV's rendering modes.

You should be able to set the sprite's colour by adding a line like this, e.g. to the update method patch:
JavaScript:
this[SPRITE_NAME].setColor(red, green, blue);
Obviously you'd also need to get the colour values (0~255) from somewhere, e.g. the notetag.
Awesome! I'll play around with that and see what happens! Thanks!
This may work (quick, untested edit of the code I posted previously):
Spoiler: plugin code v2 (colour)
JavaScript:
/*:
 * @target MV
 * @plugindesc Darkens battlefield during tagged skills.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/165329/
 * @help Adds notetag for skills/items:
 *    <isDarkened: alpha, red, green, blue>
 *        E.g. <isDarkened: 51, 0, 0, 255> - 20% opaque, 100% blue.
 *    <isDarkened: alpha, grey>
 *        Equivalent to <isDarkened: alpha, grey, grey, grey>.
 *    <isDarkened: alpha>
 *        Equivalent to <isDarkened: alpha, 0, 0, 0>.
 *    <isDarkened>
 *        Equivalent to <isDarkened: 128, 0, 0, 0>.
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (function() {
'use strict';

    const TAG_NAME = "isDarkened";

    const DIM_OPACITY = 128;

    const DIM_RATE_DOWN = 2;

    const DIM_RATE_UP = DIM_RATE_DOWN;

    const SPRITE_NAME = "_battleFieldDimmer";

    const FLAG_NAME = "_dimBattleField";

    const getTag = function(action) {
        const item = action && action.item();
        return item ? item.meta[TAG_NAME] : null;
    };

    const DFAULT_COLOUR = Object.freeze([0, 0, 0, DIM_OPACITY]);

    const getColour = function() {
        const tag = getTag(BattleManager._action);
        if (typeof tag === "string") {
            const a = tag.split(",");
            a.length = 4;
            a.push(a.shift());  // argb -> rgba
            for (let n = 0; n < a.length; ++n) {
                a[n] = parseInt(a[n], 10).clamp(0, 255);
                if (!Number.isFinite(a[n]))
                    a[n] = n === 3 ? DIM_OPACITY : a[n-1] || 0;
            }
            return a;
        }
        return DFAULT_COLOUR;
    };

    const updateDimmer = function(dimmer) {
        const c = getColour();
        dimmer.setColor(c[0], c[1], c[2]);
        if (BattleManager[FLAG_NAME]) {
            if (dimmer.opacity < c[3])
                dimmer.opacity = Math.min(c[3], dimmer.opacity + DIM_RATE_UP);
        } else if (dimmer.opacity > 0)
            dimmer.opacity -= DIM_RATE_DOWN;
    };

    void (function(alias) {
        BattleManager.startAction = function() {
            alias.apply(this, arguments);
            this[FLAG_NAME] = !!getTag(this._action);
        };
    })(BattleManager.startAction);

    void (function(alias) {
        BattleManager.endAction = function() {
            alias.apply(this, arguments);
            delete this[FLAG_NAME];
        };
    })(BattleManager.endAction);

    void (function(alias) {
        Spriteset_Battle.prototype.createBattleback = function() {
            alias.apply(this, arguments);
            this._battleField.addChild(
                this[SPRITE_NAME] = new ScreenSprite()
            );
        };
    })(Spriteset_Battle.prototype.createBattleback);

    void (function(alias) {
        Spriteset_Battle.prototype.update = function() {
            alias.apply(this, arguments);
            updateDimmer(this[SPRITE_NAME]);
        };
    })(Spriteset_Battle.prototype.update);

})();
This may work (quick, untested edit of the code I posted previously):
Spoiler: plugin code v2 (colour)
JavaScript:
/*:
 * @target MV
 * @plugindesc Darkens battlefield during tagged skills.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/165329/
 * @help Adds notetag for skills/items:
 *    <isDarkened: alpha, red, green, blue>
 *        E.g. <isDarkened: 51, 0, 0, 255> - 20% opaque, 100% blue.
 *    <isDarkened: alpha, grey>
 *        Equivalent to <isDarkened: alpha, grey, grey, grey>.
 *    <isDarkened: alpha>
 *        Equivalent to <isDarkened: alpha, 0, 0, 0>.
 *    <isDarkened>
 *        Equivalent to <isDarkened: 128, 0, 0, 0>.
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (function() {
'use strict';

    const TAG_NAME = "isDarkened";

    const DIM_OPACITY = 128;

    const DIM_RATE_DOWN = 2;

    const DIM_RATE_UP = DIM_RATE_DOWN;

    const SPRITE_NAME = "_battleFieldDimmer";

    const FLAG_NAME = "_dimBattleField";

    const getTag = function(action) {
        const item = action && action.item();
        return item ? item.meta[TAG_NAME] : null;
    };

    const DFAULT_COLOUR = Object.freeze([0, 0, 0, DIM_OPACITY]);

    const getColour = function() {
        const tag = getTag(BattleManager._action);
        if (typeof tag === "string") {
            const a = tag.split(",");
            a.length = 4;
            a.push(a.shift());  // argb -> rgba
            for (let n = 0; n < a.length; ++n) {
                a[n] = parseInt(a[n], 10).clamp(0, 255);
                if (!Number.isFinite(a[n]))
                    a[n] = n === 3 ? DIM_OPACITY : a[n-1] || 0;
            }
            return a;
        }
        return DFAULT_COLOUR;
    };

    const updateDimmer = function(dimmer) {
        const c = getColour();
        dimmer.setColor(c[0], c[1], c[2]);
        if (BattleManager[FLAG_NAME]) {
            if (dimmer.opacity < c[3])
                dimmer.opacity = Math.min(c[3], dimmer.opacity + DIM_RATE_UP);
        } else if (dimmer.opacity > 0)
            dimmer.opacity -= DIM_RATE_DOWN;
    };

    void (function(alias) {
        BattleManager.startAction = function() {
            alias.apply(this, arguments);
            this[FLAG_NAME] = !!getTag(this._action);
        };
    })(BattleManager.startAction);

    void (function(alias) {
        BattleManager.endAction = function() {
            alias.apply(this, arguments);
            delete this[FLAG_NAME];
        };
    })(BattleManager.endAction);

    void (function(alias) {
        Spriteset_Battle.prototype.createBattleback = function() {
            alias.apply(this, arguments);
            this._battleField.addChild(
                this[SPRITE_NAME] = new ScreenSprite()
            );
        };
    })(Spriteset_Battle.prototype.createBattleback);

    void (function(alias) {
        Spriteset_Battle.prototype.update = function() {
            alias.apply(this, arguments);
            updateDimmer(this[SPRITE_NAME]);
        };
    })(Spriteset_Battle.prototype.update);

})();
Wow, yes that absolutely worked! I was attempting it, scratching my head how to grab values from a tag, looking it up and everything. I don't believe I would have come to this result at all, haha. Thanks so much, caethyril!
If I wanted the darkening to fade out before the damage is displayed on screen, is that a matter of moving a line around?
To change the fade start/end points, you would patch a different method. Currently:
  • It starts fading in on BattleManager.startAction;
  • It starts fading out on BattleManager.endAction.
You could try replacing this part:
JavaScript:
    void (function(alias) {
        BattleManager.endAction = function() {
            alias.apply(this, arguments);
            delete this[FLAG_NAME];
        };
    })(BattleManager.endAction);
...with maybe something like:
JavaScript:
    void (function(alias) {
        Window_BattleLog.prototype.showAnimation = function() {
            alias.apply(this, arguments);
            delete BattleManager[FLAG_NAME];
        };
    })(Window_BattleLog.prototype.showAnimation);
It depends exactly what timing you want for the fade out.
To change the fade start/end points, you would patch a different method. Currently:
  • It starts fading in on BattleManager.startAction;
  • It starts fading out on BattleManager.endAction.
You could try replacing this part:
JavaScript:
    void (function(alias) {
        BattleManager.endAction = function() {
            alias.apply(this, arguments);
            delete this[FLAG_NAME];
        };
    })(BattleManager.endAction);
...with maybe something like:
JavaScript:
    void (function(alias) {
        Window_BattleLog.prototype.showAnimation = function() {
            alias.apply(this, arguments);
            delete BattleManager[FLAG_NAME];
        };
    })(Window_BattleLog.prototype.showAnimation);
It depends exactly what timing you want for the fade out.
Yes! With a quick adjustment, that works! Rather than Window_BattleLog.prototype.showAnimation, I swapped showAnimation for popupDamage and the effect fades as the popup damage is displayed.

Thanks, caethyril, this is a beautiful piece of work! haha
Great, you're welcome! 😂

Original thread: https://forums.rpgmakerweb.com/threads/darken-battle-background-during-spell.165329/