b.addState() in Damage Formula crashes when used by Enemies

This is causing a crash when used by enemies in the Damage Formula:

Code:
b.addState(5);

The specific State doesn't matter, and whether or not there is anything else in the formula doesn't matter. It works when actors do it, but it gives me this error when enemies try it:



It isn't causing this problem if I apply it through Skill Core's <Before Eval> notetags, which makes me think it has something to do with the way it's working in the damage formula. But the fact that it works fine for actors really has me scratching my head. It doesn't have this problem without plugins.

Anyone know what the problem is?
It appears it's trying to draw a gauge on an actor, not on the enemy.

Check what version of core files you have (in your project's js folder, in the rpg_core.js file), and that you have the appropriate versions of the plugins for that version of the core files (note, this may not be the same as your MV version if you have taken an update since creating the project and have not updated your project manually using the steps provided in the update thread).

The other thing I'd check is that your Yanfly plugins are all in the correct order. This should be the order in which they're listed on Yanfly's site.
Does the crash happen on a project with no plugins?

If the crash doesn't happen on a clean project, then you should show us a screenshot of your plugin list as well.
I have 1.6.1, and all the Yanfly plugins were freshly downloaded from his site less than two months ago. No changes afterwards. The order is as listed on the site, including following the directions in Message Backlog to place it below the Extended Message Packs if I have them.

After a lot more effort (I disabled *every* plugin in my list, and turned them on one by one), I finally found the source of the problem.

YEP_DamageCore

Without that one, there is no issue. So I suppose that means it's the damage formula itself. I need that plugin because I need the Critical Control plugin that depends on it. I haven't changed any of its settings, and I'm not using any note tags from it--so it's something about its default configuration.

Has anyone else had this sort of problem, or know what kind of default settings in it might be causing it?
lol - I thought that was a link. Would have been nice if you'd provided a link.

However, DamageCore is not one of the plugins listed in your error message, so I'm surprised it'd be that one.
Link added.

Yeah, I don't even know how to debug this stuff and it surprised me. I started by checking the plugins that were listed, but it just kept mostly the same errors and changed what plugins it referenced for them. Sometimes turning off a plugin made the error reference rpg_core.js for the same basic element it was previously referring to the plugin for, which makes me think it was only referring to those plugins because they were taking over certain functions from the base program--functions which Damage Core was interacting with in a way that causes an error when you try to add a state to an enemy through the damage formula.
I ran a battle against the default Bats in a blank project with no other plugins besides YEP_DamageCore.

The formula for the Attack skill was:
Code:
b.addState(5); a.atk * 4 - b.def * 2

I had no crash when the enemy Bats attacked, the damage formula also worked and inflicted the Blind state on Harold, Therese, Marsha, and Lucius.

If you make a blank project, I bet you'll have the same result (no crash). So there's some other part of your project where it's crashing that is the issue, which you'll have to do more troubleshooting to identify.

upload_2019-3-10_0-13-29.png
I have 1.6.1

Can you confirm that your project is also 1.6.1? Just because your MV is, doesn't mean your project is. Did you get that version from the MV editor window, or did you go to your project's files and check the rpg_core.js file? This is also not the NewData files where MV is installed - that rpg-core.js will be the same version as your MV.
Can you confirm that your project is also 1.6.1? Just because your MV is, doesn't mean your project is. Did you get that version from the MV editor window, or did you go to your project's files and check the rpg_core.js file? This is also not the NewData files where MV is installed - that rpg-core.js will be the same version as your MV.

Yep, I double-checked to make sure, and it is the file in the folder for this particular project that I'm looking at.

I was also able to confirm on a project without plugins that Damage Core doesn't cause the issue by itself.

When I identified Damage Core as the culprit, I had turned off all of the plugins and was turning them on one by one. When I got to Damage Core the issue appeared, when I turned if off the issue went away, even after I reactivated the rest of the plugins. The plugins I had turned on without issue, before I turned on Damage Core below them in the list and had the issue, are:

Community_Basic (Came pre-installed)
YEP_CoreEngine
YEP_X_CoreUpdatesOpt
YEP_BaseParamControl
YEP_X_ClassBaseParam
YEP_ExtraParamFormula
YEP_MainMenuManager
YEP_MessageCore
YEP_X_ExtMesPack1
YEP_X_ExtMesPack2
YEP_X_MessageBacklog
YEP_X_MessageSpeedOpt
YEP_X_SaveCore
YEP_SelfSwVar
YEP_SpecialParamFormula
YEP_BattleEngineCore
YEP_X_ActSeqPack1
YEP_X_ActSeqPack2
YEP_X_AnimatedSVEnemies
YEP_X_BattleSysSTB
YEP_X_CounterControl
YEP_X_InBattleStatus
YEP_X_TurnOrderDisplay
YEP_X_VisualHpGauge
YEP_X_AbsorptionBarrier
YEP_BattleAICore
YEP_BattleSelectCursor
YEP_BattleStatusWindow
YEP_BuffsStatesCore
YEP_X_StateCategories
YEP_X_VisualStateFX

So that's what we're looking at without a problem. Something in there isn't playing well with Damage Core apparently.
That sounds like it.

So get back to the stage where you'd turned all the other plugins on, along with damage core, and the others still turned off (I'm guessing damage core and everything above it in the list need to be on).

And then, from the plugin above damage core, start turning them all OFF, one at a time (with damage core still on), and see what plugin you just turned off when the issue stops happening.

It is possibly a conflict between those two plugins, so then you can put just those two in a project together, turn them both on, see if the issue happens. Then turn one on and the other off and confirm that the issue doesn't happen. Then change them again so one is off and the other is on, and confirm that the issue doesn't happen.

Once you get to that stage, you know it's a compatibility issue between the two.

If it definitely only happens when you put the formula into a skill that's used by enemies, I'd look at the Battle AI Core as a potential culprit. However, the error message indicates it's trying to render a gradient, so I'm also looking at the Visual HP Gauge.


Can you also confirm the following ... in the game where this issue is happening, did you choose New Game from the menu, or are you playing an older save file that was created prior to adding some plugins? And you ARE doing a proper test play, and not just battle test? Doing those things (adding a plugin then resuming a previously saved game, or using battle test) often crash if you have a battle plugin.
Yes, I created it as a blank New Game, and then manually installed all the plugins.

One thing I did discover, is that if I use the <Damage Formula> </Damage Formula> note tags and place the addState in there, it doesn't crash.

However
that's not an effective workaround for me, because I'm also using HIME_WeaponDamage to get damage values from weapons, and the formula I need to input into the damage formula from that plugin won't work in the notetag from Damage Core.
then try all the things I said above
I've figured it out!

I did a bunch of testing, compared different projects, and I finally found the error situation, which should be reproducible by anyone easily:

Create a New Project. Install no plugins except YEP_DamageCore. Put this alone as the damage formula of any skill:

Code:
b.addState(5);

Give that skill to an enemy. And watch the crash.

It doesn't crash if you also give it damage in the formula, like this:

Code:
b.addState(5); a.atk * 4 - b.def * 2

So if you use YEP_DamageCore, you must specify a damage value (0 works) in the formula of a skill, or it may crash when used by an Enemy. The plugin data has some Damage Step lines that refer to <0 and >0, but I'm not sure if just adjusting those settings would do anything (plus I personally don't know enough to touch them).

If anyone would like to verify this and let me know the error is reproducible, that would be great.

Thanks for all of the assistance folks. That's a big relief. Not being able to use the shorter ternary operator forms of if/else statements in my damage formula is a very small price to pay for having them actually work.
I reproduced the error following your directions, it's a bug in YEP_DamageCore.

When the damage formula has no "return value", the YEP_DamageCore is setting the actor's HP to "NaN" (which means "Not A Number"). Then the status window tries to update the actor's HP gauge. The gauge draws as a percentage of the actor's HP, but if the HP is NaN, then that percentage is also NaN (you see where I'm going with this...). Therefore, it tries to fill the gradient of the HP gauge with a width of NaN, which is a non-finite number, thus the error message.

You don't see YEP_DamageCore in the stacktrace of the error message because it's not in the stack when the error happens, but it caused the side-effect which leads to the error.

The base MV code has a check against NaN values, but Yanfly left it out for some reason. i.e.
Code:
if (isNaN(value)) value = 0;

To fix the issue, add this as a new plugin directly below YEP_DamageCore.
Code:
Game_Action.prototype.evalDamageFormula = function(target) {
  try {
    var item = this.item();
    var a = this.subject();
    var b = target;
    var user = this.subject();
    var subject = this.subject();
    var s = $gameSwitches._data;
    var v = $gameVariables._data;
    var sign = ([3, 4].contains(item.damage.type) ? -1 : 1);
    var value = 0;
    if (item.damage.custom) {
      eval(item.damage.formula);
      value = Math.max(value, 0) * sign;
    } else {
      value = Math.max(eval(item.damage.formula), 0) * sign;
    }
    if (isNaN(value)) value = 0;  // <--- New check for NaN values
    return value;
  } catch (e) {
    if (item.damage.custom) {
      Yanfly.Util.displayError(e, item.damage.custom, 'DAMAGE FORMULA ERROR');
    } else {
      Yanfly.Util.displayError(e, item.damage.formula, 'DAMAGE FORMULA ERROR');
    }
    return 0;
  }
};

I don't subscribe to them on Patreon, but if you or someone else does, you may want to let them know so they can fix it.
A damage formula is kinda for determining how much damage to inflict. So you SHOULD put something after it for the damage value, and not just use it to add states.
To fix the issue, add this as a new plugin directly below YEP_DamageCore.

Thanks! That works!

I haven't subscribed on Patreon yet (I'm tempted to for a couple of months or so), so I can't send a message either.

A damage formula is kinda for determining how much damage to inflict. So you SHOULD put something after it for the damage value, and not just use it to add states.

Sometimes there will be a damage value, but sometimes I just need to be able to use formulas for determining whether states gets applied in ways that aren't supported by the default Add State Effect which just lets me give a percentage. I was going to redefine the LUK Parameter in a plugin so that it could handle that for me, but during the course of doing that I found out that the LUK factor only comes into play if the skill isn't set to Certain Hit, and I need some of those skills to be Certain Hit. So...the damage formula box seems like the easiest way to add states based on my own probabilities.
If there's no damage, just put

; 0

at the end, so it at least returns a number.

Original thread: https://forums.rpgmakerweb.com/threads/b-addstate-in-damage-formula-crashes-when-used-by-enemies.106626/