MV Yanfly Battle AI Lag

Hey all, I posted about this in a couple discords but no luck yet, posting here just in case someone new sees it. I'm wondering if anyone has experience with a slight, but very noticeable lag issue when using Yanfly's Battle AI Core in MV? I'm also using their ATB plugin so idk if it happens with other battle systems, but whenever the enemy runs their condition codes, the game seems to stop completely for a moment. When I disable the plugin it seems to run much more smoothly. I am, admittedly, using the Eval condition a bit more extensively than normal (I have evals that change which skills are usable based on enemy level and variables, and some that check a Math.random function against a number weighted by various conditions instead of the Random% condition in the plugin by default, etc.) Even with that being the case, I would not expect the amount of time the enemy takes to decide its next skill, and it seems deleting all eval codes doesn't speed it up by very much. MAYBE it's my laptop, but it's a 4070 only a couple years old, it should be fine. Video showing what I mean is below.

If anyone else has seen this happen and solved it, is there another way to optimize the notetags and make the game run a little faster at these times? Or to start, (though i imagine this is not possible) at least keep the battler animations running, and maybe load a little elipses bubble above the enemy making the decision so it disguises it as the enemy taking the time to think. Other than that, a different battle AI plugin that uses evals like this would be appreciated, I already tried looking into a couple others but the ones I've seen don't really have an eval condition that I can use JS for.

is there another way to optimize the notetags
you would have to show the notetags youve set up to for anyone else to be able to help with that

When I disable the plugin it seems to run much more smoothly. I am, admittedly, using the Eval condition a bit more extensively than normal... and it seems deleting all eval codes doesn't speed it up by very much.
just to be sure, is this true of all enemies, or just those? do enemies without the AI notetag still cause lag when they evaluate their choices?

also it might be worth mentioning, but notebox changes wont actually be reflected until you save the project. So if you had the enemy fully set up, deleted the evals, and then ran a test without having saved in between, the enemies ingame would still be operating off of the outdated notebox data
you would have to show the notetags youve set up to for anyone else to be able to help with that
Code:
 <AI Level: 100>
<AI Priority>
Eval user._movesSet===false +++ Eval user._m1=12,user._m2=261,user._m3=0,user._m4=0,2<=user.level&&(user._m3=40),3<=user.level&&(user._m4=394),4<=user.level&&8===user._battleType&&(user._m3=73),5<=user.level&&8!==user._battleType&&3!==user._battleType&&(user._m3=390),6<=user.level&&3===user._battleType&&(user._m2=215),7<=user.level&&(7===user._battleType||8===user._battleType)&&(user._m2=6),8<=user.level&&3!==user._battleType&&261!==user._m2&&(user._m3=78,user._m4=261),11<=user.level&&3===user._battleType&&(user._m4=317),console.log(user._m1+" "),console.log(user._m2+" "),console.log(user._m3+" "),console.log(user._m4+" "),(user._movesSet=true),user._movesSet===false: Vampirism, Highest HP

User MP% param < 33% +++ User HP% param > 50%: Blood Battery, User
Eval (user._m4===317) +++ State !== state 90 +++ Eval AIManager.targetCondition("target._battleType===3") +++ Random 20%: Disinfect, Highest MDF
Eval (user._m2===215) +++ Eval AIManager.targetCondition("(Math.random()*100) < ((((target.hp/target.mhp) < 0.75)*25)+(((target.hp/target.mhp) < 0.50)*25)+(target.getStateCategoryAffectedCount('Poison')*10))"): Antitoxin, Lowest HP
Eval (user._m3===40) +++ STATE !== state 20 +++ Random 75%: Heartbeat, User
Eval (user._m3===73) +++ State !== state 22 +++ Random 20%: Menace, Random
Eval (user._m2===6) +++ State !== state 156 +++ Random 25%: Soften, Random
Eval (user._m3===78) +++ State !== state 10 +++ Random 30%: Sunset Kiss, Lowest DEF
Eval (user._m3===390) +++ STATE === state 135 +++ STATE !== state 134 +++ Random 33%: Heal Jam
Eval (user._m4===394) +++ State !== state 138 +++ Eval AIManager.targetCondition("(Math.random()*100) < (40+(target.isStateAffected(10)*20))"): Saw Tooth, Highest HP
Eval (user._m1===261||user._m4===261) +++ State === state 138: Vampirism, Lowest DEF
Eval (user._m1===261||user._m4===261) +++ State === state 137: Vampirism, Lowest DEF
Eval (user._m1===261||user._m4===261) +++ State === state 136: Vampirism, Lowest DEF
Eval (user._m1===261||user._m4===261) +++ State === state 135: Vampirism, Lowest DEF
Eval (user._m1===261||user._m4===261): Vampirism, Lowest DEF
 </AI Priority>

While the evals I'm using are complex, (The first eval works by referencing and editing a special variable in an enemy's passive state. The code works even if it's ugly. It exists here for now just because I really do not want to run that code in a passive state that has to check for every single enemy in the game, or make a separate passive state for each enemy in the game, which would be required to limit the skills they are able to use properly. Additinally, when that first eval is removed entirely and the checks for the ._m variables are removed, performance isn't increased so I don't think its that. And the AIManager.targetCondition function is part of another plugin to help make certain evals work, but taking that plugin out and reverting to normal evals also does not speed things up.) as before, even removing every eval condition and switching it to the Random x% condition causes the same lag, and I have accounted for the need to save the game after editing plugins and notetags.

So far, every enemy that has the AI Priority notetags has seen noticeable delay, sometimes more than shown in the video, but that is often when my laptop is unplugged and doesn't run to its highest degree anyway. Now that I think of it I haven't determined exactly how many conditions begins the lag, but the lowest number of skills I've used in any enemy is 4. Because that enemy only has 4 skills, it does not have the initial skill establishing eval shown in the example enemy above, and even that simple one shows a similar skip in performance. That enemy's code is below:

Code:
<AI Level: 100>
 <AI Priority>
State !=== state 24 +++ Eval (AIManager.targetCondition("(Math.random()*100)<(25*(target.hp/target.mhp))")): Coo, Highest ATK
State !=== state 27 +++ Eval (AIManager.targetCondition("(Math.random()*100)<(30*(target.hp/target.mhp))")): Iron Guard, Highest DEF
Eval (!user.isStateCategoryAffected('Shield')): Shape Memory, User
Always: Metal Bash
 </AI Priority>

Edit: Actually, scratch the last part about the simple enemy's lag being noticeable. I CAN notice it but I think it would be acceptable to anyone not meticulously testing their own game, lol. So the number of conditions is really the main cause of lag for sure, but if there's a way to optimize the performance of this many evals I'd really appreciate it. I feel like there MUST be a way to have the game run 15 evals in a row, even if its not typical.
I feel like there MUST be a way to have the game run 15 evals in a row, even if its not typical.
Additinally, when that first eval is removed entirely and the checks for the ._m variables are removed, performance isn't increased so I don't think its that.
itd be minor, but you'd want to remove console.log()s in the first one. since you said it works the way you need it to, you can remove them.

I do use yanfly's ai core and i did use DreamX's extension for a bit (replaced it with Turan's tweak cause its more convenient to use), I am not familiar with Yanfly's ATB system at all. I trust that everything is only being called once for every enemy when its time for them to act, right?

So the number of conditions is really the main cause of lag for sure
Ill need screenshots of your plugin list to see what youre working with and the order to be able to replicate your setup. I have enemies with more conditions than that without performance hits, but not on ATB. i dont recall having to adjust the code behind the AI outside of plugins to enable the target variable in evals.

Also, I take it youre using MV's default nwjs 0.29.4? i do recommend upgrading if you are. the performance difference between 0.29.4 and 0.49.2 is pretty significant imo. A little less so from 0.49.2 to newer ones

Eval (user._m1===261||user._m4===261) +++ State === state 138: Vampirism, Lowest DEF Eval (user._m1===261||user._m4===261) +++ State === state 137: Vampirism, Lowest DEF Eval (user._m1===261||user._m4===261) +++ State === state 136: Vampirism, Lowest DEF Eval (user._m1===261||user._m4===261) +++ State === state 135: Vampirism, Lowest DEF
unless you want the strict priority for each state individually, that all can be one line
Code:
Eval (user._m1===261||user._m4===261) && AIManager.targetCondition("[138,137,136,135].some(s => target.isStateAffected(s))"): Vampirism, Lowest DEF
I do use yanfly's ai core and i did use DreamX's extension for a bit (replaced it with Turan's tweak cause its more convenient to use), I am not familiar with Yanfly's ATB system at all. I trust that everything is only being called once for every enemy when its time for them to act, right?
Every time the enemy hits the beginning of the red line, and every time the actor in the first position hits that red line. Which is admittedly odd, but if the lag is reduced overall i'm okay with that. I haven't used Turan's tweak yet, but I'll look into it. Unfortunately, I'm starting to be convinced that it is a compatibility issue with the ATB battle system and Battle AI, since I'm getting more accounts of people not getting any lag when using the normal or other battle systems. It's a shame, I find it less predictable for the player than normal battle systems. Maybe I'll look into other ATB plugins or just make the AI dumber.
Ill need screenshots of your plugin list to see what youre working with and the order to be able to replicate your setup. I have enemies with more conditions than that without performance hits, but not on ATB. i dont recall having to adjust the code behind the AI outside of plugins to enable the target variable in evals.
I'll put a couple screenshots copy of my list here. I don't expect you to recreate the plugin list in your system or anything, but if you find anything obviously wrong with my setup let me know.
Spoiler: Plugin List
list1.png

list2.png

list3.png

list4.png

list5.png

list6.png

List7.png
unless you want the strict priority for each state individually, that all can be one line
Code:
Eval (user._m1===261||user._m4===261) && AIManager.targetCondition("[138,137,136,135].some(s => target.isStateAffected(s))"): Vampirism, Lowest DEF
I could probably clean that up, true. The reason it's set up like that is states 135-138 are different levels of a Bleed damage over time state, and Vampirism is a skill that heals the user more per level of Bleed already on the target. So, this way the enemy will target the one with the highest bleed stack first to get the most HP out of it. However, minimizing the number of conditions is probably more important anyway.

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-battle-ai-lag.180642/