MZ Modify Cae's Picture Z Container Plugin to allow only a specific range of Pictures to be changed?

So, it's pretty well-known that pictures have a Z axis, and depending on the value of the z AND something called 'Containers', pictures could either show above something like weather effects or below Event sprites.

Changing that priority value isn't available by default. However, @caethyril has a a short and sweet plugin that allows this to be changed!
JavaScript:
void (alias => {
  Spriteset_Map.prototype.createPictures = function() {
    alias.apply(this, arguments);
    const p = this._pictureContainer;
    this.removeChild(p);
    this._tilemap.addChild(p);
    p.z = 0.5;
  };
})(Spriteset_Map.prototype.createPictures);
(https://forums.rpgmakerweb.com/index.php?threads/showpicture-anchor-z-change.152676/#post-1310980)
Spoiler: For reference, here are the default z-values, from Tilemap#_createLayers (rmmz_core.js):
DEFAULT TILEMAP CHILD Z VALUES
0 : Lower tiles
1 : Lower characters
3 : Normal characters
4 : Upper tiles
5 : Upper characters
6 : Airship shadow
7 : Balloon
8 : Animation
9 : Destination

BUT, unfortunately, this only allows you to set the Z Layer of ALL Pictures. Cae even said in the plugin description,
"If you want different z-values for different pictures, you'll need some extra structure."
and oh god I need that structure right now.

I've got like 15 HUD pictures that I want to stay on top of the weather and character sprites, so
p.z = 11;
works great, BUT
I've also got some enviromental level animation pictures (waterfall, trashcan tipping over, dumpster open close etc) that I definitely need to be below the weather and below the Player character Sprite. so
p.z = 2;
is perfect,
But as you can see the problem is it's only capable of changing the z for ALL pictures, which is not great for my purposes...
-----
So, would it be possible to add something like this to the plugin?
"if picture ID is less than 80, then set p.z = 2;
ELSE: p.z = 11;"
???

I know another person was able to do something similar for MV because they talked about coding the Z/container change based on picture ID# range in this forum post here:
But it was only referenced briefly, and the way they described the sprite.js seemed to be of a different structure from MZ...

Also, I know that Galv Layers is a thing, and I am using it! But I'm specifically trying to use this for some smaller objects on the map that have like 5 frames of animation and/or can be collected and removed,
(show picture 33 as A.png -Wait 8 frames -show picture 33 as B.png -Wait 8 frames etc etc )
and setting this up with pictures is just so much easier.



I hope this request makes sense. Thanks to anyone that might have an idea of what to add to this plugin!
I was like "I have a plugin for that?" Then I saw the thread. 😴

Technically, display order is determined by the relative child order. By default, picture sprites do not have a z-value:
The z property is a custom sort mechanism used by the _compareChildOrder method of Tilemap (rmmz_core.js).
I.e. Tilemap checks each of its children for a z property and reorders them based on that. So it only really becomes relevant when making a display object (e.g. _pictureContainer) a child of the tilemap. Normally the picture container is a child of the spriteset: the first such child of the "upper layer" category, vs the tilemap, which is a "lower layer" child. That's why it normally displays on top of all tiles.

Usually you can get the sprite for a given picture ID from the spriteset:
JavaScript:
spriteset._pictureContainer[pictureId - 1]
However, that won't work as-is if some of the pictures have been moved elsewhere. The core scripts don't seem to do this at any point, but it's a possible source of plugin incompatibility.

With that said: you may simply be able to add another picture container and separate them like that. Or add the pictures directly to the parent, without a container. E.g.
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @plugindesc Show pictures on different layers.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/167062/
 * @help Moves picture IDs < 80 to tilemap layer 2.
 * (Between "Below" and "Normal" character layers)
 *
 * Free to use and/or modify for any project, no credit required.
 */
// Patch - move applicable sprites from container to tilemap.
;void (alias => {
  Spriteset_Map.prototype.createPictures = function() {
    alias.apply(this, arguments);
    const p = this._pictureContainer;
    // Move picture IDs 1~80 to the tilemap at z-level 2.
    for (let id = 1; id < 80; ++id) {
      const c = p.removeChildAt(0);
      c.z = 2;
      this._tilemap.addChild(c);
    }
  };
})(Spriteset_Map.prototype.createPictures);
For reference, here are the default z-values, from Tilemap#_createLayers (rmmz_core.js):
Spoiler: default tilemap child z values
0 : Lower tiles 1 : Lower characters 3 : Normal characters 4 : Upper tiles 5 : Upper characters 6 : Airship shadow 7 : Balloon 8 : Animation 9 : Destination
Technically, display order is determined by the relative child order. By default, picture sprites do not have a z-value:
Whoops about the Z-axis, I guess I saw the 'p.z' value and was like, "behold, a Z-axis!" lol

"I have a plugin for that" should be on a tshirt for so many veterans on here I think xD

Thank you for the edited plugin, it works perfectly! I'm also still shocked that MZ does not have such an insanely useful feature natively!
I hope it will also be useful for others too!

Example of the 'waterfall' and other BG animations:

...Although now that I look at it, the N_SpeechBubbles are just above everything including weather and HUD. 😠
So, I think I'll also try to apply what you've done here to that plugin as well. Just have to add it as a child to a different container, right?
If I can't manage it you may see me with a shiny new thread here in a few days 😅
Pretty! 😂

Yes, display layering is determined by child order. I had a quick look at N_SpeechBubbles and I'm guessing you can just replace line 200:
JavaScript:
        speechBubble.addTo(scene);
...with this:
JavaScript:
        speechBubble.z = 10;  // or whatever
        speechBubble.addTo(scene._spriteset?._tilemap ?? scene);
Dang, that did it!
Everything's sitting perfectly now!

Which means my abomination will continue to grow ◉‿◉)

Thank you so much @caethyril
I'll mark this to be solved and closed
This thread is being closed due to being solved. If you need it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/modify-caes-picture-z-container-plugin-to-allow-only-a-specific-range-of-pictures-to-be-changed.167062/