MV Showing a 'highlight image' when selecting an actor when in formation command

Hey everyone,

I'm trying to show a highlighting image, a .png that is essentially just an alternate color of the actor card frame that I have in my status menu. I'd like it to show when using the formation command when first pressing the ok button on an actor and then have it disappear on the next ok button press, or when canceling out of the formation command of course.

I'm using Triacontane's Vanguard and Rearguard plugin to achieve a front row/ back row mechanic akin to older Final Fantasy games.

The first screencap below is the default, and the second is what I'd like to have (mocked up in Aseprite). Both are when you've pressed the OK button on an actor.

I've been able to have the image show up, but not in the function I'd like.

As usual, any help is appreciated!

EDIT: I guess for further clarification, I'd just like to draw a .png rather than have that black translucent rectangle show up. I've looked for what's handling it and I can't seem to find it. It's likely a function that's used for multiple things, but I'm thinking if I use an if (this._formationMode) check to draw the .png within the function that draws the rectangle, that should work? I've yet to find it though. Assistance would be great!
I'd still appreciate some assistance on this one. If you need more information, let me know! I only know what I know, haha.
EDIT: I guess for further clarification, I'd just like to draw a .png rather than have that black translucent rectangle show up.
That looks like the "pending cursor", drawn by Window_MenuStatus.prototype.drawItemBackground when appropriate. I don't think the core scripts use that anywhere else.

The default pendingColor is the CSS colour of the pixel at (120, 120) in the windowskin, i.e. one of the centre pixels of the cursor. It can be overridden quite easily, e.g. this in a plugin:
JavaScript:
Window_Base.prototype.pendingColor = function() {
    return "#ff4";  // 25% white
};

If you want to replace the background image, that depends how you're adding those images in the first place. It should be straightforward to substitute one bitmap for another, based on the window's _pendingIndex, but if all 3 boxes are 1 background image then things get a little more awkward.
Ah! Ok awesome. I've got the pending index cursor transparent now.
I HAVE played around with
Code:
Window_MenuStatus.prototype.drawItemBackground = function(index) {
        if (index === this._pendingIndex) {
            var rect = this.itemRect(index);
            var color = this.pendingColor();
            this.changePaintOpacity(false);
            this.contents.fillRect(rect.x, rect.y, rect.width, rect.height, color);
            this.changePaintOpacity(true);
        }
    };
before by attempting to draw a .png in there instead, but it has never worked and I'm not sure why, because it seems like it should just be a plug and play, essentially.

I made this to call the drawing of the image
Code:
Window_Base.prototype.drawActorCardSelect = function (x, y, width) {
        this._actorCardSelect = new Sprite(ImageManager.loadPicture('actorCardSelect'));
        this.addChild(this._actorCardSelect);
    }
So then I would add this.drawActorCardSelect(); where I'd like it to show up. But it doesn't seem to in Window_MenuStatus.prototype.drawItemBackground.

The background cards are each individual images, too!
The engine uses 2 main approaches for showing an image from file:
  1. Add a child sprite based on a bitmap. Display depends on parent-child arrangement.
    • It looks like this may be what you're using, adding the sprites to the window.
    • In this case, you could either:
      • Reassign each sprite's bitmap (replace image); or
      • Add a new child sprite and move it to the right position (overlay).
      For the correct timing, maybe try patching the setPendingIndex method of Window_MenuStatus.

  2. Blit ("paint") the image from its source onto the window contents.
    • This requires the image to be in memory at the time of calling contents.blt.
    • You could add a check for _pendingIndex when blitting the card images.
Hmmm...

Ok, so I've tried a few things.
I've tried this
Code:
Window_MenuStatus.prototype.drawItemBackground = function(index) {
        if (index === this._pendingIndex) {
            this._actorCardSelect = new Sprite(ImageManager.loadPicture('actorCardSelect'));
            var rect = this.itemRect(index);
            //var color = this.pendingColor();
            //this.changePaintOpacity(false);
            this.contents.addChild(this._actorCardSelect);
            this._actorCardSelect.x = 18;
            this._actorCardSelect.y = 10;
            //this.changePaintOpacity(true);
        }
    };
but nothing shows up. Which is weird to me because this is where the pending cursor is made, so I'm confused why it doesn't show.

I've also tried
Code:
  var _Window_MenuStatus_setPendingIndex = Window_MenuStatus.prototype.setPendingIndex;
    Window_MenuStatus.prototype.setPendingIndex = function(index) {
    _Window_MenuStatus_setPendingIndex.call(this)
        var lastPendingIndex = this._pendingIndex;
        this._pendingIndex = index;
        this.drawActorCardSelect();
        this.redrawItem(this._pendingIndex);
        this.redrawItem(lastPendingIndex);
    };
and it does show up, however it's layered over my Galv's Cursor Image and only ever appears on the first actor space, and does not disappear when canceling out or confirming the selection, as the original pending cursor did.

This is where I've been stuck the last number of days. I know I'm missing some knowledge, but Googling has provided little information for me to parse out. Or I'm just Googling the wrong buzz words maybe?
Your first attempt tries to add a sprite as a child of contents, a bitmap. But a bitmap is an abstract wrapper for texture data, not a display object. Bitmaps don't have an addChild method, either. You'd probably want to add it to the window, i.e. this.addChild. It also patches drawItemBackground, which won't do anything by itself because the window does not typically redraw itself that often.

Your second attempt calls for a redraw, but I don't know why. Didn't you just want to update the "pending" indicator? The variable manipulation is redundant, since you've already called the alias, so lastPendingIndex is already equal to index at that point. Just create the sprite if it doesn't already exist, then move it into position~

In both cases you're creating a new sprite every redraw. That's...not great. It also explains why the cursor never disappears with your second option: quite the opposite! The simplest fix is lazy initialisation, i.e. create it if it doesn't exist. Maybe something like this:
Spoiler: code
JavaScript:
void (function() {
  var alias = Window_MenuStatus.prototype.setPendingIndex;
  Window_MenuStatus.prototype.setPendingIndex = function(index) {
    // Process as usual.
    alias.apply(this, arguments);
    // Ensure sprite exists.
    if (!this._actorCardSelect) {
      this._actorCardSelect = new Sprite(ImageManager.loadPicture('actorCardSelect'));
      this.addChild(this._actorCardSelect);
    }
    // Update sprite's visibility and position.
    var visible = this._pendingIndex >= 0;
    this._actorCardSelect.visible = visible;
    if (visible) {
      var rect = this.itemRect(index);
      this._actorCardSelect.move(rect.x, rect.y);
    }
  };
})();
Alternatively you could patch the window's initialize method and create the sprite there. this._actorCardSelect.visible determines whether the sprite is, well, visible.

As I mentioned earlier, display layering is based on parent-child order. A child sprite will display on top of its parent; earlier children will display under later ones. Try loading Galv's Cursor Image plugin after this one you're writing?

Just to be clear: if you want help with plugin compatibility issues, you should list all the plugins you are using. Also make all initial tests in as simple an environment as possible, i.e. turn off all other plugins that are not strictly required for your plugin to work.
Your first attempt tries to add a sprite as a child of contents, a bitmap. But a bitmap is an abstract wrapper for texture data, not a display object. Bitmaps don't have an addChild method, either. You'd probably want to add it to the window, i.e. this.addChild. It also patches drawItemBackground, which won't do anything by itself because the window does not typically redraw itself that often.

Your second attempt calls for a redraw, but I don't know why. Didn't you just want to update the "pending" indicator? The variable manipulation is redundant, since you've already called the alias, so lastPendingIndex is already equal to index at that point. Just create the sprite if it doesn't already exist, then move it into position~

In both cases you're creating a new sprite every redraw. That's...not great. It also explains why the cursor never disappears with your second option: quite the opposite! The simplest fix is lazy initialisation, i.e. create it if it doesn't exist. Maybe something like this:
Spoiler: code
JavaScript:
void (function() {
  var alias = Window_MenuStatus.prototype.setPendingIndex;
  Window_MenuStatus.prototype.setPendingIndex = function(index) {
    // Process as usual.
    alias.apply(this, arguments);
    // Ensure sprite exists.
    if (!this._actorCardSelect) {
      this._actorCardSelect = new Sprite(ImageManager.loadPicture('actorCardSelect'));
      this.addChild(this._actorCardSelect);
    }
    // Update sprite's visibility and position.
    var visible = this._pendingIndex >= 0;
    this._actorCardSelect.visible = visible;
    if (visible) {
      var rect = this.itemRect(index);
      this._actorCardSelect.move(rect.x, rect.y);
    }
  };
})();
Alternatively you could patch the window's initialize method and create the sprite there. this._actorCardSelect.visible determines whether the sprite is, well, visible.

As I mentioned earlier, display layering is based on parent-child order. A child sprite will display on top of its parent; earlier children will display under later ones. Try loading Galv's Cursor Image plugin after this one you're writing?

Just to be clear: if you want help with plugin compatibility issues, you should list all the plugins you are using. Also make all initial tests in as simple an environment as possible, i.e. turn off all other plugins that are not strictly required for your plugin to work.

Thanks for the clarification! For both of my attempts, I pretty much copy and pasted the code I thought might be relevant to what I'm trying to do, but to be honest wasn't sure exactly all their innards did, such as redraw. Because it was there already it seemed important, and all I needed to do was draw the image haha.

But this is good. I appreciate you explaining things! I did google a lot, but always came to dead ends or people trying similar but different things and couldn't figure out how to apply it to my objective.

I'm 100% a graphic designer and much less a programmer, but I can sort of grasp concepts here and there and usually cobble and fumble my way through making a functional piece of coding. I understand programming is a language, and I get the logic behind most of it, but sometimes I don't realize what logic is needed, haha. So again, I appreciate the explanations!

I'm just going to go over the script you sent to make sure I understand it and don't just copy and paste it:
Spoiler: code
JavaScript:
void (function() { //this is essentially making this chunk of script as a plugin
  var alias = Window_MenuStatus.prototype.setPendingIndex; //creating a variable for setPendingIndex
  Window_MenuStatus.prototype.setPendingIndex = function(index) {
  alias.apply(this, arguments); //runs the code within the orginal setPendingIndex from rpg_windows.js
    // Ensure sprite exists.
    if (!this._actorCardSelect) { //asks if the sprite DOESN'T exist
      this._actorCardSelect = new Sprite(ImageManager.loadPicture('actorCardSelect')); //makes the sprite
      this.addChild(this._actorCardSelect); //adds the sprite as a child of Window_MenuStatus
    }
    // Update sprite's visibility and position.
    var visible = this._pendingIndex >= 0; //creating the visible variable being equal or greater to pendingIndex if it's currently 0 (because a lower value would mean there's no actor?)
    this._actorCardSelect.visible = visible; //makes the sprite equal to the pendingIndex if it's 0 or greater
    if (visible) {
    //if it's visible, get the index of the itemRect and then add the sprite to it
      var rect = this.itemRect(index);
      this._actorCardSelect.move(rect.x, rect.y);
    }
  };
})();
😅 I often lapse into a stricter tone when focusing on technical stuff, sorry if that's off-putting. And don't worry, I wouldn't offer the explanations if I didn't think it was worth my time. =P

Here's a more thorough explanation of the untested code I suggested:
Spoiler: brace for impact
  1. void (function() { ... })();
    • This is an IIFE. It is used here to provide a function scope, preventing the var alias from being accessible outside.
    • Plugins can be written in different ways: IIFEs are often used in some capacity, but aren't required.

  2. var alias = Window_MenuStatus.prototype.setPendingIndex;
  3. Window_MenuStatus.prototype.setPendingIndex = function(index) {
    • Redefine the setPendingIndex method as a new function with 1 named argument.
    • Note that alias is unaffected: we are not changing the existing function stored in alias, we are creating a brand new one.

  4. alias.apply(this, arguments);
    • Calls the stored function in the current context (this) with the current function arguments, named or otherwise.
    • The function stored in alias is not necessarily the original one from rpg_windows.js: it might have been replaced already by a plugin that loads before this one.

  5. if (!this._actorCardSelect) {
    • You're correct: this passes iff this._actorCardSelect is not truthy. Any object (such as a sprite) is truthy; a value like undefined is falsy.

  6. this._actorCardSelect = new Sprite(ImageManager.loadPicture('actorCardSelect'));
    • Creates a new Sprite with a given Bitmap, then stores a pointer to that sprite in the _actorCardSelect property of this (i.e. the menu status window instance).
    • Advanced: when you make a new object, it occupies some space in memory - the pointer tells you where it is in memory. This also means that to copy an object you have to actually create a new object, otherwise you're just copying the pointer.

  7. this.addChild(this._actorCardSelect);
    • Adds the sprite to this display object (a Window_MenuStatus instance, like your comment says), so it will show up when its parent is rendered as part of the current scene.

  8. var visible = this._pendingIndex >= 0;
    • Define a new local variable named visible, and set it equal to true if the current pending index is 0 or more, or false otherwise.
    • The pending index is the position of the pending item in the list. The first item in the list is index 0. Pending index is set to -1 to disable it (e.g. see the onFormationOk and onFormationCancel methods of Scene_Menu, rpg_scenes.js).

  9. this._actorCardSelect.visible = visible;
    • Set your pending sprite's visible property equal to the value of the local visible variable, defined on the previous line. So it'll be visible iff the pending index is 0 or more. Objects that aren't visible will not be rendered.

  10. if (visible) {
    • Self-explanatory!

  11. var rect = this.itemRect(index);
    • Get the Rectangle for the item at position index. This is an object with (mainly) 4 properties: x, y, width, and height.
    • (It would probably have been better design for me to write this._pendingIndex here instead of index, just in case something changed how the value is assigned.)

  12. this._actorCardSelect.move(rect.x, rect.y);
    • Move your pending sprite to the XY coordinates for this item.
I've included links to MDN for some basic stuff. The display elements used in RMMV are (wrappers for) PIXI things.

Hopefully that answers more questions than it raises!

[Edit: added missing "not" to point 5. 😴]
Oh no worries at all! I completely understand. I can also be like when being instructional in a thing I know very well, haha.

Wow ok this is awesome! The best database I've come across is the GitHub breakdown of all the things, but they're not really explained. More like "this is found here and it's related to that."

You've helped me out a bunch again. caethyril! Thank you so much!

EDIT: ALSO! changing the child/parenting of the highlight from addChild to addChildToBack move the sprite behind Galv's cursor and fixed it up nicely! (Galv's cursor image was below but the highlight was still showing on top).

Original thread: https://forums.rpgmakerweb.com/threads/showing-a-highlight-image-when-selecting-an-actor-when-in-formation-command.167043/