MV [Solved] Force display of another message face image through a script call

Edit / TL;DR: This has been solved by adding @Mac15001900's code as a plugin, initializing it, and adding script commands to some events. You can check all the instructions on the last post in this thread.
​


Hi, everyone!
Thanks in advance for any help provided.
​


First of all, I have to mention that I know that what I want isn't the best of practices and that I should've thought of it from the beginning of development. Also, I'll definitely be ok with it if it's really unfeasible and won't blame the engine or anyone else, but I've been wondering and trying to make it work.
​


I have a game with tons of already existing messages set with different face image files and expressions for each one. However, now I want to add different skins/outfits/etc. to my playable characters (who also have their own messages in-game). So that I don't have to go all over each message, I want to add a script call to a parallel common event that will handle that.

In other words:

What I want is to use JavaScript to forcibly display some different message face images from the ones already assigned on event pages. I want to use that for adding different skins/outfits to my main characters only, and it could also be used for automatically showing injured/happy/different faces according to different variables.

The game was released years ago and has hundreds, if not thousands, of dialogue messages, already set with multiple facial expressions (face indices) and face files (with more face indices).

For example, let's say I want to change the protagonist's skin/outfit. That could be easily achievable through the "Change Actor Images" event command. However, all the existing messages in the game would still show the base skin/outfit, which I set up on each event page.

I'm aware that YEP_MessageCore has the option to show the face of a specific actor through text codes, and I've seen similar plugins that require editing each Show Text event command. The problem is that there are simply too many messages within the game (even considering only the characters I want to add a skin to), and since I use SRD_TranslationEngine, I would have to either translate the whole game again or edit each message in the translation file too. That isn't doable at the current state of the game, thus my first paragraph.

I've also found ANY_Message_Face_Switcher, which is promising, but the problem is that its function isn't fully compatible with what I want. It automatically changes the message window face to an Actor's face based on the file name, it won't allow me to separately switch it in a parallel common event using variables or other conditions while also considering face indices. I would also have to edit my face picture files' names, which could lead to unexpected behavior in the game, especially due to the number of plugins I use. Furthermore, trying to edit that plugin on ChatGPT didn't work for me either.

One example of what I want is a parallel common event with something similar to this:
​
Code:
if(currentlyShownMessageFace == (part of an array of Aurora's face files) && variable1 = 0)
{
drawImage("aurora.png", sameFaceIndexAsOriginal);
}
else if (currentlyShownMessageFace == (part of an array of Aurora's face files) && variable1 = 1)
{
drawImage("aurora2.png", sameFaceIndexAsOriginal);
}

But even with a code like that, I'd have some trouble because I sometimes use more than one face file per character on messages. After all, the main characters have around 16 facial expressions each. I'm not sure how that could be handled, but I'll explore that below.



The reason why I'm not posting this as a plugin request is that I've already tried some stuff, have some examples, and noticed it seems possible to do so by using a single script call (though I guess I might probably be wrong). I hope that wasn't the wrong decision.

I researched a bit on other existing plugins but without success. Since I've just started learning some JavaScript and my current knowledge of it is limited, the first thing I tried was to create a plugin on ChatGPT, which gave me some headaches but allowed me to understand how message face display works. Unfortunately, the plugin itself didn't work, even after tons of prompts and "manual" coding.

Here is its currently unusable code in case anyone wants to take a look (don't mind the name, help file, etc. I was just trying to have some fun imagining myself as a plugin creator lol):

Spoiler: ZOE_DynamicMessageFaces.js (Currently useless)
JavaScript:
//=============================================================================
// ZOE_DynamicMessageFaces.js
//=============================================================================
/*:
* @plugindesc Plugin for dynamically changing message face images based on variables. <ZOE_DynamicMessageFaces>
* @author ZoeZero (Zoe Miranda)
*
* @help
* Dynamic Message Faces
* Version 1.0
*
* Plugin Commands:
*   DynamicFace originalFaceFile newFaceFile
*     - Changes the original face image file to the new face image file dynamically.
*       Example: DynamicFace gabriela aurora
*
*   ResetDynamicFaces
*     - Resets and shows all original face images set in the database.
*
* Script Calls:
*   $gameMessage.dynamicChangeFace(originalFaceFile, newFaceFile)
*     - Changes the original face image file to the new face image file dynamically.
*       Example: $gameMessage.dynamicChangeFace('gabriela', 'aurora');
*
*   $gameMessage.resetDynamicFaces();
*     - Resets and shows all original face images set in the database.
*
*   $gameMessage.getCurrentFaceIndex()
*     - Retrieves the current facial expression ID (face index) of the upcoming message.
*
* Terms of Use:
* This plugin is free for both commercial and non-commercial use.
* You may modify it for your own purposes, but you may not sell this plugin directly
*/

(function() {

    // Initialize dynamic faces object in Game_Message
    var _Game_Message_initialize = Game_Message.prototype.initialize;
    Game_Message.prototype.initialize = function() {
        _Game_Message_initialize.call(this);
        this._dynamicFaces = {};
    };

    // Override setFaceImage method in Window_Base to store original face information
    var _Window_Base_setFaceImage = Window_Base.prototype.setFaceImage;
    Window_Base.prototype.setFaceImage = function(faceName, faceIndex) {
        this._originalFaceName = faceName;
        this._originalFaceIndex = faceIndex;
        _Window_Base_setFaceImage.call(this, faceName, faceIndex);
    };

    // Override method to load message face, considering dynamic faces
    var _Window_Message_loadMessageFace = Window_Message.prototype.loadMessageFace;
    Window_Message.prototype.loadMessageFace = function() {
        var faceName = this._originalFaceName;
        var faceIndex = this._originalFaceIndex;
        if ($gameMessage._dynamicFaces && $gameMessage._dynamicFaces[faceName]) {
            faceName = $gameMessage._dynamicFaces[faceName];
            if (faceIndex === undefined) {
                faceIndex = 0;
            }
        }
        _Window_Base_setFaceImage.call(this, faceName, faceIndex); // Call setFaceImage from Window_Base
        _Window_Message_loadMessageFace.call(this, faceName, faceIndex);
    };

    // Add methods to Game_Message to dynamically change and reset faces
    Game_Message.prototype.dynamicChangeFace = function(originalFaceFile, newFaceFile) {
        this._dynamicFaces[originalFaceFile] = newFaceFile;
    };

    Game_Message.prototype.resetDynamicFaces = function() {
        this._dynamicFaces = {};
    };

    // Extend Game_Interpreter to handle plugin commands for dynamic faces
    var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        _Game_Interpreter_pluginCommand.call(this, command, args);
        if (command === 'DynamicFace') {
            var originalFaceFile = args[0];
            var newFaceFile = args[1];
            $gameMessage.dynamicChangeFace(originalFaceFile, newFaceFile);
        } else if (command === 'ResetDynamicFaces') {
            $gameMessage.resetDynamicFaces();
        }
    };
})();

What I last tried to do after analyzing YEP_MessageCore and rpg_objects.js was to create the following script call, to no avail. It has also faced some changes by AI:

Spoiler: Common Event Script Call
JavaScript:
$gameVariables.setValue(1342, $gameMessage.faceName());
        $gameVariables.setValue(1343, $gameMessage.faceIndex());
if($gameMessage.faceName() == "gabriela" && $gameVariables.value(1341) != 0)
{
$gameMessage.setFaceImage("gabriela-miner", $gameVariables.value(1343));
Window_Message._faceBitmap = ImageManager.reserveFace("gabriela-miner", 0, this._imageReservationId);
console.log("Debug:",$gameMessage.faceName(),$gameMessage.faceIndex(),$gameVariables.value(1341),$gameVariables.value(1343));
Window_Base.prototype.drawFace("gabriela-miner", $gameVariables.value(1343), 0, 0);
ImageManager.releaseReservation(this._imageReservationId);
}

I tried testing it out with the following event, but only the original message window face picture is shown (it won't change according to what I specified in the script call):
Spoiler: Test Event Page
event-page.png


One important thing to mention is that I had managed to alter the currently shown message window face picture file with my ZOE_DynamicMessageFaces plugin before I made more changes on ChatGPT. However, all messages, regardless of the set message window face picture file, would show the new face. Basically, all NPCs had my character's new face when talking to them. I've once also managed to achieve the same with a script call (without the plugin but with the same problem). I just wish I had backed up every piece of barely working code, but I didn't.

Ah, also rather importantly, many of my problems when experimenting were related to retrieving the appropriate face index. I have separate files with the same facial expressions on each corresponding face index, but I couldn't get the game to identify the current face index and show the same face index of a different file. If it is possible to manually set up specific face indices/expressions that would be equivalent to the original ones for cases in which the file doesn't follow the same pattern, even better (though I suspect that would perhaps constitute a plugin request). Here is an example of an original and an alternative face picture file to illustrate what I mean (the second part of my paragraph suggests dealing with picture files that don't follow the same design):

Spoiler: Original and alternative face files
gabriela-example.png

In conclusion, what I believe is that getting information from rpg_object's Game_Interpreter.prototype.command101 and using Anyone's Message Face Switcher as a reference to create a single script call (even if minified) in a parallel common event might be the best way to accomplish the desired effect.

Since I don't know what I'm doing wrong or what else I can do, I am asking for your help.

These are the plugins I use (I know some Yanfly plugins are not in order. I'll handle that soon):

Spoiler: My current plugin list
1714278962393.png

I can provide any necessary information, links, codes, Steam keys, screenshots, or explanations.

Thanks a lot in advance! :LZYgrin:​
you're missing one point: only the current map exists in memory, and that includes its events.
so you would have to make one parallel process on each map and play through each map once to have that effect.

I think an external approach would be better.
Use any multifile text editor to open all map json files, then search through them for the text commands to decide what to replace automatically.
This will allow you to implement and add text codes without having to go through each message.
Like the yanfly ones for using actor faces and so on.

that of course is only the first step as you want more changes, but it should also be possible to use that to add new event commands into the structure, if you are very careful with the formats.
this could then add change actor face commands.

As to your original idea: please look through the MV forums, on one of them there is a pinned topic about the script equivalents of event commands.
You would want the script equivalent of "change actor graphics" for those cases that can't be handled with the two options above.
I'm not quite sure from the post, but I'm assuming you'd be fine with just replacing certain files with other files? As in, if a character changes outfit, you'd only need to swap the files for that character? If you do have multiple characters per file, it might be a bit more complicated. But for now I'll assume that's not the case.

It's probably simplest to alias Game_Message.prototype.faceName and do any face filename replacement there. Then create an object that stores each substitution and put that in, say, $gameSystem.

Run this line at the beginning of the game (before any messages are shown):
$gameSystem.faceSwaps = {};
Add that as a plugin:
JavaScript:
Game_Message.prototype.faceName = function () {
    return $gameSystem.faceSwaps[this._faceName] ?? this._faceName;
};
When you want to replace a file (e.g. "Actor1" with "Actor1-summer"), use a script command:
$gameSystem.faceSwaps["Actor1"] = "Actor1-summer";
If you want it to go back to default:
$gameSystem.faceSwaps["Actor1"] = undefined;

Also note that, as Andar mentioned, modifying every message isn't that big of a deal if you'd prefer a solution with it - there are ways of automatically doing that, though they're probably a bit more complicated than just using the above script.
you're missing one point: only the current map exists in memory, and that includes its events.
so you would have to make one parallel process on each map and play through each map once to have that effect.
Oh, I didn't know that! I thought I could just use a common event to parallelly get the currently shown graphic and replace it at the exact moment it was supposed to be displayed, without having to deal with what is allocated in the memory or something. Thanks!

I think an external approach would be better.
Use any multifile text editor to open all map json files, then search through them for the text commands to decide what to replace automatically.
This will allow you to implement and add text codes without having to go through each message.
Like the yanfly ones for using actor faces and so on.
Also note that, as Andar mentioned, modifying every message isn't that big of a deal if you'd prefer a solution with it - there are ways of automatically doing that
I guess that could work. I wonder what SRD_TranslationEngine's behavior would be, though. I'm pretty sure I would have to do the same procedure in its Translations file with perfect accuracy afterwards. I'd really rather leave that as the last option if possible (although I obviously depend on you all to provide me alternatives).

As to your original idea: please look through the MV forums, on one of them there is a pinned topic about the script equivalents of event commands.
You would want the script equivalent of "change actor graphics" for those cases that can't be handled with the two options above.
Thanks! I love that topic and use it all the time, but would using the actor-based graphic change help me with changing message-based graphics? Anyway, that will definitely be useful for avoiding multiple conditional branches and "Change Actor Images" commands, considering I'll also have to handle that. I appreciate it!

I'm assuming you'd be fine with just replacing certain files with other files? As in, if a character changes outfit, you'd only need to swap the files for that character? If you do have multiple characters per file, it might be a bit more complicated. But for now I'll assume that's not the case.
Absolutely! That was actually my first approach when trying to prompt a plugin. Fortunately, I separated most face files per character from the start, so I don't think I'll even need to touch the ones mixed in a single file when dealing with skins/outfits.

Run this line at the beginning of the game (before any messages are shown):
$gameSystem.faceSwaps = {};
Add that as a plugin:
JavaScript:
Game_Message.prototype.faceName = function () {
return$ gameSystem.faceSwaps[this._faceName] ?? this._faceName;
};
When you want to replace a file (e.g. "Actor1" with "Actor1-summer"), use a script command:
$gameSystem.faceSwaps["Actor1"] = "Actor1-summer";
If you want it to go back to default:
$gameSystem.faceSwaps["Actor1"] = undefined;
The simplicity of this truly brought a sparkle to my eyes. However, for some reason, it doesn't seem to be working for me. I even tried deactivating all the unnecessary plugins (I thought it had been incompatible with YEP_MessageCore).

To clarify, I did add the initialization line to the first event in the game, but I also created a common event to apply it to existing save files. This is the common event:

Spoiler: Initialization Common Event
1714293498122.png

This is the common event I used for handling a skin-change test (I had even added a "show text" command just to check if it was working. Moreover, the Actor Images do change):

Spoiler: Skin-Change Test Common Event
1714293600288.png

This is how I tested it (interacting with the event multiple times didn't change a thing, nor did setting the face index to 0):

Spoiler: Test Event Page
1714293738332.png

Am I doing anything wrong?

One way or the other, I really appreciate your help, @Andar and @Mac15001900 (and this isn't the first time y'all have helped me lol)!
Thanks again!
However, for some reason, it doesn't seem to be working for me
I've looked at it again, and it looks like I've somehow managed to put a space in the wrong place when adding this to the post (return$ gameSystem instead of return $gameSystem) xD
I've fixed that in the above post, try adding the plugin part again.
I've looked at it again, and it looks like I've somehow managed to put a space in the wrong place when adding this to the post (return$ gameSystem instead of return $gameSystem) xD
I've fixed that in the above post, try adding the plugin part again.
I didn't see that either. Haha. Even so, it still won't work.

This is the current plugin:

Spoiler: Screenshot and code
1714296610820.png

JavaScript:
Game_Message.prototype.faceName = function () {
    return $gameSystem.faceSwaps[this._faceName] ?? this._faceName;
};

No matter the position I put it in the plugin manager and even with only the necessary plugins turned on, it won't work. 😕
but would using the actor-based graphic change help me with changing message-based graphics?
not directly - that is why I wrote "it helps with the rest".

basically after you added the text code for party member face into the message feom yanfly's plugin (which is the main reason for the text replaces), then it would work.
without that code and plugin the face is fixed in the message itself, and changing that dynamically is even worse - you would have to analyse every event on the map for its commands, and on finding the show text commands you would have to modify that.
Identifying which of your hundreds of show text commands is which and knowing what file to replace before exchanging that wil be a real problem doing it your way.
No matter the position I put it in the plugin manager and even with only the necessary plugins turned on, it won't work. 😕
Maybe it's something with that event setup, or one of necessary plugins interfering? I've tried this on a fresh project and it behaves as expected:
1714311621085.png
not directly - that is why I wrote "it helps with the rest".

basically after you added the text code for party member face into the message feom yanfly's plugin (which is the main reason for the text replaces), then it would work.
without that code and plugin the face is fixed in the message itself, and changing that dynamically is even worse - you would have to analyse every event on the map for its commands, and on finding the show text commands you would have to modify that.
Identifying which of your hundreds of show text commands is which and knowing what file to replace before exchanging that wil be a real problem doing it your way.
I see. Thank you very much for the information. I'll keep track of the game's performance in case I use this method, and if it's really needed, I'll try going with the external approach.

Maybe it's something with that event setup, or one of necessary plugins interfering? I've tried this on a fresh project and it behaves as expected:
1714311621085.png
Thanks for trying that!

Edit: You can jump to the end of this message, there have been edits and it looks like I've managed to make it work.

Before edit:
Spoiler: Things I tried before editing the message and the code
Well, I tried it that way in my game and it didn't work. Then I tried it on a fresh project too and it won't work either, even when it's the only plugin on the project. I guess I'm doing something wrong, but I can't figure out what exactly.

Let's recapitulate:
1- I add the plugin:
Spoiler: Mac15001900's Face Swaps Plugin
JavaScript:
Game_Message.prototype.faceName = function () {
    return $gameSystem.faceSwaps[this._faceName] ?? this._faceName;
};

2- I initialize it with $gameSystem.faceSwaps = {}; at the beginning of the event or in a separate event (if I don't do either, the game will crash when running the next part).

3- I create an event similar to yours and playtest. The desired effect won't be reached.
Spoiler: Screenshots
1714323209348.png

1714322948311.png


1714322995521.png

What am I missing? :LZSsad:
Knowing it works for you gives me hope that I won't have to do anything more complicated or time-consuming.

Edit: Oh, I've just noticed this on the console:
1714324628462.png


Edit 2: Removing ?? this._faceName from the code made the desired face graphic be displayed. However, the original one is not shown, it's blank.
Spoiler: Screenshots
1714324916213.png

1714324927019.png

Edit 3: Doing this to the code apparently makes it work:
Spoiler: New edited code
JavaScript:
Game_Message.prototype.faceName = function () {
if($gameSystem.faceSwaps[this._faceName] !== undefined)
{
    return $gameSystem.faceSwaps[this._faceName];
}
else
{
return this._faceName;
}
};

Is this the right thing to do? I'll test it out on my project.

Edit 4: The new code seems to work on my project too! Is the code ok? I mean, did I edit it correctly? I still don't get why the ?? or ? didn't work (it was ?? in the plugin, exactly as you sent it).
Ah, are you using RPG Maker 1.5 by any chance? I think that might not support the ?? operator. In hindsight I probably should have asked about it.

I'm glad you managed to get it to work anyway though 🙂 Btw you should also be able to just replace ?? with ||, in this case the behaviour should be the same, and that one will definitely be compatible.
As an explanation: the latest version of RMMV ships with NWJS v0.29.4, which runs on Chromium v65.0.3325.181 (process.versions.chromium). That's the environment used for playtest and non-web deployment, and I believe it has support up to ES9. The nullish-coalescing operator ?? was added in ES11, so is unsupported for MV unless you've manually updated the runtime.
As an explanation: the latest version of RMMV ships with NWJS v0.29.4, which runs on Chromium v65.0.3325.181 (process.versions.chromium). That's the environment used for playtest and non-web deployment, and I believe it has support up to ES9. The nullish-coalescing operator ?? was added in ES11, so is unsupported for MV unless you've manually updated the runtime.
Ah, I see, thank you. I seemed to remember you just needed the newest RPG Maker for it, and I think I've even used it in a fair amount of scripts that people confirmed to work. I guess I just got lucky enough that they had updated nwjs (well, until now xD ). So that is entirely on me, sorry about that.
Ah, are you using RPG Maker 1.5 by any chance? I think that might not support the ?? operator. In hindsight I probably should have asked about it.
Nope, RPG Maker MV 1.6.3.

I'm glad you managed to get it to work anyway though 🙂 Btw you should also be able to just replace ?? with ||, in this case the behaviour should be the same, and that one will definitely be compatible.
Thank you very much! You're right, that worked too!

As an explanation: the latest version of RMMV ships with NWJS v0.29.4, which runs on Chromium v65.0.3325.181 (process.versions.chromium). That's the environment used for playtest and non-web deployment, and I believe it has support up to ES9. The nullish-coalescing operator ?? was added in ES11, so is unsupported for MV unless you've manually updated the runtime.
I can only understand the general meaning, but I guess that explains it. Thanks, @caethyril! :LZSsmile:

Ah, I see, thank you. I seemed to remember you just needed the newest RPG Maker for it, and I think I've even used it in a fair amount of scripts that people confirmed to work. I guess I just got lucky enough that they had updated nwjs (well, until now xD ). So that is entirely on me, sorry about that.
Haha, let's hope everything remains well with them. Now that we know that, it'll be useful for future coding.




Let's sum up what happened in this thread:
I wanted a code for dynamically swapping message faces to create skins and outfits in my game. After many attempts and some discussion on implementation, this is the final Mac15001900_FaceSwaps plugin (I've added it at the end of my plugin list):

Spoiler: Mac15001900's Face Swaps Plugin
JavaScript:
Game_Message.prototype.faceName = function () {
    return $gameSystem.faceSwaps[this._faceName] || this._faceName;
};

It requires initialization with $gameSystem.faceSwaps = {}; at the beginning of the game before any message is shown. If you have a released game, that can be handled through a parallel common event (where switch1, teste, is turned on at the beginning of the game. So, you'll need a switch that will meet every player's current progress):
Spoiler: Initialization Common Event
1714337594432.png

Then you'll have to handle skin/outfit setup, which can be more easily done by following @Andar's and @Mac15001900's tips.

I'll deal with that now, but here is a simple example of how to implement it if you only have a couple of skins. NEW-GABRIELA-SKINS-HIST is the variable I'm using to change the main character's outfit, you'll have to choose a variable and change its value somewhere else to make it work. Alternatively, you can find some other conditions that apply to your project. I also don't think it's necessary to add it to a common event if you set it up properly, but here it is:
Spoiler: Common Event to handle skin change
1714337929617.png

Additionally, if you use name boxes on the message window, you can use SRD_AutoNameBox to automatically display the correct name box according to the file name (you'll have to set up the parameters for each file, though).

Moreover, we should consider @Andar's insights on this implementation when evaluating if it's the best for our projects. There are alternative ways to achieve the same goal. Thank you, @Andar! :LZSproud:



I'd like to immensely thank @Mac15001900 for the help and coding provided in this and other threads! It's been helping me improve my game in ways I could never achieve by myself. I'm beyond grateful! :LZScheeze:

I'll just edit the title and the first post, then I'll report this as solved!
Thank you very much, everyone!
:LZSexcite:

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-force-display-of-another-message-face-image-through-a-script-call.168134/