startDamagePopup() not working in a "turn end" troop event?

I've been looking into implementing a new mechanic in one of my boss battles where it heals based on how many party members are poisoned. In order to do so, I was using the good ol' tried-and-tested method of calling gainHp(), followed by startDamagePopup().

However, this doesn't actually work when the page condition is set to "Turn End". The healing happens, but the popup does not. Any other condition and it works fine, but not turn end.

Thanks to @bass2yang it seems that the updateTurnEnd function is what's responsible for this, as it's implemented differently to MV's (where this method works fine with that condition).

Has anyone else come across this or found a way to fix it?
Oh, at first I tested in Time Progress mode and didn't see anything, but I get it now.

I think it's an issue with timing. Scene_Battle updates in this order:
  1. BattleManager stuff (including troop events);
  2. Spriteset (including damage popups).
gainHp (step 1) correctly updates the action result, which flags the battler for a battle popup. But if that was the last command, then the interpreter is no longer running (BattleManager.update -> updateEvent -> updateEventMain). So the BattleManager goes on, within that frame, to update the current phase. In the Turn-based battle system, "end turn" involves clearing all battler results. That means when the spriteset (step 2) goes to check "should we show a popup now", the answer is no.

You could add a Wait 1 frame (or any command that takes 1+ frames to process) after the Script command, so the spriteset gets a chance to see the result. E.g.

◆Script:const nme = $gameTroop.members()[0]; : :nme.gainHp(10); : :nme.startDamagePopup(); ◆Wait:1 frame
Alternatively, you could tell the sprite to popup the damage immediately, e.g.
JavaScript:
const nme = $gameTroop.members()[0];
const sprite = SceneManager._scene._spriteset.findTargetSprite(nme);
nme.gainHp(10);
nme.startDamagePopup();
sprite.setupDamagePopup();
Oh, at first I tested in Time Progress mode and didn't see anything, but I get it now.

I think it's an issue with timing. Scene_Battle updates in this order:
  1. BattleManager stuff (including troop events);
  2. Spriteset (including damage popups).
gainHp (step 1) correctly updates the action result, which flags the battler for a battle popup. But if that was the last command, then the interpreter is no longer running (BattleManager.update -> updateEvent -> updateEventMain). So the BattleManager goes on, within that frame, to update the current phase. In the Turn-based battle system, "end turn" involves clearing all battler results. That means when the spriteset (step 2) goes to check "should we show a popup now", the answer is no.

You could add a Wait 1 frame (or any command that takes 1+ frames to process) after the Script command, so the spriteset gets a chance to see the result. E.g.

◆Script:const nme = $gameTroop.members()[0];
: :nme.gainHp(10);
: :nme.startDamagePopup();
◆Wait:1 frame

Alternatively, you could tell the sprite to popup the damage immediately, e.g.
JavaScript:
const nme = $gameTroop.members()[0];
const sprite = SceneManager._scene._spriteset.findTargetSprite(nme);
nme.gainHp(10);
nme.startDamagePopup();
sprite.setupDamagePopup();
Thanks @Trihan and @caethyril for the explanation and working through this - it's good to know that SceneManager has the findTargetSprite - I've been using a filter to go through enemySprites for the enemies. The wait frame does make sense - I remember having to modify the setWaitMode() function to include 'picture' waiting (instead of using the blank/transparent text command to fake a wait for a picture) and needing a 1 wait frame after pressing 'ok' so that it can clear the pictures without re-triggering the interaction with the event.

I tested this going a different route where the area "purges" enemies by damaging a random enemy every turn (sort of like a sanctuary where you have the advantage or it could be something like a NPC that is with you that assists you in damaging your opponent every turn, or it could be a state, etc.).

In case someone is looking to "KO" their opponent, you should probably use changeHp and set the parameter to true to allow for KO in the troop event (which also means you have to define the ._result properties since changeHp doesn't include those properties like in gainHp):
JavaScript:
const m = $gameTroop.aliveMembers();
const r = Math.randomInt(m.length);
const value = 50;
m[r]._result.hpDamage = value;
m[r]._result.hpAffected = true;
m[r].performDamage();
m[r].startDamagePopup();
this.changeHp(m[r], -value, true);
this.wait(1);
it's good to know that SceneManager has the findTargetSprite - I've been using a filter to go through enemySprites for the enemies.
To clarify: findTargetSprite is a method of the spriteset and was added in MZ. The core scripts implement it as a simple Array#find call rather than an inverse map, so it's best to use it sparingly.

In case someone is looking to "KO" their opponent, you should probably use changeHp and set the parameter to true to allow for KO in the troop event (which also means you have to define the ._result properties since changeHp doesn't include those properties like in gainHp)
Good point about the damage cases. 👋

Game_BattlerBase#gainHp calls setHp -> refresh, and Game_Battler#refresh adds/removes the death state as appropriate. With gainHp you do still have to manually call performCollapse on the target when needed; changeHp includes a check for this, in the form:
JavaScript:
if (target.isAlive()) {
  target.gainHp(value)
  if (target.isDead())
    target.performCollapse();
}
(There's no need to manually edit the result, because changeHp calls gainHp.)
To clarify: findTargetSprite is a method of the spriteset and was added in MZ. The core scripts implement it as a simple Array#find call rather than an inverse map, so it's best to use it sparingly.


Good point about the damage cases. 👋

Game_BattlerBase#gainHp calls setHp -> refresh, and Game_Battler#refresh adds/removes the death state as appropriate. With gainHp you do still have to manually call performCollapse on the target when needed; changeHp includes a check for this, in the form:
JavaScript:
if (target.isAlive()) {
  target.gainHp(value)
  if (target.isDead())
    target.performCollapse();
}
(There's no need to manually edit the result, because changeHp calls gainHp.)
Thanks! Maybe I'll pull some of that code and write it into MV for convenience haha.

Also, thank you for the workflow - I skipped over the gainHp() function inside the .changeHp(), thinking it didn't work because I placed it at the bottom (which meant startDamagePopup() doesn't have any values to use).

I realize why I had to do it the way I did which was unnecessary. The .performDamage() function "prevents" the collapse if you put the changeHp before .performDamage() and .changeHp needs to be before .startDamagePopup(). I put the changeHp() at the bottom thinking it would solve everything, but didn't allow for anything for the popup.

The correct order is as you stated:

JavaScript:
const m = $gameTroop.aliveMembers();
const r = Math.randomInt(m.length);
const value = 50;
m[r].performDamage();
this.changeHp(m[r], -value, true);
m[r].startDamagePopup();
this.wait(1);

Thanks for the clarification!
Oh, at first I tested in Time Progress mode and didn't see anything, but I get it now.

I think it's an issue with timing. Scene_Battle updates in this order:
  1. BattleManager stuff (including troop events);
  2. Spriteset (including damage popups).
gainHp (step 1) correctly updates the action result, which flags the battler for a battle popup. But if that was the last command, then the interpreter is no longer running (BattleManager.update -> updateEvent -> updateEventMain). So the BattleManager goes on, within that frame, to update the current phase. In the Turn-based battle system, "end turn" involves clearing all battler results. That means when the spriteset (step 2) goes to check "should we show a popup now", the answer is no.

You could add a Wait 1 frame (or any command that takes 1+ frames to process) after the Script command, so the spriteset gets a chance to see the result. E.g.

◆Script:const nme = $gameTroop.members()[0];
: :nme.gainHp(10);
: :nme.startDamagePopup();
◆Wait:1 frame

Alternatively, you could tell the sprite to popup the damage immediately, e.g.
JavaScript:
const nme = $gameTroop.members()[0];
const sprite = SceneManager._scene._spriteset.findTargetSprite(nme);
nme.gainHp(10);
nme.startDamagePopup();
sprite.setupDamagePopup();
We reached the exact same set of solutions. 🙂
We reached the exact same set of solutions. 🙂
Can confirm that this did happen
It was quite funny. I'm like OH HEY YOU CAN TOTALLY DIRECTLY MANIPULATE THE SPRITESET

And then Tea is like "what if you just wait a few frames"

Original thread: https://forums.rpgmakerweb.com/threads/startdamagepopup-not-working-in-a-turn-end-troop-event.167710/