Z index for animations in battle

Hello! I would like to know if there is an option in RPG Maker MZ to display an animation in battle, but on a layer behind the enemy (Z index). I would like to place some animations that go behind both characters and enemies.

I'm using side-view battles, in case that matters (I mention it because I read some old posts, from 2020, but they were for RPG Maker MV, where they suggested a solution for the issue I mentioned, but only for front-view battles.).

Maybe a plugin or script?

Any ideas? Thanks in advance!
Bump 😛
Here's a plugin to try~
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Animations below battlers.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/172532/
 * @help Give "below" animations a name starting with an underscore.
 * E.g. "_Aura".
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (() => {
'use strict';
  /** Regular expression matching below-battler animation names. */
  const RX = /^_/;  // "starts with _"

  /**
   * @param {$dataAnimation} anim Database animation record to check.
   * @returns {boolean} `true` iff given animation should show below battlers.
   */
  const isBelowAnim = function(anim) {
    const n = anim?.name;
    return n ? RX.test(n) : false;
  };

  /** Identifier for new "below" effects container property on `Spriteset_Battle`. */
  const C2 = "_effectsContainerBelow";

  /** Non-conflicting identifier for get-and-erase method for `store`. */
  const SYM = Symbol();

  /** Private storage for latest value intercepted by {@linkcode fakeContainer}. */
  let store = null;

  /** Interceptor for add child calls. */
  const fakeContainer = Object.freeze({
    [SYM]: function() { const o = store; store = null; return o; },
    addChild: function(o = null) { store = o; }
  });

  // Patch - create "below" effects container.
  void (alias => {
    Spriteset_Battle.prototype.createBattleField = function() {
      const c = this[C2] = new Sprite();
      this._baseSprite.addChild(c);
      alias.apply(this, arguments);
      c.updateTransform = function() {
        Sprite.prototype.updateTransform.apply(this, arguments);
        this.transform.updateTransform(this.parent.parent._battleField.transform);
      };
    };
  })(Spriteset_Battle.prototype.createBattleField);

  // Patch - redirect below-type anim creation calls.
  void (alias => {
    Spriteset_Battle.prototype.createAnimationSprite = function(targets, animation) {
      const c = isBelowAnim(animation) ? this._effectsContainer : null;
      if (c)
        this._effectsContainer = fakeContainer;
      alias.apply(this, arguments);
      if (c) {
        this._effectsContainer = c;
        this[C2].addChild(fakeContainer[SYM]())
      }
    };
  })(Spriteset_Battle.prototype.createAnimationSprite);

  // Patch - also remove from "below" effects container.
  void (alias => {
    Spriteset_Battle.prototype.removeAnimation = function(sprite) {
      if (isBelowAnim(sprite?._animation))
        this[C2].removeChild(sprite);
      alias.apply(this, arguments);
    };
  })(Spriteset_Battle.prototype.removeAnimation);

})();
As mentioned in the help, it uses the animation's database name to decide which layer each animation should play on. You can edit this line to customise that:
JavaScript:
const RX = /^_/;  // "starts with _"
Here's a plugin to try~
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Animations below battlers.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/172532/
 * @help Give "below" animations a name starting with an underscore.
 * E.g. "_Aura".
 *
 * Free to use and/or modify for any project, no credit required.
 */
;void (() => {
'use strict';
  /** Regular expression matching below-battler animation names. */
  const RX = /^_/;  // "starts with _"

  /**
   * @param {$dataAnimation} anim Database animation record to check.
   * @returns {boolean} `true` iff given animation should show below battlers.
   */
  const isBelowAnim = function(anim) {
    const n = anim?.name;
    return n ? RX.test(n) : false;
  };

  /** Identifier for new "below" effects container property on `Spriteset_Battle`. */
  const C2 = "_effectsContainerBelow";

  /** Non-conflicting identifier for get-and-erase method for `store`. */
  const SYM = Symbol();

  /** Private storage for latest value intercepted by {@linkcode fakeContainer}. */
  let store = null;

  /** Interceptor for add child calls. */
  const fakeContainer = Object.freeze({
    [SYM]: function() { const o = store; store = null; return o; },
    addChild: function(o = null) { store = o; }
  });

  // Patch - create "below" effects container.
  void (alias => {
    Spriteset_Battle.prototype.createBattleField = function() {
      const c = this[C2] = new Sprite();
      this._baseSprite.addChild(c);
      alias.apply(this, arguments);
      c.updateTransform = function() {
        Sprite.prototype.updateTransform.apply(this, arguments);
        this.transform.updateTransform(this.parent.parent._battleField.transform);
      };
    };
  })(Spriteset_Battle.prototype.createBattleField);

  // Patch - redirect below-type anim creation calls.
  void (alias => {
    Spriteset_Battle.prototype.createAnimationSprite = function(targets, animation) {
      const c = isBelowAnim(animation) ? this._effectsContainer : null;
      if (c)
        this._effectsContainer = fakeContainer;
      alias.apply(this, arguments);
      if (c) {
        this._effectsContainer = c;
        this[C2].addChild(fakeContainer[SYM]())
      }
    };
  })(Spriteset_Battle.prototype.createAnimationSprite);

  // Patch - also remove from "below" effects container.
  void (alias => {
    Spriteset_Battle.prototype.removeAnimation = function(sprite) {
      if (isBelowAnim(sprite?._animation))
        this[C2].removeChild(sprite);
      alias.apply(this, arguments);
    };
  })(Spriteset_Battle.prototype.removeAnimation);

})();
As mentioned in the help, it uses the animation's database name to decide which layer each animation should play on. You can edit this line to customise that:
JavaScript:
const RX = /^_/;  // "starts with _"
Thank you very much! I can confirm it works in new projects.

Unfortunately, it’s not compatible with VisuStella's Battle Core (as usual... xD ='( )...

If I place your plugin above Battle Core, there’s no error, but the animation doesn’t play behind the fighter (therefore, the plugin's purpose isn’t fulfilled). If I place it below Battle Core, it gives the error with the screenshot I’ve attached.

Any ideas or hope? 😅

I’m leaving the report in case it helps in any way.

TypeError
Cannot read properties of null (reading 'parent')


1728933559494.png
Any ideas or hope
So I wouldn't be surprised if Caethyril was able to finagle something to work - she is a wizard after all.
However, I also wouldn't surprised if she can't, since VS plugins are obfuscated, it would be difficult to make the plugins compatible.
So I wouldn't be surprised if Caethyril was able to finagle something to work - she is a wizard after all.
However, I also wouldn't surprised if she can't, since VS plugins are obfuscated, it would be difficult to make the plugins compatible.
Yeah, I know... I have a love-hate relationship with Visustella because of that xD

I can dream that she hoppefuly can
Unfortunately, it’s not compatible with VisuStella's Battle Core
Ah, see, this is why it's important to mention which plugins you're using. No worries, though, I'm sure someone will find a use for it. 👋

So, the error in your screenshot is being thrown by addChild, via this line:
JavaScript:
        this[C2].addChild(fakeContainer[SYM]())
It expects some object to have a parent property, but that object happens to be null when checked, therefore has no properties. It's related in some way to this[C2], the new "below battlers" animation container sprite, but it isn't that container specifically because otherwise the error would be "cannot read property addChild [of blah]". 😴

From what you said, I'm guessing VisuStella run some mystery extra code that messes up my approach in some way. There are a lot of ways to achieve that, though, and if you've updated your PIXI version (which I suspect you have, based on those pixi.js line/col numbers) then that may also affect things. Yes, even if it works OK with Battle Core turned off. Like @TheAM-Dol said, it'd be a lot of effort to figure out the root cause on this end, largely due to VisuStella's obfuscation.

Battle Core mentions Spriteset_Battle changes, so the incompatibility doesn't surprise me:

Spriteset_Battle Update​

The spriteset now has extra containers to separate battlers (actors and enemies), animations, and damage.
If you're lucky, VisuStella might already have their own implementation for this animation-layering thing. I think they have a Discord server or such for support, so you might try asking there~
Ah, see, this is why it's important to mention which plugins you're using. No worries, though, I'm sure someone will find a use for it. 👋

So, the error in your screenshot is being thrown by addChild, via this line:
JavaScript:
        this[C2].addChild(fakeContainer[SYM]())
It expects some object to have a parent property, but that object happens to be null when checked, therefore has no properties. It's related in some way to this[C2], the new "below battlers" animation container sprite, but it isn't that container specifically because otherwise the error would be "cannot read property addChild [of blah]". 😴

From what you said, I'm guessing VisuStella run some mystery extra code that messes up my approach in some way. There are a lot of ways to achieve that, though, and if you've updated your PIXI version (which I suspect you have, based on those pixi.js line/col numbers) then that may also affect things. Yes, even if it works OK with Battle Core turned off. Like @TheAM-Dol said, it'd be a lot of effort to figure out the root cause on this end, largely due to VisuStella's obfuscation.

Battle Core mentions Spriteset_Battle changes, so the incompatibility doesn't surprise me:
​
If you're lucky, VisuStella might already have their own implementation for this animation-layering thing. I think they have a Discord server or such for support, so you might try asking there~
I understand. Thank you so much for the effort in trying to help me again and for the detailed explanation.

I’m going to do what you suggest and ask in the Visustella Discord =)

Anyway, just as you said, I'm sure this plugin will be helpful to someone, so it's great to know that there's already a solution to that problem =)

Have a good day!! 😀

Original thread: https://forums.rpgmakerweb.com/threads/z-index-for-animations-in-battle.172532/