How and When are character images loaded?

I would like to know if it's possible to overwrite a character image file during runtime then have the game show the updated character image.

I noticed if I overwrite the png image while playing the game it doesn't display the updated image.

In games like Animal Crossing or Mario Kart DS, the player can create an image in game, then that image is displayed as the town's flag, decal on the kart, etc.
It seems like you should prefer to make a new file (e.g. origName_mod) and have event pages or commands to change to that filename.

There's no way someone would be able to start your game over? In which case they'd need the original image?

A stricter answer to your question is events should update any time you load a different map, and the player character and followers will update when they're set to a different filename or index, per Sprite_Character.isImageChanged().
It seems like you should prefer to make a new file (e.g. origName_mod) and have event pages or commands to change to that filename.

There's no way someone would be able to start your game over? In which case they'd need the original image?
If you made a new file every time the image was changed, e.g. the player redesigned their town flag, the image directory will get bloated with image files.

If the player restarted the game, you would just have a command to overwrite the file to its original state, e.g. a blank canvas
Okay. Well, that function and what checks it should lead you to what you want to happen.
Some more notes:
  1. You'd probably want to flag the character and have their sprite update its bitmap when necessary.

  2. Image data in the game's image cache is unaffected by overwriting a file on disk.

  3. You'd want to save such an image either as part of the save data (e.g. data URL) or as, say, a PNG in a parallel structure. Overwriting a core image file would only be a workable approach with at most 1 save slot.
Some more notes:
  1. You'd probably want to flag the character and have their sprite update its bitmap when necessary.
How do you do this? If I have an event object, what script do I use to update its bitmap sprite?

  1. Image data in the game's image cache is unaffected by overwriting a file on disk.

  2. You'd want to save such an image either as part of the save data (e.g. data URL) or as, say, a PNG in a parallel structure. Overwriting a core image file would only be a workable approach with at most 1 save slot.
So should I be creating a new image file like ATT_Turan said, then setting the event image to that new file name? What do I do about file bloat from creating a new file every time the image is changed?
What do I do about file bloat from creating a new file every time the image is changed?
Isn't it a trivial bit of logic to go back and forth between two filenames? If current filename is object_1, save the new image as object_2, then change the event's image to object_2. Then reverse.
How do you do this? If I have an event object, what script do I use to update its bitmap sprite?
You would start with the sprite, not the event object. Something like this, as part of a plugin:
JavaScript:
sprite.bitmap = yourBitmapReferenceHere;
The engine is structured so that sprites (visual) update based on their character (model); an event object doesn't know anything about its sprite. E.g. a Game_Event has a corresponding Sprite_Character: that sprite updates its bitmap, screen coordinates, etc based on values from the event.

Following that structure, the sprite instance would be responsible for updating its own bitmap.
Spoiler: supporting rpg_sprites.js excerpt
JavaScript:
Sprite_Character.prototype.updateBitmap = function() {
    if (this.isImageChanged()) {
        this._tilesetId = $gameMap.tilesetId();
        this._tileId = this._character.tileId();
        this._characterName = this._character.characterName();
        this._characterIndex = this._character.characterIndex();
        if (this._tileId > 0) {
            this.setTileBitmap();
        } else {
            this.setCharacterBitmap();
        }
    }
};

Sprite_Character.prototype.isImageChanged = function() {
    return (this._tilesetId !== $gameMap.tilesetId() ||
            this._tileId !== this._character.tileId() ||
            this._characterName !== this._character.characterName() ||
            this._characterIndex !== this._character.characterIndex());
};
Like Turan said, you could patch the isImageChanged method to also return true if the sprite's _character has a specific property set. Then just set that value on the character when appropriate, e.g. $gameMap.event(123).needsBitmapRefresh = true;.

So should I be creating a new image file like ATT_Turan said, then setting the event image to that new file name? What do I do about file bloat from creating a new file every time the image is changed?
Why not just retain the data for the altered image? Edit it however many times you like per playthrough...no point keeping any version other than the most recent though, right?

If you were to overwrite the original, it would change for all playthroughs; resetting it on new game would also reset it for existing saves. That's the main problem I was pointing out. If you instead record 1 image per save file (e.g. as part of the save data, like I mentioned) then that problem disappears.
Image data in the game's image cache is unaffected by overwriting a file on disk.
I am still stuck on this issue. If I overwrite an image, then I clear out the cache with
ImageManager.clear()

then change the player's image
$gamePlayer.setImage("$newImage");

It still changes the player to the old image. Where was the old bitmap data being stored if the cache had been cleared?
First: setImage will have no effect if the "new" image filename is the same as the old one. So make sure it's not that. Note that setImage(name, index) affects the character: you'll need to wait at least 1 frame between image changes to actually affect the corresponding sprite.

Second: I believe you'd need to clear all references to the "old" image first? That includes any textures currently using the image. That includes calling destroy on any generated bitmaps once you're done with them, since otherwise they'll persist for the rest of the session (which also constitutes a memory leak). Otherwise PIXI, or the browser itself, may simply return the original data from their cache systems.

More generally: exporting data to file does not destroy the data already in RAM. It'd be much simpler to just use that already-in-memory data as opposed to trying to save to file and then load that file. Yes, you'd need something (e.g. data URL or PNG) to carry the data through the in-game save/load process, but that only needs to be referenced when loading a save.

(In case it helps, feature Q36 of my plugin Cae_Tweaks, "Show Picture snapshots", implements save/load of generated images. I've used data URLs, but I don't see why it wouldn't be doable with PNGs instead.)
Otherwise PIXI, or the browser itself, may simply return the original data from their cache systems.
I confirmed it's both the ImageManager cache and the browser cache as I was only able to get it to finally load the new image after clearing the ImageManager cache and disabling the browser cache through Developer Tools.

I have to ask then, is there any way to either remove or stop an image with a specific filename from being added to the browser cache without going through Developer Tools?

As for using the already in-memory data, I'd have to think about how to work it in, because my plugins are looking to load from a file. I would have to overwrite parts of the plugins to work that way instead. The alternative is do what ATT_Turan said and keep creating new files instead of overwriting the image, and deleting the old files.
OK I'm not very familiar with browser cache stuff, but I did a little searching and found this:
I haven't tested, but maybe you could simply write a patch to append "?" + performance.now() to all qualifying Bitmap.load URLs?

In case it helps, I also found this info on cache control request headers:
Not sure how that'd work with RM's bitmaps, though. 😴
Thanks for your help, I finally got it working. Here's the solution:

Paste this in a plugin file
Code:
//overwrite bitmap load to bypass browser cache for certain image names
Bitmap.load = function(url) {
    if (url.includes("$newImage.png")) {
        url += "?bust=" + Date.now(); // Bypass cache for that image
    }

    var bitmap = Object.create(Bitmap.prototype);
    bitmap._defer = true;
    bitmap.initialize();

    bitmap._decodeAfterRequest = true;
    bitmap._requestImage(url);

    return bitmap;
};

Script to clear the ImageManager cache of one file
Code:
ImageManager._imageCache._items["img/characters/%24newImage.png:0"]=null; //the %24 is a $
Great! 😂

Just to note: if another plugin changes the same method (Bitmap.load in this case), a strict override like this is liable to cause problems, depending on load order. When adding before/after stuff like this it's safer to use a "patch" approach, i.e. redefine the method to do some stuff and call the original version. Example:
JavaScript:
const alias_Bitmap_load = Bitmap.load;  // Store original method.
Bitmap.load = function(url, ...args) {
    if (url.includes("$newImage.png")) {
        url += "?bust=" + Date.now(); // Bypass cache for that image
    }
    return alias_Bitmap_load.call(this, url, ...args);  // Call original method.
};
Not sure how likely a conflict is in this case, but best to play it safe~ 👋

Original thread: https://forums.rpgmakerweb.com/threads/how-and-when-are-character-images-loaded.173674/