Doom state and animations

I've created a Doom-like state. I'm using the VisuStella Skills & State plugin to play an animation when the state expires, then set the target's health to 0. It works... but not visually in the way I want.

Code:
<JS On Erase State>
if (target.stateTurns(stateId) <= 0) {
  $gameTemp.requestAnimation([target], 136); //Doom
  target.addState(1);
}
</JS On Erase State>

The problem is, the target dies before the animation plays. I tried adding a timeout, but that causes other problems; the current target in the battle command queue can be deemed to be the one afflicted, or no-one dies at all.

Code:
<JS On Erase State>
if (target.stateTurns(stateId) <= 0) {
  $gameTemp.requestAnimation([target], 136); //Doom

  setTimeout(() => {
    target.addState(1);
    target.refresh();
  }, 1200);
}
</JS On Erase State>

I tried using target.SetHp(0) but the problem remains.
I want this fix to be embedded in the state, not in Troop commands.
The state should work whether applied to one or many targets.
The state should work whether applied to enemies or allies.

Any suggestions?
I've created a Doom-like state. I'm using the VisuStella Skills & State plugin to play an animation when the state expires, then set the target's health to 0. It works... but not visually in the way I want.

Code:
<JS On Erase State>
if (target.stateTurns(stateId) <= 0) {
  $gameTemp.requestAnimation([target], 136); //Doom
  target.addState(1);
}
</JS On Erase State>

The problem is, the target dies before the animation plays. I tried adding a timeout, but that causes other problems; the current target in the battle command queue can be deemed to be the one afflicted, or no-one dies at all.

Code:
<JS On Erase State>
if (target.stateTurns(stateId) <= 0) {
  $gameTemp.requestAnimation([target], 136); //Doom

  setTimeout(() => {
    target.addState(1);
    target.refresh();
  }, 1200);
}
</JS On Erase State>

I tried using target.SetHp(0) but the problem remains.
I want this fix to be embedded in the state, not in Troop commands.
The state should work whether applied to one or many targets.
The state should work whether applied to enemies or allies.

Any suggestions?

SOLUTION

The workaround is to call a Common event to pause, allowing the animation to complete, and then apply the Dead state. This requires passing the ID of the afflicted actor via a game variable.

Because the Doom state could apply to multiple targets, enemies or allies, an array of Battlers must be passed from the State to a Common Event. A game variable can do this, but what if another skill uses this variable during battle? The fix is to create a dedicated Doom Queue property in the BattleManager.

Code:
<No Death Clear>
<JS On Erase State>
$gameTemp.requestAnimation([target], 136);

// Initialize custom Doom queue on the BattleManager
// Does not require using a game variable.
BattleManager._doomQueue = BattleManager._doomQueue || [];

// Add target to the Doom queue.
// Targets can be enemies or allies.
BattleManager._doomQueue.push(target);

// Trigger the Instakill Common Event.
// This allows the Doom animation to play before killing target.
$gameTemp.reserveCommonEvent(188);
</JS On Erase State>

In the Common Event extra guardrails are needed as the Doom queue persist between battles, e.g.
  • What if the enemies are defeated while the party is afflicted with Doom?
  • What if the players Flee while afflicted?
  • What if players Flee while an enemy is afflicted?

Code:
// Check if the Doom queue exists
if (BattleManager._doomQueue && BattleManager._doomQueue.length > 0) {
 
  // Only proceed if the battle hasn't already ended (e.g., via Escape)
  if ($gameParty.inBattle()) {
    BattleManager._doomQueue.forEach(target => {

      // Safety: Check if the target is still valid and in the current battle
      if (target && (target.isActor() || target.isEnemy())) {
        target.addState(1); //Dead
        target.performCollapse();
      }
    });

    // UI Refresh
    if (SceneManager._scene instanceof Scene_Battle) {
      SceneManager._scene._statusWindow.refresh();
      SceneManager._scene._enemyWindow.refresh();
    }
    BattleManager.checkBattleEnd();
  }

// Clear the Doom queue at the end, even if the battle ended early,
// to ensure the Doom states don't carry over to the next battle.
BattleManager._doomQueue = [];
}
Try doing it with common event:

<JS On Erase State>
$gameVariables.setValue(X, target.index());
$gameTemp.reserveCommonEvent(Y);
</JS On Erase State>

COMMON EVENT:

Script:
const target = $gameParty.battleMembers()[$gameVariables.value(X)];
$gameTemp.requestAnimation([target], Z);
Wait: 180 frames
Script:
$gameParty.battleMembers()[$gameVariables.value(X)].addState(1);

EDIT:
Though didn't read the last part of you message properly. So you'll have to add extra code to check for enemy or actor and also you need to modify that to account for the state being on multiple battlers.
EDIT2:
Maybe make the animations start in the erase state notetag, push the target into an array, only reserve the common event if it's not already in the queue. Then in the common event wait x frames and add death state to all battlers in the array. LOL.
Try doing it with common event:

<JS On Erase State>
$gameVariables.setValue(X, target.index());
$gameTemp.reserveCommonEvent(Y);
</JS On Erase State>

COMMON EVENT:

Script:
const target = $gameParty.battleMembers()[$gameVariables.value(X)];
$gameTemp.requestAnimation([target], Z);
Wait: 180 frames
Script:
$gameParty.battleMembers()[$gameVariables.value(X)].addState(1);

EDIT:
Though didn't read the last part of you message properly. So you'll have to add extra code to check for enemy or actor and also you need to modify that to account for the state being on multiple battlers.
EDIT2:
Maybe make the animations start in the erase state notetag, push the target into an array, only reserve the common event if it's not already in the queue. Then in the common event wait x frames and add death state to all battlers in the array. LOL.
How do I pass an array variable to a common event? I thought they were strictly scalar.
Yeah, you can use stuff like this in the games:

$gameVariables.setValue(16, []);
$gameVariables.value(16).push($gameActors.actor(1), $gameActors.actor(2));
$gameVariables.value(16).forEach(actor => actor.addState(1));

EDIT: Oh yeah and one point I was thinking. I think you should use expire state notetag instead of erase state.

EDIT2: And just to clarify, I don't mean to use the above code as is. You don't want to turn a variable into an empty array every time. Do that like at game start.
Thanks. The Common Event workaround seems to do the job.
I don't use VisuStella but I think you should just make a skill that has the doom animation and give the user the death state. That way RPG Maker would wait for the animation and then kill the user IIRC. Let me know if it works.

JavaScript:
<JS On Erase State>
if (target.stateTurns(stateId) <= 0) {
  target.forceAction(doomSkillId, target.index())
}
</JS On Erase State>
I don't use VisuStella but I think you should just make a skill that has the doom animation and give the user the death state. That way RPG Maker would wait for the animation and then kill the user IIRC. Let me know if it works.

JavaScript:
<JS On Erase State>
if (target.stateTurns(stateId) <= 0) {
  target.forceAction(doomSkillId, target.index())
}
</JS On Erase State>
That didn't work, but fortunately I have a solution now. It's in an accidental double-post of the same subject line.

@jshunter I've merged your threads together, please don't make multiple threads for the same problem.


If you've found a solution, please report your thread to be closed.
Moderator note: @jshunter I've merged your threads together, please don't make multiple threads for the same problem.
If you've found a solution, please report your thread to be closed.
Ah. I thought Report was just for reporting abuse. I was looking for a delete button after I realized I pushed the Post button twice.

Original thread: https://forums.rpgmakerweb.com/threads/doom-state-and-animations.182545/