MZ Question about sprite sheet file name swapping

Hi folks,

My game uses a number of plugins and events to swap the player sprite sheet, whether it is for playing an animation, displaying diagonal movement sprites via plugin, or showing idle/running animations via plugin.

Something I would like to do is to add other character sprite options (gender options) to my game. The issue I have is that my game already has a ton of animations which would need new conditions and alternative sprite sheets set, and many features which are tied to the actor ID directly (so just adding a new actor for the other gender so that the plugins get a different sprite sheet for diagonals/idle/running wont work).

So my question is...

Is it theoretically possible for a plugin to be made which would "swap" the name of one sprite sheet to another?

So if I have a sprite sheet named "Main_Char1" which all my plugins/movement routes reference, I could change another sprite sheet named "Main_Char2" to now be treated as "Main_Char1", effectively causing everything to now reference the new sprite sheet.

In short, I want to potentially commission a plugin which would "swap" all my existing sprite sheets to another sheet, changing how everything I have already set up would display, but am not sure if it even possible.

I am open to any alternative suggestions to implement the same sort of functionality as well.

Thanks!
depends on what exactly you mean.

you cannot change the filenames themselves, but you can change the string of the filename stored in the database.

unfortunately that can get more complex depending on what exactly your existing extensions and changes require, especially if you're using the event command change actor graphics anywhere - because that command does not check what is already there.

Please post examples of how you handle the already existing changes in your game, then we should be able to give you more detailed advice.
depends on what exactly you mean.

you cannot change the filenames themselves, but you can change the string of the filename stored in the database.

unfortunately that can get more complex depending on what exactly your existing extensions and changes require, especially if you're using the event command change actor graphics anywhere - because that command does not check what is already there.

Please post examples of how you handle the already existing changes in your game, then we should be able to give you more detailed advice.

The specific examples are:

- Movement routes (change image)
- Galv's Diagonal Movement plugin (gets images for character diagonals from the sprite beneath the current current chracter's default sheet)
- Galv's Character Animations (Uses the first 3 sprites on the current character sprite sheet for idle/walk/run animations)

The toughest one here is Galv's Diagonal Movement, as it doesn't care about what the "current" sprite is, such as changing the character's image via a movement route. It always takes from the sprite below the default sprite sheet for the character, so I can't think of what to do other than making the sprite it is pointing to into something different.
Is it theoretically possible for a plugin to be made which would "swap" the name of one sprite sheet to another?
Sure, it should be fairly simple. For example:
JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if(filename.startsWith("Main") && $gameSwitches.value(42)) filename += "-F";
        return alias.call(this, folder, filename);      
    }
})(ImageManager.loadBitmap);
This will make it so that whenever you load any image with a name that starts with "Main" and switch 42 is true, it will append "-F" to the filename. This will work for any images - spritesheets, face images etc.
Sure, it should be fairly simple. For example:
JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if(filename.startsWith("Main") && $gameSwitches.value(42)) filename += "-F";
        alias.call(this, folder, filename);     
    }
})(ImageManager.loadBitmap);
This will make it so that whenever you load any image with a name that starts with "Main" and switch 42 is true, it will append "-F" to the filename. This will work for any images - spritesheets, face images etc.

I gave it a try, using this code in a script command:

JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if(filename.startsWith("!Main") && $gameSwitches.value(666)) filename += "F";
        alias.call(this, folder, filename);      
    }
})(ImageManager.loadBitmap);
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if(filename.startsWith("!Secondary") && $gameSwitches.value(666)) filename == "!Main Char Sheet";
        alias.call(this, folder, filename);      
    }
})(ImageManager.loadBitmap);

It gave me an error when running it, though
("Cannot read property 'addLoadListener' of undefined")

I had a file named "!Main Character" and one named "!Secondary Character", so the idea was to add something to the "!Main Character" so that I don't have a duplicate, and then set "!Secondary Character" to instead be "!Main Character", so that everything points to it instead.
My ability to debug MZ problems without having it is rather limited, but showing the full error in console could definitely help. Press F8, select the 'console' tab, click on the triangles next to any red messages to expand them, and screenshot that.

Some further notes about your changes (they probably won't prevent the error though):
- You can combine those options into a single alias, e.g.
JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if($gameSwitches.value(666)){
            if(filename.startsWith("!Main")) filename += "F";   
            else if(filename.startsWith("!Secondary")) filename = "!Main Char Sheet";
        }       
        alias.call(this, folder, filename);     
    }
})(ImageManager.loadBitmap);
- In your second one you've used == instead of = (assuming simple reassignment is what you wanted).
My ability to debug MZ problems without having it is rather limited, but showing the full error in console could definitely help. Press F8, select the 'console' tab, click on the triangles next to any red messages to expand them, and screenshot that.

Some further notes about your changes (they probably won't prevent the error though):
- You can combine those options into a single alias, e.g.
JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if($gameSwitches.value(666)){
            if(filename.startsWith("!Main")) filename += "F";  
            else if(filename.startsWith("!Secondary")) filename = "!Main Char Sheet";
        }      
        alias.call(this, folder, filename);    
    }
})(ImageManager.loadBitmap);
- In your second one you've used == instead of = (assuming simple reassignment is what you wanted).

I tried turning off most of my plugins (Just leaving VisuStella Core and Galv's Layer Graphics on, as my game wont load without them), and got this console error:


Spoiler
Code:
rmmz_managers.js:2036 TypeError: Cannot read property 'width' of undefined
    at Sprite_Character.patternWidth (rmmz_sprites.js:369)
    at Sprite_Character.updateCharacterFrame (rmmz_sprites.js:322)
    at Sprite_Character.updateFrame (rmmz_sprites.js:308)
    at Sprite_Character.update (rmmz_sprites.js:241)
    at Tilemap.update (rmmz_core.js:2332)
    at Sprite.update (rmmz_core.js:1987)
    at Spriteset_Map.Sprite.update (rmmz_core.js:1987)
    at Spriteset_Map.Spriteset_Base.update (rmmz_sprites.js:3158)
    at Spriteset_Map.Spriteset_Base.<computed>.<computed> [as update] (VisuMZ_0_CoreEngine.js:9231)
    at Spriteset_Map.update (rmmz_sprites.js:3379)

The other error (with plugins enabled was:
Spoiler
Code:
TypeError: Cannot read property 'addLoadListener' of undefined
    at Sprite_Character.addLoadListenerOnCharacterBitmap (EliMZ_Book.js:2442)
    at Sprite_Character.setCharacterBitmap (EliMZ_Book.js:2438)
    at Sprite_Character.updateBitmap (rmmz_sprites.js:281)
    at Sprite_Character.update (rmmz_sprites.js:240)
    at Sprite_Character.update (Galv_ShadowDarkenMZ.js:138)
    at Tilemap.update (rmmz_core.js:2332)
    at Sprite.update (rmmz_core.js:1987)
    at Spriteset_Map.Sprite.update (rmmz_core.js:1987)
    at Spriteset_Map.Spriteset_Base.update (rmmz_sprites.js:3158)
    at Spriteset_Map.Spriteset_Base.<computed>.<computed> (VisuMZ_0_CoreEngine.js:9231)
I tried turning off most of my plugins
any change in the plugin manager means that you need to start a new game for a correct setup of those plugins, because most plugins store their data in savefiles, and as a result savefiles are only compatible to the same plugin configuration they are saved under.

did you start new games before getting those errors, or did you load savegames?
if those errors came from savegames, please test again with new games.
I tried turning off most of my plugins (Just leaving VisuStella Core and Galv's Layer Graphics on, as my game wont load without them), and got this console error:
Ah, that one makes more sense. Looks like I forgot to actually return the value from the alias. Try this:
JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if($gameSwitches.value(666)){
            if(filename.startsWith("!Main")) filename += "F";   
            else if(filename.startsWith("!Secondary")) filename = "!Main Char Sheet";
        }       
        return alias.call(this, folder, filename);
    }
})(ImageManager.loadBitmap);

If you run into further problems with that, I'd recommend trying with filenames that don't have spaces or special characters.
Ah, that one makes more sense. Looks like I forgot to actually return the value from the alias. Try this:
JavaScript:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if($gameSwitches.value(666)){
            if(filename.startsWith("!Main")) filename += "F"; 
            else if(filename.startsWith("!Secondary")) filename = "!Main Char Sheet";
        }     
        return alias.call(this, folder, filename);
    }
})(ImageManager.loadBitmap);

If you run into further problems with that, I'd recommend trying with filenames that don't have spaces or special characters.

This didn't give me any bugs now, but unfortunately didn't do anything when run.

I took out the spaces and tried with some different sprite sheets, but it still didn't change anything (even after reloading the map etc.). I guess once the game has loaded in the default sprite sheet, it doesn't re-load it?

This was the code with updated file names:
Code:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if($gameSwitches.value(666)){
            if(filename.startsWith("!SMain2")) filename += "F";  
            else if(filename.startsWith("!SMain1")) filename = "!SMain2";
        }      
        return alias.call(this, folder, filename);
    }
})(ImageManager.loadBitmap);
This didn't give me any bugs now, but unfortunately didn't do anything when run.

I took out the spaces and tried with some different sprite sheets, but it still didn't change anything (even after reloading the map etc.). I guess once the game has loaded in the default sprite sheet, it doesn't re-load it?

This was the code with updated file names:
Code:
void ((alias) => {
    ImageManager.loadBitmap = function(folder, filename) {
        if($gameSwitches.value(666)){
            if(filename.startsWith("!SMain2")) filename += "F"; 
            else if(filename.startsWith("!SMain1")) filename = "!SMain2";
        }     
        return alias.call(this, folder, filename);
    }
})(ImageManager.loadBitmap);
I'm assuming this is the spritesheet for the player? Does it work for modifying ones for events?
You might also want to use the event command for changing the player's graphics, that might cause a reload when you change back.

If that doesn't work, you might need to find someone with MZ to help you, that's about all I can do.
I think you need to put a check inside character sprite class' update function, I forgot the exact name. Because it has a reference to the character.
Should be easy to swap bitmaps there.
Or you put check in the update function of the game player class, setImage if conditions met.
I feel like it might be easier to use JavaScript string operators in the places where you need to reference the other files.

For example, if you have MainCharM_regular and MainCharM_run (and same with MainCharF), you could go from regular to run with
Code:
let name=user.battlerName().substring(-1, user.battlerName().indexOf('_'));
name += "_run";
user.setBattlerImage(name);

You'd have to modify that in your events/script calls, so maybe trying to do the interception/internal modification of the filename is easier, if you can get it to work.

Original thread: https://forums.rpgmakerweb.com/threads/question-about-sprite-sheet-file-name-swapping.170552/