MZ How to make new enemy sprites appear?

So I'm basically trying to make a way to add enemies to a troop DURING a battle. From a mechanic standpoint, it's working perfectly. The problem is the new enemy's sprite won't show up, which also messes with targeting and spell animations for that enemy.

JavaScript:
BattleManager.refreshEnemies = function() {
    this._spriteset.refresh();
};

Spriteset_Battle.prototype.refresh = function() {
    var sprites = this._enemySprites;
    for (i = 0; i < sprites.length; i++) {
      this._battleField.removeChild(sprites[i]);
    };
    this.createEnemies();
  };

Basically, when I finish adding the enemy to the troop, I call the Battlemanager.refreshEnemies, which refreshes the sprite set. I've tested the clearing of the old sprites, that part works (I think. It throws an error if left by itself, which tells me that code IS being reach and executed). But after clearing the old sprites, I call the built-in function createEnemies, which is supposed to draw ALL the troop members (and the console tells me the new enemies ARE being added to the troop members). But it only redraws the original enemies, and not the new ones.

I'm not graphically inclined, so graphic-related code makes my head spin, but since the function DOES redraw the original troop sprites, I know at least that part of it is working, I'm just apparently screwing up the "draw the new ones too" part. Does anyone happen to have any thoughts on where I'm messing up, or perhaps can point me in the right direction to look? Thanks in advance!
If the javascript code you posted is the only code you've got so far, I'd say the code for "add new sprites to the Spriteset_Battle" would be your next step.

Though, I thought RPG Maker's editor natively had some functionality for hiding/unhiding enemies mid-battle?

Also, some battle plugins have this functionality added (I think visustella's battle engine does).
If the javascript code you posted is the only code you've got so far, I'd say the code for "add new sprites to the Spriteset_Battle" would be your next step.

Though, I thought RPG Maker's editor natively had some functionality for hiding/unhiding enemies mid-battle?

Also, some battle plugins have this functionality added (I think visustella's battle engine does).
JavaScript:
Spriteset_Battle.prototype.createEnemies = function() {
    const enemies = $gameTroop.members();
    const sprites = [];
    for (const enemy of enemies) {
        sprites.push(new Sprite_Enemy(enemy));
    }
    sprites.sort(this.compareEnemySprite.bind(this));
    for (const sprite of sprites) {
        this._battleField.addChild(sprite);
    }
    this._enemySprites = sprites;
};
This is the default sprite creation function in the base script, called when the battle starts, and again when I add new enemies. It pulls all members of the current troop and draws their sprites. That's why I'm not sure why my extra enemies aren't being drawn, as they are added to the game troop members. Even in console, all the sprite information is there, at least from the enemy data, such as the picture url. It draws the original enemies, but not the new one. I even set the new enemy's X and Y to be between the two enemies in the troop I'm testing with, to be sure that I'm not just drawing it offscreen.

As for hiding enemies, that only comes into play if the enemy is set as hidden, and the one I'm adding is not.

As for VisuStella, I'm actually trying to develop my own plugin, inspired by the Enemy Reinforcements plugin from HIME for MV. VisuStella plugins are powerful and capable, but tend to have a LOT of extra features I either don't need or don't want to have to deal with if they change other aspects. That and I read a thread not long ago about someone ranting about having no alternatives to VisuStella and a reply about the "problem" with alternatives being either by request or based on the writer's own needs. So...between all that and my own desire to learn every aspect of plugin/engine methods as I can, I prefer to write my own if I think I can get away with it. I'm not capable of writing my own scenes yet, and am practically graphics-illiterate, so this is basically the simplest starting point for learning MZ's graphics handling. So if I can't even figure THIS out... then custom scenes are a bit further away, especially since I don't want to just copy code without UNDERSTANDING what I'm doing, which is why I haven't done anything with notetags that don't use metadata yet. I've seen plugins using RegEx, even the metadata function itself... and I can't make heads or tails of it. Baby steps.
I totally respect your desire to learn and comprehend what you're even doing! Even though I'm a developer by day, reading other people's plugins to see how they do it was basically how I came to be able to even build JABS (or any other plugins I've written).

I have found one of the most effective means of troubleshooting why things aren't working is simply to console.log() it. I probably have tons of log statements still floating about my code as a result lol. In your case, I'd start by logging out what actually is being returned by $gameTroop.members() (console.log(enemies)). Maybe the enemy isn't being successfully added like you thought? Or maybe investigate what Game_Troop.prototype.members actually looks like. I know for $gameParty.members(), what it returns depends on if you're in battle or not.

A quick glance at Game_Troop.prototype.members looks like it doesn't return anything conditionally like gameParty does, but the this._enemies that it does return appears to be only assigned once: in Game_Troop.prototype.setup... which is also only ran once. Perhaps you already followed that trail, but this is the type of stuff that, though frustrating, is kind of satisfying when you solve it :ahappy:.
Why are you trying to do this by script? You can set an enemy up in the troops tab to "appear halfway" and then just use the Enemy Appear command when you want it to show up.
I totally respect your desire to learn and comprehend what you're even doing! Even though I'm a developer by day, reading other people's plugins to see how they do it was basically how I came to be able to even build JABS (or any other plugins I've written).

I have found one of the most effective means of troubleshooting why things aren't working is simply to console.log() it. I probably have tons of log statements still floating about my code as a result lol. In your case, I'd start by logging out what actually is being returned by $gameTroop.members() (console.log(enemies)). Maybe the enemy isn't being successfully added like you thought? Or maybe investigate what Game_Troop.prototype.members actually looks like. I know for $gameParty.members(), what it returns depends on if you're in battle or not.

A quick glance at Game_Troop.prototype.members looks like it doesn't return anything conditionally like gameParty does, but the this._enemies that it does return appears to be only assigned once: in Game_Troop.prototype.setup... which is also only ran once. Perhaps you already followed that trail, but this is the type of stuff that, though frustrating, is kind of satisfying when you solve it :ahappy:.
JavaScript:
Game_Troop.prototype.addEnemy = function(enemyId, x, y) {
    console.log(enemyId)
    console.log(x)
    console.log(y)
    const enemy = new Game_Enemy(enemyId, x, y);
    this._enemies.push(enemy);
    BattleManager.refreshEnemies();
};
This is the code I use to add an enemy, called with a plugin command. I've got it logging that it's pulling the ID and coords correctly, I even check in console for $gameTroop.members() and it returns all enemies, even the added ones.

Why are you trying to do this by script? You can set an enemy up in the troops tab to "appear halfway" and then just use the Enemy Appear command when you want it to show up.
Part of it is for the practice, part of it is for "people might want something like this", either by added enemies or to have more than 8 enemies in a troop, and part of it is, in my personal project, there's a boss fight where the guy will keep adding additional enemies, sort of a time trial or gear check kind of thing, you have to be able to take him down while not getting overwhelmed. But I want no cap to the number of enemies he adds, or to be dependent on "open" index slots.
You might get some tips, then, if you look at how the engine handles "appear halfway" enemies and see if you can adapt some of the logic to your own needs. See at what point the sprites are loaded, and whether they're on the screen with opacity 0 from the start of battle, or only at the time they "appear".
You might get some tips, then, if you look at how the engine handles "appear halfway" enemies and see if you can adapt some of the logic to your own needs. See at what point the sprites are loaded, and whether they're on the screen with opacity 0 from the start of battle, or only at the time they "appear".
As far as I can tell, when troop members are flagged "appear halfway", they are set as hidden, and are then hidden when created. My additional enemies, at least the way I'm currently handling single enemies from the database, don't carry this information, so they aren't hidden, and their opacity is set to 255. I'm console logging the sprite's opacity, and it shows 255 for both the original sprites and the new ones. So clearly my problem is trying to find out where the actual sprite drawing is done, to see if there's anything that prevents anything other than the database troop from being drawn. I wouldn't think so, but I can't discount the possibility, since everything about the sprite is showing in the data correctly, it's just not actually drawing the picture.
At what point are you assigning the image to the sprite, and where does it happen for 'appear halfway' enemies?

I'm wondering if it's a preloading issue, in that the 'appear halfway' enemies have their sprites loaded when the battle is initiated, but for you, if you're only loading them when you need them, then the sprite is being drawn with an empty bitmap because it hasn't been loaded yet.

I know this is what happens with actor faces in the menu, for some plugins. The menu is drawn but the images haven't been loaded yet so don't get included.
Honestly I think I'm beyond lost here. Following the chain isn't seeming to lead... anywhere. The createEnemies function for the spriteset grabs all members of a troop, sets up the sprites... and then nothing. As far as I can tell, the trail stops cold. Logically, since my function adds a member and forces a recall of the script that created the original sprites (after deleting them all to start fresh), and that called function grabs all members of the troop to do its thing, there should be no difference between the original troop and the newly modified version, save for the new enemy.

All the relevant data points I can find are loaded and correct. As far as everything I can seem to find via script and console is concerned, my troop should now have one more enemy on screen than before, currently set up to appear between the original enemies, roughly at the center of the screen. During the recall, the original enemies are drawn. Why isn't the new one, despite ALL its data being there, called the same way as the others(no custom script for the sprite creation)? WHERE is the drawing actually being done? Can't seem to find it. I can find where the bitmap is loaded... but the trail seems to stop there.

The data says the sprite exists, is where I told it to be, and is visible with full opacity with the correct bitmap. The screen basically says it isn't. (All animations on an enemy are tied to the sprite itself, from it flashing when selecting it as a target to battle animations from skills being played.)

I even set it to add the same kind of enemy that was already in the battle, in case there was some limitation to WHEN the images are loaded... still no dice. Everything, literally EVERYTHING I'm seeing tells me the enemy should be showing up alongside the rest of the troop... but it's not.

I just can't seem to figure it out. Literally the only difference between the original members and the new ones is just that... they're not part of $dataTroops for that troop... but the function uses $gameTroop to get its member information. So why, after removing the sprites, is it only redrawing the original members? It's the EXACT same function, unmodified. It cares about the game troop, not the data troop. The members are added properly. The new sprite is added properly, or at least the information is. The game just seems to be refusing to actually put it on the screen.

...Sorry, I think my rant might have gone a little long. But this seemingly straightforward code has been frustrating me to no end.
At what point are you assigning the image to the sprite, and where does it happen for 'appear halfway' enemies?

I'm wondering if it's a preloading issue, in that the 'appear halfway' enemies have their sprites loaded when the battle is initiated, but for you, if you're only loading them when you need them, then the sprite is being drawn with an empty bitmap because it hasn't been loaded yet.

I know this is what happens with actor faces in the menu, for some plugins. The menu is drawn but the images haven't been loaded yet so don't get included.
I'm not manually setting the images, I'm calling the base script used to set up the original enemies. As far as I can tell, the hidden enemies are loaded the same as the rest, they just have their opacity set to 0 immediately. I'm using the default troop and enemy settings in a new, empty project with just this plugin, to minimize overlooking something.

I'm trying to reduce my opportunity for dumb mistakes as much as possible by reusing the original functions, since they're already working as intended. Normally the enemy creation functions and everything associated with them are ran at the beginning of battle. All I'm doing is adding the enemy to the troop, then calling those functions again. Data-wise, it's working exactly as I assume it should. The enemy fights, can be fought, killed, and its rewards are given. You just can't see it. The console shows me that all the information is filled out like the originals, down to the bitmap data. As far as the data is concerned, they're identical objects.

I've even tried calling a function for the sprite to update, which reloads/refreshes the graphics information, including the bitmap. Still nothing.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-new-enemy-sprites-appear.134572/