Swapping enemy sprite when affected by state

I have a polymorph ability that is mechanically similar to a rage ability, so it is important that the target’s base stats are taken into consideration. The enemy transform function does not serve my purpose, I only want to change the sprite of the affected enemy.

I was able to find the code for swapping enemy sprites and patch it into Yanfly’s Enemy Transformations example to get it reverting back perfectly… with the one exception that it does not change the correct sprite.

Screenshot (335).png
Screenshot (336).png


I’m using BattleManager._spriteset._enemySprites[target.index()].bitmap = ImageManager.loadEnemy("Cockatrice");, but the sprite that is changed is based on the order of the enemy with the highest y position to the lowest… Not their index in the troop. In the first image, Bat A was targeted, gains the state counter and mechanical effects of the state, but Bat B is the one whose sprite changes. If I position them awkwardly like the second image, the effect lines up with the enemy that actually has the state.

This happened in my attempts before downloading the prerequisite yanfly plugins for the Enemy Transform function, and persisted after I removed every non-prerequisite plugin, so it doesn't seem related to that.
I have not found a different index based on screen position to call, even if it was based on the x position, that would be an okay workaround. I also tried BattleManager._spriteset._enemySprites[this._targetIndex].bitmap = ImageManager.loadEnemy("Cockatrice");, but it didn't seem to parse.


As a secondary question, is there any etiquette regarding opening multiple support threads at the same time, time between making new threads, or asking multiple questions within a single thread? I wasn't sure who/where would be the right place to ask.
Yanly gives the battler() method to get the sprite of the enemy (at least I think it comes from Yanfly).
So write it like:
target.battler().bitmap = ImageManager.loadEnemy("Cockatrice");
Yanly gives the battler() method to get the sprite of the enemy (at least I think it comes from Yanfly).
So write it like:
target.battler().bitmap = ImageManager.loadEnemy("Cockatrice");
This seems to have done the trick!
I'll do a little more testing to make sure everything is working 100%, but in the meantime, I have a couple more questions to make sure I fully understand the code.

Does the battler method contain all information on an enemy, and then battler().bitmap calls the image that is stored at that value? Does it work on allies as well so that user.battler() would (in this case) call the party member who cast the spell?

I also don't 100% understand how the Notes area work. It seems to mostly be used for interacting with plugins, but the base engine sometimes leaves descriptions there. Is the idea that anything in plain text there is literally just comments and notes, and then if you want to modify code it has to be within a set of note tags?
Cool.

Doesn't the battler method just return the sprite? I checked it half a year ago and don't have motivation to recheck, but IIRC Yanfly adds the sprite as a property of the battler? And that method gives you the sprite. It works for actors too. But the .battler() doesn't give the battler, it gives the sprite.

(// EDIT: I rechecked it, Yanfly stores the sprites to BattleManager._registeredSprites array)

I think the base engine notes just say something about skill 1 being attack and skill 2 being guard. It's just text and it's in .note property. MV and MZ has a built in code that parses the notes and stores stuff written as <key:value> to a meta property of the object. So you can read stuff written in that format in a simplified way without having to write your own parser function. It's up to you how you use that.

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.

Re-opened at OP's request.

I ended up finding another graphical quirk in my setup.
If the spell is cast on an enemy that is already polymorphed, it will save the polymorph sprite over the original sprite, so when the state expires it remains with the polymorph bitmap.

I attempted to add a fix by putting the line that saves the original bitmap in an if statement for when the target is not already affected by the state, but the state expiring on any enemy, even where the spell was only cast once, brings up the following error:
Spoiler: Error Message
Screenshot (360).png

I suspect the reason is that because the conditional is within the state's notes, the state is already technically applied, even if the effects aren't fully active yet.

Here are my notes for the state. Like I mentioned, I used the code here as a base for swapping the bitmap, so it has some of the original functionality commented out and target._prevEnemyId is reused to save the bitmap instead.
Spoiler: State Notes
Code:
<Custom Apply Effect>
// Check if the user is an enemy.
if (target.isEnemy()) {
  // Archive the previous enemy ID.
  if (target.isStateAffected(13) == false){ //rethy fix attempt
  target._prevEnemyId = target.battler().bitmap;}
  // Set the target enemy ID you want the enemy to change into.
  // var transformEnemyId = 51;
  // Transform the user.
  //BattleManager._spriteset._enemySprites[target.index()].bitmap = ImageManager.loadEnemy("Cockatrice");//coyotecraft on the forums
  target.battler().bitmap = ImageManager.loadEnemy("Cockatrice"); //rpgLord69 on forums
}
</Custom Apply Effect>

<Custom Remove Effect>
// Check if the user is an enemy.
if (target.isEnemy()) {
  // Retrieve the archived enemy ID.
  //var transformEnemyId = target._prevEnemyId;
  // Transform the enemy back.
  target.battler().bitmap = target._prevEnemyId;
  // Clear the archived data.
  target._prevEnemyId = undefined;
}
</Custom Remove Effect>

I tried using <Reapply Ignore Turns> from the Yanfly BuffsStates Core, but <Custom Apply Effect> runs regardless of if any turns were added.
apply effects run after the state is on the battler, so trying to check if its on doesnt get you anywhere. try something like this here you use a property to check instead.
Code:
<Custom Apply Effect>
// Check if the user is an enemy.
if (target.isEnemy() && !target._prevEnemyId) {
  // Archive the previous enemy ID.
  target._prevEnemyId = target.battler().bitmap;
  // Transform the user.
  target.battler().bitmap = ImageManager.loadEnemy("Cockatrice"); //rpgLord69 on forums
}
</Custom Apply Effect>

<Custom Remove Effect>
// Check if the user is an enemy.
if (target.isEnemy() && target._prevEnemyId) {
  // Transform the enemy back.
  target.battler().bitmap = target._prevEnemyId;
  // Clear the archived data.
  delete target._prevEnemyId;
}
</Custom Remove Effect>
It works! I don't think I would've thought of checking it that way on my own, thank you.

Original thread: https://forums.rpgmakerweb.com/threads/swapping-enemy-sprite-when-affected-by-state.182072/