Add new enemy in a Troop via script?

Hello everyone.
What i'm trying to make is to create a slime that, when hit by certain attacks, he will divide in two copies of itself, but with half of their current HP. The condition to cause the dividing is the easy part, i can use Yanfly's BuffStatesCore plugin for that. The hard part is the effective dividing.
Tinkering with the engine, my first idea was to apply a code like this:
var a = {enemyId: 2, x: 200, y: 200, hidden: false}; $gameTroop.troop().members.push(a); $gameTroop.setup(2); $gameTroop.refreshMembers();
Basically i would force into the array the presence of a new enemy in the battle, and then setup the encounter to make the game re-read the member array. I immediately found myself in front of a problem, though, since this process won't generate a battle sprite for the new enemy, making it 1) invisible, and 2) cause to crash the game if targeted.
Is there a way to add a new, complete enemy in the Troop via script?
Try also calling SceneManager._scene._spriteset.createEnemies(). (though you may have to clear them out first to avoid duplicating enemies that were already there)
Thank you, that worked! Only thing remaining is to fix some visual stuff with other plugins, but other than that is perfect. Thank you again ^^
For anyone interested, here's the complete code for making an enemy divide itself upone being attacked (this method works alongside YEP_BuffStatesCore

Spoiler
<Custom React Effect> if(target.hp > 10) { var hp = Math.floor(target.hp/2); var s = target._speed; var id = target._enemyId; var index = target.index(); var x = []; var y = []; if(Math.random() < 0.5) { x.push(Math.round(target._screenX * 0.85)); x.push(Math.round(target._screenX * 1.2)); } else { x.push(Math.round(target._screenX * 1.2)); x.push(Math.round(target._screenX * 0.85)); } if(Math.random() < 0.5) { y.push(Math.round(target._screenY * 0.9)); y.push(Math.round(target._screenY * 1.1)); } else { y.push(Math.round(target._screenY * 1.1)); y.push(Math.round(target._screenY * 0.9)); } var a = {enemyId: id, x: x[0], y: y[0], hidden: false}; var b = {enemyId: id, x: x[1], y: y[1], hidden: false}; target.addState(1); target.performCollapse(); $gameTroop.troop().members.splice(index, 1, a, b); $gameTroop.setup($gameTroop._troopId); SceneManager._scene._spriteset.createEnemies(); $gameTroop._enemies.forEach(function(enemy, index2) { enemy._speed = s; if(index2 == index || index2 == index+1) { enemy._hp = hp; } }); $gameTroop.refreshMembers(); } </Custom React Effect>
For anyone interested, here's the complete code for making an divide itself upone being attack (this method works alongside YEP_BuffStatesCore

Spoiler
<Custom React Effect> if(target.hp > 10) { var hp = Math.floor(target.hp/2); var s = target._speed; var id = target._enemyId; var index = target.index(); var x = []; var y = []; if(Math.random < 0.5) { x.push(Math.round(target._screenX * 0.85)); x.push(Math.round(target._screenX * 1.2)); } else { x.push(Math.round(target._screenX * 1.2)); x.push(Math.round(target._screenX * 0.85)); } if(Math.random < 0.5) { y.push(Math.round(target._screenY * 0.9)); y.push(Math.round(target._screenY * 1.1)); } else { y.push(Math.round(target._screenY * 1.1)); y.push(Math.round(target._screenY * 0.9)); } var a = {enemyId: id, x: x[0], y: y[0], hidden: false}; var b = {enemyId: id, x: x[1], y: y[1], hidden: false}; target.addState(1); target.performCollapse(); $gameTroop.troop().members.splice(index, 1, a, b); $gameTroop.setup($gameTroop._troopId); SceneManager._scene._spriteset.createEnemies(); $gameTroop._enemies.forEach(function(enemy, index2) { enemy._speed = s; if(index2 == index || index2 == index+1) { enemy._hp = hp; } }); $gameTroop.refreshMembers(); } </Custom React Effect>
Just a heads-up: Math.random() is a function, not a property, so your if statements will always return false until you put the brackets in.
Just a heads-up: Math.random() is a function, not a property, so your if statements will always return false until you put the brackets in.
ops, must have been distracted. Thanks for telling me

EDIT: mhh, now it emerged a new problem. Sometimes, when i cut a slime in two, the sprite of the old slime will still remain there, even if it's dead. This happen if you cut the first slime, then one of the two emerged, and then the other you didn't.

Spoiler
Immagine 2021-11-05 220214.png


Immagine 2021-11-05 220254.png


Immagine 2021-11-05 220334.png
Honestly you may be going overboard as you can set up troops that are not visible and then use the event system to make them visible as needed. That way each copy is a separate entity so you should have an easier time dealing with the dead ones.

1636153963786.png


Check out the various system calls the event system uses if you want to script that as it may well make your life easier than trying to just script something that the system can possibly do 🙂
Honestly you may be going overboard as you can set up troops that are not visible and then use the event system to make them visible as needed. That way each copy is a separate entity so you should have an easier time dealing with the dead ones.

View attachment 205935

Check out the various system calls the event system uses if you want to script that as it may well make your life easier than trying to just script something that the system can possibly do 🙂
I was going to suggest this solution but then you wouldn't be able to have more than 2 slimes in battle at any one time.
Honestly you may be going overboard as you can set up troops that are not visible and then use the event system to make them visible as needed. That way each copy is a separate entity so you should have an easier time dealing with the dead ones.

View attachment 205935

Check out the various system calls the event system uses if you want to script that as it may well make your life easier than trying to just script something that the system can possibly do 🙂
I know about the possibility, but my problem is that i want this to be a dynamic system, so i can use the Slime for multiple fights and with other enemies as well
I know about the possibility, but my problem is that i want this to be a dynamic system, so i can use the Slime for multiple fights and with other enemies as well
Regarding your sprite ghost issue, did you delete the enemy sprites before calling createEnemies()?
Ah. You're getting fancier than I do then. I just have a separate troop for each fight. If I decide to get too complex in RM then I start looking at the other engines I have to see if that project is better moved to one of them.
Regarding your sprite ghost issue, did you delete the enemy sprites before calling createEnemies()?
i assigned the death state to the slime and called the collapse animation before that. But i think i found the problem: basically, whenever i do $gameTroop.setup(), all the enemies that are not affected by the current division get reset as well. This means that, if i have two slime copies on the field and i attack one, the other will be reset. This for some reason i think causes the sprite to not disappear upon death, but i don't know how to solve this
I think to have half of the solution, but i still miss something:

Spoiler
<Custom React Effect> if(target._hp > 10) { var hp = Math.floor(target._hp/2); var tp = target._tp; var s = target._speed; var id = target._enemyId; var index = target.index(); var x = [0, 0]; var y = [0, 0]; x[0] = Math.round(target._screenX * (Math.random() * 0.36 + 0.85)); x[1] = (x[0] < target._screenX*1.02) ? Math.round(target._screenX * (Math.random() * 0.19 + 1.02)) : Math.round(target._screenX * (Math.random() * 0.18 + 0.85)); y[0] = Math.round(target._screenY * (Math.random() * 1.1 + 0.5)); y[1] = (x[0] < target._screenX) ? Math.round(target._screenY * (Math.random() * 0.6 + 1)) : Math.round(target._screenY * (Math.random() * 0.5 + 0.5)); target.addNewState(1); target.deadMotion(); var members = $gameTroop._enemies.slice(); var a = {enemyId: id, x: Math.min(Math.max(x[0], 10), 600), y: Math.min(Math.max(y[0], 300), 500), hidden: false}; var b = {enemyId: id, x: Math.min(Math.max(x[1], 10), 600), y: Math.min(Math.max(y[1], 300), 500), hidden: false}; $gameTroop.troop().members.splice(index, 1, a, b); $gameTroop.setup($gameTroop._troopId); SceneManager._scene._spriteset.createEnemies(); $gameTroop._enemies.forEach(function(enemy, index2) { if(index2 == index || index2 == index+1) { enemy._hp = hp; enemy._speed = s; enemy._tp = tp; } else { if(index2 > index) { enemy = members[0][index2-(index2-index)]; } else { enemy = members[0][index2]; } } }); } </Custom React Effect>

The idea was to copy the enemies on the field before doing the setup again, and then put the enemies not affected by the last division back in the field with their current HP, states, debuffs and what not. Problem is, i seem to not be able to dereference the copy of the old enemy
I have an alternative Suggestion: You could use the Hime plugin https://himeworks.com/2015/11/enemy-reinforcements-mv/
I use it in my game and it seems to work pretty well. Even if you can't accomplish what you want exactly you can probably figure out what you are missing by looking at the code.
Alright, i believe to have fixed it! Following eomereolsson's advice, i used HIME's Enemy Reinforcements as a base for my code:

Spoiler
<Custom React Effect> if(target._hp > 10) { var hp = Math.floor(target._hp/2); var tp = target._tp; var s = target._speed; var id = target._enemyId; var index = target.index(); var x = [0, 0]; var y = [0, 0]; x[0] = Math.round(target._screenX * (Math.random() * 0.36 + 0.85)); y[0] = Math.round(target._screenY * (Math.random() * 1.1 + 0.5)); x[1] = (x[0] < target._screenX*1.02) ? Math.round(target._screenX * (Math.random() * 0.19 + 1.02)) : Math.round(target._screenX * (Math.random() * 0.18 + 0.85)); y[1] = (y[0] < target._screenY) ? Math.round(target._screenY * (Math.random() * 0.6 + 1)) : Math.round(target._screenY * (Math.random() * 0.5 + 0.5)); target.addNewState(1); target.deadMotion(); $gameTroop.refreshMembers(); var a = {enemyId: id, x: Math.min(Math.max(x[0], 10), 600), y: Math.min(Math.max(y[0], 300), 500), hidden: false}; var b = {enemyId: id, x: Math.min(Math.max(x[1], 10), 600), y: Math.min(Math.max(y[1], 300), 500), hidden: false}; $gameTroop.members().splice(index, 1, new Game_Enemy(a.enemyId, a.x, a.y), new Game_Enemy(b.enemyId, b.x, b.y)); $gameTroop._enemies.forEach(function(enemy, index2) { if(index2 == index+1 || index2 == index) { enemy._hp = hp; enemy._speed = s; enemy._tp = tp; } }); $gameTroop.makeUniqueNames(); setTimeout(function() { SceneManager._scene._spriteset.removeEnemies(); SceneManager._scene._spriteset.createEnemies(); }, 600); } </Custom React Effect>

Original thread: https://forums.rpgmakerweb.com/threads/add-new-enemy-in-a-troop-via-script.141881/