I want to run a common event when regeneration activates

Hello,

As the title suggests, this is what I am trying to achieve.

I want this common event to execute every time the HP regeneration rate is applied to a single character.

Here is what I have put together so far in my attempt:

/*:
 * @target MZ
 * @plugindesc Common Event on Regeneration
 * @author go_comma
 *
 * @param CommonEvent1
 * @text Common Event when HP is recovered
 * @type common_event
 * @default 0
 *
 * @param CommonEvent2
 * @text Common Event when HP is decreased
 * @type common_event
 * @default 0
 *
 * @param Variable
 * @text Variable to store the battler that triggered regeneration. The index starts at 0; for actors, the actor ID is reflected directly, and for enemies, 1000 is added.
 * @type variable
 * @default 0
 * 
 * @help
 * Executes the specified common event when the "HP Regeneration Rate" is applied.
 * By storing the target in a variable, you can also determine which character should act within the common event.
 */

(() => {
  "use strict";
Game_Battler.prototype.regenerateHp = function() {
    const minRecover = -this.maxSlipDamage();
    const value = Math.max(Math.floor(this.mhp * this.hrg), minRecover);
    if (value !== 0) {
		this.gainHp(value);
		// Up to here is normal behavior

	const Vindex=Number(PluginManager.parameters("regenerationCommon").Variable)
	Vindex!=0 && $gameVariables.setValue(Vindex,this.index()+(this.isActor() ? 0 : 1000))//If a variable is specified, store the index in the variable. Check if this is an actor or enemy, and add 1000 if it is an enemy.
	
	const common1 = Number(PluginManager.parameters("regenerationCommon").CommonEvent1)
	const common2 = Number(PluginManager.parameters("regenerationCommon").CommonEvent2)
				

//↓ Since I am only reserving them, they are likely being overwritten by later reservations
		0<common1 && common1 < $dataCommonEvents.length && value>0 && $gameTemp.reserveCommonEvent(common1)//If a common event for recovery is specified, execute Common Event 1
		0<common2 && common2 < $dataCommonEvents.length && value<0 && $gameTemp.reserveCommonEvent(common2)//If a common event for damage is specified, execute Common Event 2
//↑
    }
};

})();

This does reserve the common event for each HP regeneration rate processing, but it is executed only after the regeneration processing is complete. As a result, it seems that only one common event fires, reflecting the information of the last character.

How can I modify this so that the common event fires once for each character every time the HP regeneration rate is applied?

Also, regardless of plugin knowledge, if anyone knows a method to insert a common event every time regeneration triggers for a single character, I would appreciate any information you can provide!

Welcome @go_comma!

In MZ, reserveCommonEvent actually adds to a queue and they run oldest first, so I don’t think the reservations are overwriting each other. I’m wondering if they all do run, but since none of them start until the regeneration processing is done, every one of them reads the variable after the last character already wrote to it.

What if you queued the target along with the common event, instead of writing the variable right away? Inside your if (value !== 0) block, after gainHp, something like this:

const params = PluginManager.parameters("regenerationCommon");
const common = Number(value > 0 ? params.CommonEvent1 : params.CommonEvent2);
if (common > 0 && common < $dataCommonEvents.length) {
    $gameTemp._regenTargets = $gameTemp._regenTargets || [];
    $gameTemp._regenTargets.push(this.index() + (this.isActor() ? 0 : 1000));
    $gameTemp.reserveCommonEvent(common);
}

Then make the first command of both common events a Script that pulls the next target into your variable (swap 10 for your variable ID):

$gameVariables.setValue(10, $gameTemp._regenTargets.shift());

Both lists are first in, first out, so each common event should pick up the character that reserved it.

@Baz
Thank you for your response.
So the common event is being processed twice by variables entered later.
And if you define the variables within the common event, you can indeed switch them with each execution!

I rewrote the code based on the information you provided, and it worked right away!
Thank you so much for your help!!!