MV OnDeathEval weirdness

I am trying to make a state that kills an actor or battler, and then switches which team they are on in following battles (only works on some enemies/actors). This includes having an enemy equivalent to some of the actors in the game.
Notable plugins include YEP_BuffsStatesCore and WAY_CustomOnDeathEval.
What I'm trying to accomplish is a state using the StateCounter function which, when the state counter reaches 10, kills the affected actor or battler, running a common event with some dialogue specific to what actor or battler died and then either spawning an enemy event or adding them to the party respectively.
Here's the state note:
JavaScript:
<Custom Apply Effect>
target.addStateCounter(8, 1);
$gameVariables.setValue(1, target.actorId());
if ($gameVariables.value(1) >= 1) {
 if (target.getStateCounter(8) >= 10) {
  $gameVariables.setValue(3, 8);
  $gameTemp.reserveCommonEvent($gameVariables.value(1));
 }
}
else if (target.getStateCounter(8) >= 10) {
 $gameVariables.setValue(3, 8);
}
</Custom Apply Effect>
It checks if the target is an actor (and what actor it is) before running a common event for that actor, or if it is an enemy, simply tells the game that variable 3 (my 'cause of death' variable) was state 8.

The skill which applies the state has the damage formula:
JavaScript:
b.addState(8); if (b.getStateCounter(8) >= 10) {b.die()}; a.atk * 0.5
which applies the state, kills the target if it is at 10, and does some damage regardless.
The enemies which are susceptible to this state all have the notetag:
JavaScript:
<Custom On Death Eval>
 $gameVariables.setValue(2, [a number specific to each enemy to ID who died]);
 $gameTemp.reserveCommonEvent(8);
</Custom On Death Eval>
Which does effectively the same thing as the target.actorId() section in the state tag but for enemies.

My problem is that enemies killed by the state aren't triggering the common event. Actors killed by state level 10 will successfully trigger it and subsequently determine whether or not they should be switching teams, and enemies killed before reaching state level 10 are successfully triggering event 8, calling which enemy has died, and declaring that they are not state affected based on variable 3.
For whatever reason, killing an enemy via reaching state level 10 will not trigger any common events I try. They just die and nothing else happens.
I'm a novice at coding and can't figure out why this specific scenario doesn't work. The best guess I have is the order of operations is wrong somehow.
you should show your common events to make it easier to understand what's happening.

What I'm trying to accomplish is a state using the StateCounter function which, when the state counter reaches 10, kills the affected actor or battler, running a common event with some dialogue specific to what actor or battler died and then either spawning an enemy event or adding them to the party respectively.
the skill alone is what kills the battler when they hit 10 stacks? it sounds like the state would by the description

For whatever reason, killing an enemy via reaching state level 10 will not trigger any common events I try. They just die and nothing else happens.
it seems that the death eval hooks into the executeHpDamage function, which doesnt doesnt get called if you kill a battler through die(), as that kills by setting the target's hp to 0.

your actor common event triggers on state application and the skill applies the state, so your skill works in that case. but your enemies need to die to skill damage to trigger theirs they way you have it now. try changing your approach so that your state application fires off the common event the same way you do for actors
you should show your common events to make it easier to understand what's happening.
For testing purposes, 1-7 are currently just "[name of actor killed] has died".
Common event 8 starts with a text window "trigger 8" to see if it is triggering at all, then checks variable 2 via 7 different if statements to see if the enemy is one of the ones who can switch teams, then checks if variable 3 (StateVariable) = 8 and either ends here if it isn't or displays a line of custom dialogue for each character if it is.
Screenshot 2024-05-19 204243.png

(start of common event 8)
Enemies ID 2-7 can switch, so there are 5 more if statements below that look identical. ID 1 has an error message because there is no enemy with ID 1.
Screenshot 2024-05-19 204505.png

(end of common event 8)

the skill alone is what kills the battler when they hit 10 stacks? it sounds like the state would by the description
For whatever reason I couldn't get death to trigger when I was using state notetags so I shifted to using the damage formula and it seemed to work.

try changing your approach so that your state application fires off the common event the same way you do for actors

I removed the enemies' OnDeathEval and changed the state note to:
JavaScript:
<Custom Apply Effect>
target.addStateCounter(8, 1);
$gameVariables.setValue(1, target.actorId());
$gameVariables.setValue(2, target.enemyId());
if ($gameVariables.value(1) >= 1) {
 if (target.getStateCounter(8) >= 10) {
  $gameVariables.setValue(3, 8);
  $gameTemp.reserveCommonEvent($gameVariables.value(1));
 }
}
else if ($gameVariables.value(2) >= 1) {
 if (target.getStateCounter(8) >= 10) {
  $gameVariables.setValue(3, 8);
  $gameTemp.reserveCommonEvent(8);
 }
}
else {
 $gameVariables.setValue(1, 0);
 $gameVariables.setValue(2, 0);
}
</Custom Apply Effect>
Which should do the same thing for enemies as it does for actors.
I think I messed it up somewhere because this new state code causes neither actors nor enemies to trigger their respective common events.

Interestingly, this has also made it so actor's in-battle portraits (courtesy of YEP_BattleStatusWindow) no longer turn gray or lose TP on death. This isn't game-breaking, but is a little annoying.
It checks if the target is an actor (and what actor it is) before running a common event for that actor
You can do that more efficiently and without using a game variable. I would also set a property on the target instead of using the other game variable. It'll save your game from clutter to just use variables when they actually need to be universally accessible.

I also don't understand why you're using the CustomOnDeathEval functionality at all when you already have Buffs & States in your project. What I would do is put into your killing state's notes:

Code:
<Custom Apply Eval>
target.addStateCounter(8, 1);
if (target.getStateCounter(8) >= 10)
    target.killedByState = true;
</Custom Apply Eval>

Then state 1 has:
Code:
<Custom Apply Effect>
if (user.killedByState)
{
    if (user.isActor())
    {
        delete user.killedByState;
       $gameTemp.reserveCommonEvent(user.actorId());
    }
    else
    {
        // This is presuming that your variable 2 is actually serving some exterior purpose
        $gameVariables.setValue(2, user.enemyId());
        $gameTemp.reserveCommonEvent(8);
    }
}
</Custom Apply Effect>
Code:
<Custom Apply Eval>
target.addStateCounter(8, 1);
if (target.getStateCounter(8) >= 10)
    target.killedByState = true;
</Custom Apply Eval>
I was unaware that a property function existed. I will try setting this up when I next have a chance.
I was unaware that a property function existed.
It's not a function - a function is a predefined set of instructions that you can call with a shorthand - its name - followed by parentheses.

In JavaScript, anything that's an object can have properties, and those properties can be defined on the spot. You might want to read through Yanfly's Tips & Tricks articles, they make extensive use of this.

You also might want, if you're going to be trying to write your own JavaScript in various notetags, to do some tutorials on it. Variables, functions, and object properties are relatively basic stuff - I recommend the w3schools tutorials, starting with the Statements chapter.

Good luck with your project!
Okay, so I've got this property applied to an actor/enemy. How do I check if they have it in a common event? I tried making a script line with:
JavaScript:
if ($gameActors.actor(6).killedByState)
to trigger the effect in the common event if actor 6 has this property, which didn't work. I assume my problem is just referring to one of these two things wrong, but I can't find this specifc scenario in the threads/yanfly tutorials I'm checking, which all do their checks in the skill/state/actor, letting them use user. or target., which can't be referenced in common events.
Okay, so I've got this property applied to an actor/enemy. How do I check if they have it in a common event?
Can you show what your revised code is, and explain why you need this?

If you're using the code that I gave you, this should be unnecessary. It is running the common event assigned to that actor ID IFF they were killed by the state.

Code:
<Custom Apply Effect>
if (user.killedByState)
{
    if (user.isActor())
    {
        delete user.killedByState;
       $gameTemp.reserveCommonEvent(user.actorId());
    }

There's no logical need to check it again.

If there is a need - you use each actor's common event for other things - you should be able to see in the code (which I quoted again above) that the killedByState property has been deleted before calling the common event.

If you need to be able to reference it inside that common event, simply swap those two lines so it's not deleted until afterward. (or take it out of the notetag and put it in a Script command at the end of your common event)
or take it out of the notetag and put it in a Script command at the end of your common event
I did end up doing this, because I am looking to trigger a dialogue window in the common event that will either have the actor simply give a 'im dying' message or a 'im dying asnd subsequently changing teams' message, so I need to check if that property is applied.

I did end up doing this, because I am looking to trigger a dialogue window in the common event that will either have the actor simply give a 'im dying' message or a 'im dying asnd subsequently changing teams' message, so I need to check if that property is applied.
JavaScript:
<Custom Apply Effect>
if (user.killedByState)
{
    if (user.isActor())
    {
       $gameTemp.reserveCommonEvent(user.actorId());
    }
    else
    {
        $gameVariables.setValue(2, user.enemyId());
        $gameTemp.reserveCommonEvent(8);
    }
}
</Custom Apply Effect>
(forgot to show altered code)

This seems to be working; turns out the issue listed here
Okay, so I've got this property applied to an actor/enemy. How do I check if they have it in a common event? I tried making a script line with:
JavaScript:
if ($gameActors.actor(6).killedByState)
to trigger the effect in the common event if actor 6 has this property, which didn't work. I assume my problem is just referring to one of these two things wrong, but I can't find this specifc scenario in the threads/yanfly tutorials I'm checking, which all do their checks in the skill/state/actor, letting them use user. or target., which can't be referenced in common events.
was caused by me having an extra if in the common event
Screenshot 2024-05-20 105643.png

Fixed it now, will do one last test with all triggers (enemy and actor with and without state) to check if all is well.

Renamed the property to be shorter, currently everything looks like this:
State 8 note:
JavaScript:
<Custom Apply Effect>
target.addStateCounter(8, 1);
if (target.getStateCounter(8) >= 10)
    target.state8 = true;
</Custom Apply Effect>
State 1 note:
JavaScript:
<Custom Apply Effect>
if (user.isActor())
{
   $gameTemp.reserveCommonEvent(user.actorId());
}
else
{
    $gameVariables.setValue(2, user.enemyId());
    $gameTemp.reserveCommonEvent(8);
}
</Custom Apply Effect>
Common event 6 (actor death test):
1716220096856.png

Common event 8 (enemy death test):
1716220152775.png

(also clears variable 2—which is Current Enemy Target—at the end of the event)

Right now I am encountering 2 issues:
1. According to rpg_managers.js, '$gameEnemies.' is not a thing and I'm not sure how else to refer to enemy IDs when checking for the property. Would it be some variation of 'BattleManager._action._targetIndex' or would I have to store a variable and reference their position in the troop?
2. I don't know how to clear the property via common event as 'delete $gameActors.actorId(6).state8' as seen above is not working.
I've been trying a variety of methods to circumvent the first of the two problems at the end of my last post and so far haven't had much luck. I tried saving the enemy's troop index to a variable and referencing that, and then i tried using the [-2] value of $gameTroop.members(), but both just seemed to stop the common event from triggering altogether.
As for the second issue, I'm still not sure how to clear the property via common event. At this point it may be easier to clear the property in the state evaluation and call an entirely different common event if it's detected.
You seem to be mixing up a few different things between your code and your events.

An enemy's ID is just that - the ID of their entry in the Enemies tab of the database. It is not any kind of reference to a specific instance of an enemy in a battle. You could have a troop with 5 enemies that are all ID 2.

I know you've come to the realization, but there's no such thing as $gameEnemies. Did you actually see that somewhere, or were you guessing? (it never makes sense to guess with code)

1 - The only way to reference a specific enemy is by its placement in the troop. So you could store in the variable target.index() and then later reference $gameTroop.members()[$gameVariables.value(2)].state8 and that would check it on that specific enemy.

2 - Why do you care about deleting the property? No data on an enemy gets written into your save games, or persists at all once the battle has ended, so it really makes no difference for you to delete it. (in fact, it's slightly less efficient for you to do so because it's an extra command that ends up not mattering)

3 - Is there more to this common event that you haven't written out yet? It doesn't seem like there's much point to go through all of this stuff with the variable and conditional branches in the common event using script calls and setting the state8 property...instead of just adding one line of code to make a message box pop up inside the notetag.
Did you actually see that somewhere, or were you guessing? (it never makes sense to guess with code)
I checked the library of script calls and the managers file in the actual project once i realized enemies wasn't doing anything.

1 - The only way to reference a specific enemy is by its placement in the troop. So you could store in the variable target.index() and then later reference $gameTroop.members()[$gameVariables.value(2)].state8 and that would check it on that specific enemy.
That is what I was thinking. The version I tried wasn't working so I will try a couple more iterations.

Why do you care about deleting the property? No data on an enemy gets written into your save games, or persists at all once the battle has ended, so it really makes no difference for you to delete it.
I didn't really think about it, but I suppose the game wouldn't need to store that data for enemies. Problem solved for them, but the current line in the common event i'm using for actors (delete $gameActors.actorId(6).state8) is not working either and I don't understand why.

3 - Is there more to this common event that you haven't written out yet? It doesn't seem like there's much point to go through all of this stuff with the variable and conditional branches in the common event using script calls and setting the state8 property...instead of just adding one line of code to make a message box pop up inside the notetag.
The event is supposed to be a team switch check, that's the whole point of the state. Right now the common event is only playing text messages to demonstrate that all is working as it should, but the goal is to have someone affected by state8 leave the party when they die (or join it in the case of an enemy), while also playing some dialogue from that specific enemy/actor for flavour.
the current line in the common event i'm using for actors (delete $gameActors.actorId(6).state8) is not working either and I don't understand why.
Because that function doesn't exist. If you press F8 to go to the Console, you'd see an error from your code.

Only the Game_Actor data type has an actorId() method, and it doesn't take any arguments - it returns the ID of that actor.

$gameActors is an array, not a Game_Actor object.

the goal is to have someone affected by state8 leave the party when they die (or join it in the case of an enemy), while also playing some dialogue from that specific enemy/actor for flavour.
I really think it would be more efficient to simply put all of that into the notetag. You would have to say explicitly what you want to happen and look up the script calls for it (or ask for help with them), but it would in the end be way more efficient than these hoops you're jumping through to try to track everything with a common event.

If you decide to go that route and Google the script calls for what you want to happen, I advise you try to be more careful - there have been a number of places where you seem to have either made up or combined code from different things, and that isn't how code works; it has specific syntax that must be followed.

Good luck!
Alright, through a combination of all the above I've cobbled together a mix of a very long state tag and a few common events (Which I still prefer because they are a little easier for me to compartmentalize and understand) which is successfully triggering the effects I am looking for. Problem solved.
Thanks for the help!

Original thread: https://forums.rpgmakerweb.com/threads/ondeatheval-weirdness.168837/