MZ Javascript for adding new enemies into the battle

Hi,

I'm trying to find a way to use a javascript to add new enemies into the battle. The idea is that I have a status which, when it expires a new enemy gets added into the battle. I'm using Visustella's "skills and states core" plugin and "On Expire State" notetag.

Currently I have this code:

<JS On Expire State>
const NewEnemy = new Game_Enemy(2,200,200);
$gameTroop.members().push(NewEnemy);
const sprite = new Sprite_Enemy();
sprite.setBattler(NewEnemy);
const spriteset = SceneManager._scene._spriteset;
spriteset._battlefield.addChild(sprite);
spriteset._enemySprites.push(sprite);
</JS On Expire State>

It only manages to create an enemy that only appears in target menu and it is killable, but it doesn't do anything and it isn't visible.

If I can get this to work I can continue to develop this mechanic further and make it find an empty spot on the battlefield and spawn an enemy there and which enemy appears depends on various conditions.
Hi,

I'm trying to find a way to use a javascript to add new enemies into the battle. The idea is that I have a status which, when it expires a new enemy gets added into the battle. I'm using Visustella's "skills and states core" plugin and "On Expire State" notetag.

Currently I have this code:

<JS On Expire State>
const NewEnemy = new Game_Enemy(2,200,200);
$gameTroop.members().push(NewEnemy);
const sprite = new Sprite_Enemy();
sprite.setBattler(NewEnemy);
const spriteset = SceneManager._scene._spriteset;
spriteset._battlefield.addChild(sprite);
spriteset._enemySprites.push(sprite);
</JS On Expire State>

It only manages to create an enemy that only appears in target menu and it is killable, but it doesn't do anything and it isn't visible.

If I can get this to work I can continue to develop this mechanic further and make it find an empty spot on the battlefield and spawn an enemy there and which enemy appears depends on various conditions.
Probably need some sort refresh call, either on the battler or the troop
Probably need some sort refresh call, either on the battler or the troop
Unfortunately this wasn't that simple problem to solve. 🙁

I've made some tests and I've found some more problems. First of all the previous code didn't work at all meaning the code didn't even run completely. I tested it with $gameMessage.add("test"); between every line and it stopped at the third line. I made some changes and currently I have this code:

const NewEnemy = new Game_Enemy(2,200,200);
$gameTroop.members().push(NewEnemy);
const sprite = new Sprite_Enemy(NewEnemy);
const spriteset = SceneManager._scene._spriteset;
spriteset._battlefield.addChild(sprite);
spriteset._enemySprites.push(sprite);

Now it fails at "spriteset._battlefield.addChild(sprite);". But that's not all I also noticed that even if I managed to give the newly created enemy their sprite they still wouldn't do anything because for some reason their tpb gauge doesn't fill up at all. They only take action in turn based battle system.
Ok, Now I've made some real progress. Hopefully at least...


<JS On Expire State>

const NewEnemy = new Game_Enemy(2,200,200);

$gameTroop.members().push(NewEnemy);

const sprite = new Sprite_Enemy(NewEnemy);

const spriteset = BattleManager._spriteset;

spriteset.addChild(sprite);

spriteset._enemySprites.push(sprite);

BattleManager.setSpriteset(spriteset);

</JS On Expire State>

The enemy appears as it should, but it won't do anything in time progress battle mode. This will work in turn based battle and it seems like it can keep spawning an infinite number of enemies.

But now I have no idea how to make these new enemies take part in time progress battle and I can't even find any scriptcalls that have anything to do with tpb. Can anyone help me?
Game_Enemy only accepts one argument unless it has been modified by external plugins

You do not need to reset the Battlemanager link to the spriteset, you want to refresh the spriteset, if the sprite is not visible.

Sprites are added to spriteset._battlefield and not just spriteset based on core scripts

My advice accounts for the base engine and not any special modification to the core scripts.
Now I have TPB working as it should. I just needed to add "NewEnemy.onBattleStart();". Now I noticed a problem where the sprites appear in wrong order on screen. For example the damage numbers are displayed behind the enemy sprite and enemy sprite that should be on the back row appears on top of all other enemy sprites. How can I fix this?


const NewEnemy = new Game_Enemy(1,800,800);

$gameTroop.members().push(NewEnemy);

const sprite = new Sprite_Enemy(NewEnemy);

const spriteset = BattleManager._spriteset;

spriteset.addChild(sprite);

spriteset._enemySprites.push(sprite);

BattleManager.update();

NewEnemy.onBattleStart();

Sprites are added to spriteset._battlefield and not just spriteset based on core scripts
I tried that with "spriteset._battlefield.addChild(sprite);" but it doesn't work at all. I don't know why it refuses to even run that code. "spriteset.addChild(sprite);" at least seems to work. I don't know if that is causing the damage numbers to appear behind the enemy sprites.
Now I have TPB working as it should. I just needed to add "NewEnemy.onBattleStart();". Now I noticed a problem where the sprites appear in wrong order on screen. For example the damage numbers are displayed behind the enemy sprite and enemy sprite that should be on the back row appears on top of all other enemy sprites. How can I fix this?


const NewEnemy = new Game_Enemy(1,800,800);

$gameTroop.members().push(NewEnemy);

const sprite = new Sprite_Enemy(NewEnemy);

const spriteset = BattleManager._spriteset;

spriteset.addChild(sprite);

spriteset._enemySprites.push(sprite);

BattleManager.update();

NewEnemy.onBattleStart();


I tried that with "spriteset._battlefield.addChild(sprite);" but it doesn't work at all. I don't know why it refuses to even run that code. "spriteset.addChild(sprite);" at least seems to work. I don't know if that is causing the damage numbers to appear behind the enemy sprites.

Glad you got it to work, it's still pretty strange how your code executes. As for the damage behind battlers, that's just layering. I think damage sprites are added to the battle field rather than the sprite if it appears behind rather than in front
Glad you got it to work, it's still pretty strange how your code executes. As for the damage behind battlers, that's just layering. I think damage sprites are added to the battle field rather than the sprite if it appears behind rather than in front
Ok, I managed to fix the damage popup issue with "sprite.updateDamagePopup();" , but I'm not sure how I can change the layering of enemy sprites. I've tried updating the enemy spriteset and reordering enemy spriteset array, but the newest enemy sprite will still appear in front of all other enemy sprites.
Ok, I managed to fix the damage popup issue with "sprite.updateDamagePopup();" , but I'm not sure how I can change the layering of enemy sprites. I've tried updating the enemy spriteset and reordering enemy spriteset array, but the newest enemy sprite will still appear in front of all other enemy sprites.
This has to do with how AddChild works. It always puts the next child on top of the previous. You can use AddChildAt and specify an index to put it at a specific index in the child stack
Ok, I managed to fix the damage popup issue with "sprite.updateDamagePopup();" , but I'm not sure how I can change the layering of enemy sprites. I've tried updating the enemy spriteset and reordering enemy spriteset array, but the newest enemy sprite will still appear in front of all other enemy sprites.
It will always appear on top if you're using addChild function.

You can try throwing in a sort function there and see how it goes.
This has to do with how AddChild works. It always puts the next child on top of the previous. You can use AddChildAt and specify an index to put it at a specific index in the child stack
Thanks! Now it seems to work!

Also, I noticed that the only reason "spriteset._battleField.addChild(sprite);" didn't work was... I forgot to use capital F... That was embarrasing...

Original thread: https://forums.rpgmakerweb.com/threads/javascript-for-adding-new-enemies-into-the-battle.144770/