MZ Slowdown from element rate code.

TL;DR: I'm experiencing some slowdown with some code in a Javascript notetag. Can someone look at my code and tell me if there's a more optimal way of writing it?


I'm working on a tutorial boss fight for my game where the boss has a skill called Shield Pollen, which applies an effect of the same name. This effect is intended to teach the player about reading state tooltips (using Visustella State Tooltips plugin) to find out the state negates the boss' natural weakness to fire magic, but can be removed with a physical attack. I wanted to use this effect later in my game, since it's a fairly solid effect, but I only wanted it to negate weaknesses, not set all element rates to 100% (using Visustella Elements and Status Core). There is a javascript notetag that lets me do that, and I wrote out code for it like this:
Code:
<JS Force Received Element 4 Rate: this.elementRate(4) > 1.0 ? 1.0 : this.elementRate(4)>
It works as intended, negating the weakness, but I'm noticing the game slowing down when the player is using a fire spell while the effect is active. My guess is the code I'm using isn't the most optimal way of getting the desired effect. Anyone good with javascript care to tell me if there's a better way of doing this?
I don't know whether you're still looking for an answer to this, but...

It might be because you've referenced elementRate(4) in the code for determining (a part of) the value of elementRate(4), i.e. an infinite loop. I imagine that causes an error and returns the default rate, 1. You could try it out on someone who has, like, 50% rate to see what happens? Maybe check the console (F8 or F12, Console tab) to see if any error messages (red text) are shown there.

Typically you'd base the check on "element rate without forced rate contributions". Unfortunately I don't see anything like that in the Elements & Status Menu Core documentation, and the code is obfuscated.

You could try replacing this.elementRate(4) in your notetag with:
JavaScript:
this.traitsPi(Game_BattlerBase.TRAIT_ELEMENT_RATE, 4)
This is just the core behaviour, based entirely on traits: it (probably) won't count any of the effects from VisuStella notetags.
I tried out the code you provided and it worked the same as what I already had. Unfortunately, it also caused more errors to pop up in the console than my original code did. They aren't red-text errors, fortunately (in either case), they're [RangeError: Maximum call stack size exceeded].

I did also check the effect with an element at 50% rate before the state is applied, and they're maintaining that rate with the state applied, so it doesn't seem like it's an infinite loop situation.

Thinking on it, I'm wondering if the element rate calculations for the notetag might be getting performed multiple times throughout the progression of the skill, rather than just once when the state is applied. That might cause some slowdown if it tries to recalculate the rate during each step of a skill's functioning.
If the error is "stack size exceeded", then it is almost certainly an infinite loop.

I imagine VisuStella use a try-catch block to capture the error, then use console.log (plain format) to output its message to the console, rather than console.error (red/error format). Hence the difference in appearance.

For the notetag:
  1. You should have replaced both instances of this.elementRate.

  2. I'm not sure how VisuStella parse their notetags (probably a custom routine), but normally the first > marks the end of the tag. So it could be worth rewriting the comparison using < (i.e. less-than) instead of >, just in case.

  3. It's possible that one or more of your plugins are modifying the element rate traits in a way that loops back to "what is the element rate" (maybe an init loop like this), but I think that's unlikely.
So, I'd suggest this for the tag:

<JS Force Received Element 4 Rate: 1 < this.traitsPi(Game_BattlerBase.TRAIT_ELEMENT_RATE, 4) ? 1 : this.traitsPi(Game_BattlerBase.TRAIT_ELEMENT_RATE, 4)>
Remember to save your project before testing, just in case.

If that doesn't seem to work either, are you certain there are no other contributions affecting element 4? Have you tried to reproduce a minimum test case in a new project with nothing but VisuStella Elements & Status Core?

The rate will be checked whenever the game asks for it to be checked. Usually that's only when damage is dealt, but the plugins you're using might reference the element rate at other times. Maybe there other relevant effects going on, e.g. an element 4 DoT, or something related to the state tooltips, or another custom display element present in your game's battles.
Okay, so good news and bad news:
Good news is that the slowdown I was experiencing before is gone, so this code is much better than what I had initially cobbled together.
Bad news is that when trying this code on a different element that the enemy is resistant to, it removes the resistance, setting the multiplier to 100% for the resisted element instead of leaving it at the resistance rate (50%).
It might be because you've referenced elementRate(4) in the code for determining (a part of) the value of elementRate(4), i.e. an infinite loop.
You are correct. The elementRate method triggers a parsing of the note tag, which will result in an infinite loop if said note tag also contains calls to elementRate.

'm not sure how VisuStella parse their notetags (probably a custom routine), but normally the first > marks the end of the tag. So it could be worth rewriting the comparison using < (i.e. less-than) instead of >, just in case.
In this case, it shouldn't be an issue. The RegEx they use should match the last > on the line as the end of the tag.

@JadeTheurgist
If you want to retrieve the received element rate, while accounting for any other element note tags (excluding "Force" ones) that might be in effect, and avoid the infinite loop, I believe this should do it (make sure to replace elementId with the actual numerical ID of the element):
JavaScript:
VisuMZ.ElementStatusCore.Settings.ElementRules.ReceivedRateJS.call(this, elementId)

So this note tag should work:
Code:
<JS Force Received Element 4 Rate: Math.min(VisuMZ.ElementStatusCore.Settings.ElementRules.ReceivedRateJS.call(this, 4), 1)>
Yep, works as I need without slowdown. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/slowdown-from-element-rate-code.164463/