MZ Help with displaying an image correctly in the equip menu (using js)

Hello I'm looking to add a character image in the equip menu as well as other menus, I have gotten images to display but have been struggling to get things working the way I would like them too.

I have absolutely 0 knowledge in JS and have just kinda been winging it as I go. right now I have been just modifying the .js scripts in the project file as I don't know how plugins work which is probably a bad idea but what can you do...
I have this function I made in Window_StatusBase to draw pictures for me, I'm basically just taking a bunch of images an layering them on top each other to create the desired final image for the character
(note this code is the combination of like 4 or 5 different posts)
JavaScript:
Window_StatusBase.prototype.drawPicture = function(filename, x, y, w, h) {
    const Image = ImageManager.loadPicture(filename);
    const sprite = new Sprite(Image);
    Image.addLoadListener(function() {
        var pw = Image.width;
        var ph = Image.height;
        var sx = 0;
        var sy = 0;
        var bitmap = new Bitmap(pw,ph)
        bitmap.blt(Image, sx, sy, pw, ph, 0, 0, w, h);
        sprite.bitmap = bitmap
        sprite.setFrame(0,0,w,h)
        sprite.x = x
        sprite.y = y
    }.bind(this));
    SceneManager._scene.addChild(sprite);
};
this almost does job although I'm wondering if there is a better way to do it as it seems to be very clunky in game... one of the hardest parts of this whole thing has been trying to get the image to scale and be in order, since this function is used in multiple menus I need to be able to scale it as need. besides that the real issue here is on the equip screen, the image needs to update when you change equipments which it does, the problem is the old image does not get removed. so you end up with a big stack of images and the game starts to lag until you leave that menu.

I just run the code here,
JavaScript:
Window_EquipStatus.prototype.refresh = function() {
    this.contents.clear();
    if (this._actor) {
        const nameRect = this.itemLineRect(0);
        this.drawActorName(this._actor, nameRect.x, 0, nameRect.width);
        this.drawActorFace(this._actor, nameRect.x, nameRect.height);
        this.drawActorBust(this._actor,420,4,1069);
        this.drawAllParams();
    }
};
and would think that this.contents.clear(); would do everything but it does not seem too
(also drawActorBust() is the function draws the character with the drawPicture() function all it dose is draw the pictures in order based on what gear is equipped)

I have been trying to wrap my head around how this all works for a few days just messing with it, and googling things trying to find an answer to what I thought would be a simple problem lol. If anyone knows what is the issue or knows of a plugin or something that allows me to freely draw images in menus that would be very helpful.

(also sorry if this post is wrong in some way I have never posted here before)
Moved to: Learning Javascript
Okay, so from what I'm seeing you're planning to create a system that features composite images where individual images are layered on top of each other, and you want those images based on what items a character equips. Am I understanding that correctly?

In that case, you're going to have to learn javascript & learn how to do plugins rather than direct edits.
That's probably not what you wanted to hear, but actually doing what you intend to do requires an understanding of javascript & how the processes work that's way above what you can cobble together without actual knowledge.

How do I know? I was in your shoes, tried the same thing, and failed utterly because I didn't understand how the things were working underneath it all.
Since then, I've learned JS and I did in fact create a plugin that allows a character's images (face, battler, charset, etc.) to change depending on what equipment a character wears. That plugin is not available publicly yet, though.

That's probably not what you're hoping for, but that's what I went through.

Because what you're planning to do (from my understanding), I cannot give you detailed advice on how to resolve all the issues you're currently running in and all the issues you will run in with other plugins if you modify the core js files.

I can, however, point out some of the problems:
1674392905060.png

There's a couple problems here.
First the area marked in red:
You're creating a sprite that only exists inside this function. When you then add that sprite to the scene (via addChild), the instance inside the function will cease to exist and the only "address" where you can find that sprite to control & manipulate it is somewhere inside the children array of the scene.
It's like creating a little robot that you can control by manually pressing buttons on it - but you throw it out into a forest with no way to reach it easily.
That means the moment the function ends, you cannot control it anymore (e.g. change its image, destroy the sprite, etc.)
Instead you'll have to iterate through the children array of the scene in order to find that little sprite via some marker unique to it.

Instead, when creating objects you wish to be able to manipulate, attach them to a fixed, easily reachable address:
JavaScript:
Window_StatusBase.prototype.drawPicture = function(filename, x, y, w, h) {
    const Image = ImageManager.loadPicture(filename);
    this._actorSprite = new Sprite(Image);
    Image.addLoadListener(function() {
        var pw = Image.width;
        var ph = Image.height;
        var sx = 0;
        var sy = 0;
        var bitmap = new Bitmap(pw,ph)
        bitmap.blt(Image, sx, sy, pw, ph, 0, 0, w, h);
        this._actorSprite.bitmap = bitmap
        this._actorSprite.setFrame(0,0,w,h)
        this._actorSprite.x = x
        this._actorSprite.y = y
    }.bind(this));
    SceneManager._scene.addChild(this._actorSprite);
};
By using "this._actorSprite" we're creating a fixed location attached to the scene object where we will always find this sprite and thus we can always check if it exists (and if not create it) or if it does exist, change its current image.

Secondly, there's a lot of stuff in your code I don't think needs to be there.
When you use ImageManager.loadPicture(filename) it will already return to you a image from the url cache that's either loaded or pending. You don't need to create a new bitmap - you already have a bitmap.
You also already have the sprite's bitmap assigned to it when you're creating the sprite via "new Sprite(Image)" so you don't need to give it a bitmap - it already has one. But if you're worried the image may not yet have loaded, you can delay the bitmap assignment.
JavaScript:
Window_StatusBase.prototype.drawPicture = function(filename, x, y, w, h) {
    const Image = ImageManager.loadPicture(filename);
    this._actorSprite = new Sprite();
    Image.addLoadListener(function() {
        this._actorSprite.bitmap = Image;
        this._actorSprite.x = x;
        this._actorSprite.y = y;
    }.bind(this));
    SceneManager._scene.addChild(this._actorSprite);
};
You don't need to set a frame for a picture, since pictures only have one image (the entire image). You'd use the setFrame command if you have multiple images in one image file like in the charset images.

There's still a big problem with what you're doing, though:
the image needs to update when you change equipments which it does, the problem is the old image does not get removed.
This is the result of creating a sprite and then throwing them via addChild into the children array.
"this.contents.clear()" does not actually clear everything inside the scene.
It only clears the contents object, which is a bitmap image. The sprite you're creating isn't part of that bitmap image, so you clearing the contents doesn't affect any of the sprite children you're dealing with. You'll just be creating new sprites every time the function fires.

That's why it's important to have the sprite in a reachable location. (via this._actorSprite)
JavaScript:
Window_EquipStatus.prototype.refresh = function() {
    this.contents.clear();
    if (this._actorSprite) {
        this._actorSprite.destroy();
        delete this._actorSprite;
    }
    if (this._actor) {
        const nameRect = this.itemLineRect(0);
        this.drawActorName(this._actor, nameRect.x, 0, nameRect.width);
        this.drawActorFace(this._actor, nameRect.x, nameRect.height);
        this.drawActorBust(this._actor,420,4,1069);
        this.drawAllParams();
    }
};
This checks if an actorSprite exists, and if yes, it destroys the sprite & deletes the entry, prepping it for the next time you send a sprite there.

While the two improved functions above resolve some of the problems, I don't think they'll get you past all of them. Especially if you're planning to use layered images or draw images from equipment etc.
Okay, so from what I'm seeing you're planning to create a system that features composite images where individual images are layered on top of each other, and you want those images based on what items a character equips. Am I understanding that correctly?

In that case, you're going to have to learn javascript & learn how to do plugins rather than direct edits.
That's probably not what you wanted to hear, but actually doing what you intend to do requires an understanding of javascript & how the processes work that's way above what you can cobble together without actual knowledge.

How do I know? I was in your shoes, tried the same thing, and failed utterly because I didn't understand how the things were working underneath it all.
Since then, I've learned JS and I did in fact create a plugin that allows a character's images (face, battler, charset, etc.) to change depending on what equipment a character wears. That plugin is not available publicly yet, though.

That's probably not what you're hoping for, but that's what I went through.

Because what you're planning to do (from my understanding), I cannot give you detailed advice on how to resolve all the issues you're currently running in and all the issues you will run in with other plugins if you modify the core js files.

I can, however, point out some of the problems:
View attachment 251288
There's a couple problems here.
First the area marked in red:
You're creating a sprite that only exists inside this function. When you then add that sprite to the scene (via addChild), the instance inside the function will cease to exist and the only "address" where you can find that sprite to control & manipulate it is somewhere inside the children array of the scene.
It's like creating a little robot that you can control by manually pressing buttons on it - but you throw it out into a forest with no way to reach it easily.
That means the moment the function ends, you cannot control it anymore (e.g. change its image, destroy the sprite, etc.)
Instead you'll have to iterate through the children array of the scene in order to find that little sprite via some marker unique to it.

Instead, when creating objects you wish to be able to manipulate, attach them to a fixed, easily reachable address:
JavaScript:
Window_StatusBase.prototype.drawPicture = function(filename, x, y, w, h) {
    const Image = ImageManager.loadPicture(filename);
    this._actorSprite = new Sprite(Image);
    Image.addLoadListener(function() {
        var pw = Image.width;
        var ph = Image.height;
        var sx = 0;
        var sy = 0;
        var bitmap = new Bitmap(pw,ph)
        bitmap.blt(Image, sx, sy, pw, ph, 0, 0, w, h);
        this._actorSprite.bitmap = bitmap
        this._actorSprite.setFrame(0,0,w,h)
        this._actorSprite.x = x
        this._actorSprite.y = y
    }.bind(this));
    SceneManager._scene.addChild(this._actorSprite);
};
By using "this._actorSprite" we're creating a fixed location attached to the scene object where we will always find this sprite and thus we can always check if it exists (and if not create it) or if it does exist, change its current image.

Secondly, there's a lot of stuff in your code I don't think needs to be there.
When you use ImageManager.loadPicture(filename) it will already return to you a image from the url cache that's either loaded or pending. You don't need to create a new bitmap - you already have a bitmap.
You also already have the sprite's bitmap assigned to it when you're creating the sprite via "new Sprite(Image)" so you don't need to give it a bitmap - it already has one. But if you're worried the image may not yet have loaded, you can delay the bitmap assignment.
JavaScript:
Window_StatusBase.prototype.drawPicture = function(filename, x, y, w, h) {
    const Image = ImageManager.loadPicture(filename);
    this._actorSprite = new Sprite();
    Image.addLoadListener(function() {
        this._actorSprite.bitmap = Image;
        this._actorSprite.x = x;
        this._actorSprite.y = y;
    }.bind(this));
    SceneManager._scene.addChild(this._actorSprite);
};
You don't need to set a frame for a picture, since pictures only have one image (the entire image). You'd use the setFrame command if you have multiple images in one image file like in the charset images.

There's still a big problem with what you're doing, though:

This is the result of creating a sprite and then throwing them via addChild into the children array.
"this.contents.clear()" does not actually clear everything inside the scene.
It only clears the contents object, which is a bitmap image. The sprite you're creating isn't part of that bitmap image, so you clearing the contents doesn't affect any of the sprite children you're dealing with. You'll just be creating new sprites every time the function fires.

That's why it's important to have the sprite in a reachable location. (via this._actorSprite)
JavaScript:
Window_EquipStatus.prototype.refresh = function() {
    this.contents.clear();
    if (this._actorSprite) {
        this._actorSprite.destroy();
        delete this._actorSprite;
    }
    if (this._actor) {
        const nameRect = this.itemLineRect(0);
        this.drawActorName(this._actor, nameRect.x, 0, nameRect.width);
        this.drawActorFace(this._actor, nameRect.x, nameRect.height);
        this.drawActorBust(this._actor,420,4,1069);
        this.drawAllParams();
    }
};
This checks if an actorSprite exists, and if yes, it destroys the sprite & deletes the entry, prepping it for the next time you send a sprite there.

While the two improved functions above resolve some of the problems, I don't think they'll get you past all of them. Especially if you're planning to use layered images or draw images from equipment etc.
thank you for the response! I think I somewhat understand, alot of the weird code comes from me trying things like what you have stated, for the red outline. for some reason if I do this._actorSprite the image appears under the window and gets cut off, no idea whats up with that but I try it again, as for the yellow outline, from what I understand of the code, the reason i have 2 bit maps is because i use 1 to load the image and the the second to draw the scaled version. for some reason if i only used 1 i would get this weird effect where the image would repeatedly draw on top of itself smaller and smaller. like an infinite recursion effect.

but I do agree, I should probably learn js, its just so different from what I know about programming *cough cough* lua *cough cough* that its hard for me to understand it

since I am planning to use layered images doing this._actorSprite seems to really mess stuff up, im not sure if there is a way to make like an array of these custom images and then clear that with a function. its an idea I had but not sure how that would be done in js.

either way rambling aside thanks for the response again :3
thank you for the response! I think I somewhat understand, alot of the weird code comes from me trying things like what you have stated, for the red outline. for some reason if I do this._actorSprite the image appears under the window and gets cut off,
If a sprite is attached to a parent (window, scene, etc.) it will base their x & y coordinates in their parent's by default. This makes it easier to put a sprite into the frame of a window, since the 0,0 coordinates are the top left of that window, not of the overall screen.
no idea whats up with that but I try it again, as for the yellow outline, from what I understand of the code, the reason i have 2 bit maps is because i use 1 to load the image and the the second to draw the scaled version.
for some reason if i only used 1 i would get this weird effect where the image would repeatedly draw on top of itself smaller and smaller. like an infinite recursion effect.
It depends on how you set the scale. What you're describing sounds like you continually reduce the scale every time the function fires.

That's why generally it'd be better to create the sprite only when the window creates itself, and set the scale then, and in the update sections to only check & adjust the bitmap being used.
but I do agree, I should probably learn js, its just so different from what I know about programming *cough cough* lua *cough cough* that its hard for me to understand it
JS is - in my opinion - surprisingly simple once you've wrapped your head around it. Once you understand how functions work, const, let, var, objects & arrays & class/prototype, you've basically got all the elements you need for RPGM Plugins.
I've posted a couple of courses & ways to get into JS a little while ago here.
since I am planning to use layered images doing this._actorSprite seems to really mess stuff up, im not sure if there is a way to make like an array of these custom images and then clear that with a function. its an idea I had but not sure how that would be done in js.

either way rambling aside thanks for the response again :3
Layered images are better handled as single images. You feed the images to a function & have them fuse them into a single image layer which you then use.

If you want to do that easily, check out my Plugin for Plugin Developers in the signature.
There's two functions that really help with that.
const multipleImages = new ANY_ImageLoader("arrayOfBitmapsHere")
-> allows you to have multiple images loaded seperately at once.
You can use multipleImages.isReady() to check if all images have been loaded & you can use multipleImages.addLoadListener(function) to have a function executed once all images have been loaded.

This allows you to only execute a function when all images have been loaded into the game.

The second function is ANY_combineImages(bitmaps/imageLoader), which is a function that receives an ANY_ImageLoader or bitmaps or an array of bitmaps, and it'll hand you back a single bitmap that's all those images fused in the order in which they were stored.

You'll find a demo & documentation attached to the download.
The demo level in question is the Utilities map, the event in the center. It features a template plugin that creates a scene & loads multiple images at once that it fuses into a single image.

In the documentation folder, in the utilities subfolder, you'll find detailed documentation on how the image loader and the image combiner are used, and there's also the Example Plugin mentioned above, which can be used as template for how to create your own scenes with images, or how to inject sprites into existing scenes.

Note that to use my plugin, you'll have to create your own plugin. It's not possible for my plugin to be loaded before the core JS files, so you wouldn't be able to use my functions when purely editing JS files.

Fortunately, you can use any of my example plugins as a template to create your own plugins.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-displaying-an-image-correctly-in-the-equip-menu-using-js.154148/