MV YEP_BattleAiCore creates lag/fps drop to 8



If the AI Priority is too long, it creates about 1 second of a lag before the action is started.

I have an I7 and a gtx1080ti. So the problem is not the setup.
Drivers are up to date.
All Plugins are up to date.
It is not the plugin order.
I don't want to make the AI less complex.

Besides that, Any help is appreciated! 🙂

My guess: The issue is that Javascript is a single task language, which means what you see on screen is rendered AFTER the <AI Priority> Code is finished?
Maybe the <Ai Priority> can be preloaded, so that the game doesn't lag?
Maybe the screen can freeze, so that at least no frames are skipped while calculating ai? (maybe only the moves and actions can be freezed but the images keep updating)?

Complex AI:
Code:
<AI Priority>
//11

//10
EVAL user === $gameParty.members()[0] && $gameVariables.value(2) > 1 || user === $gameParty.members()[1] && $gameVariables.value(2) > 1 || user === $gameParty.members()[2] && $gameVariables.value(2) > 1 || user === $gameParty.members()[3] && $gameVariables.value(2) > 1 || user === $gameTroop.members()[0] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[1] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[2] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[3] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[4] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[5] && $gameVariables.value(5) > 1: skill 460
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ state === state 5 +++ state !== state 159 +++ state !== state 185 +++ state !== state 186 +++ state !== state 674 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL user === $gameParty.members()[0] && $gameVariables.value(2) > 1 || user === $gameParty.members()[1] && $gameVariables.value(2) > 1 || user === $gameParty.members()[2] && $gameVariables.value(2) > 1 || user === $gameParty.members()[3] && $gameVariables.value(2) > 1 || user === $gameTroop.members()[0] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[1] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[2] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[3] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[4] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[5] && $gameVariables.value(5) > 1 +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 450, lowest mdf
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ state === state 5 +++ state !== state 159 +++ state !== state 185 +++ state !== state 186 +++ state !== state 674 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL user === $gameParty.members()[0] && $gameVariables.value(2) > 1 || user === $gameParty.members()[1] && $gameVariables.value(2) > 1 || user === $gameParty.members()[2] && $gameVariables.value(2) > 1 || user === $gameParty.members()[3] && $gameVariables.value(2) > 1 || user === $gameTroop.members()[0] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[1] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[2] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[3] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[4] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[5] && $gameVariables.value(5) > 1 +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 450, lowest mdf
EVAL AIManager.targetCondition("target.states().length > 8") +++ state !== state 726 +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 485, lowest mdf
EVAL AIManager.targetCondition("target.states().length > 8") +++ state !== state 726 +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 485, lowest mdf

//9
EVAL AIManager.targetCondition("target.states().length > 6") +++ state !== state 726 +++ state !== state 711 +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 485, lowest mdf
EVAL AIManager.targetCondition("target.states().length > 6") +++ state !== state 726 +++ state !== state 711 +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 485, lowest mdf

//8
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ EVAL AIManager.targetCondition("target.isStateAffected(179) || target.isStateAffected(20)") +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 464, lowest mdf
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ EVAL AIManager.targetCondition("target.isStateAffected(179) || target.isStateAffected(20)") +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 464, lowest mdf
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ state === state 5 +++ state !== state 159 +++ state !== state 185 +++ state !== state 186 +++ state !== state 674 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 450, lowest mdf
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ state === state 5 +++ state !== state 159 +++ state !== state 185 +++ state !== state 186 +++ state !== state 674 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 450, lowest mdf
EVAL AIManager.targetCondition("target.states().length > 4") +++ state !== state 726 +++ state !== state 711 +++ state !== state 699 +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 485, lowest mdf
EVAL AIManager.targetCondition("target.states().length > 4") +++ state !== state 726 +++ state !== state 711 +++ state !== state 699 +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 485, lowest mdf

//7

//6
state !== state 160 +++ state !== state 725 +++ EVAL AIManager.targetCondition("target.isStateAffected(260) || target.isStateAffected(262) || target.isStateAffected(266)") +++ turn < 2 +++ state !== state 159 +++ state !== state 674 +++ state !== state 185 +++ state !== state 186 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 450, lowest mdf
state !== state 160 +++ state !== state 725 +++ EVAL AIManager.targetCondition("target.isStateAffected(260) || target.isStateAffected(262) || target.isStateAffected(266)") +++ turn < 2 +++ state !== state 159 +++ state !== state 674 +++ state !== state 185 +++ state !== state 186 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 450, lowest mdf

//5

//4

//3
EVAL AIManager.ifanyoneCondition("target.isStateAffected(44) || target.isStateAffected(118)") +++ turn < 2: skill 463

//2
turn < 2: skill 460
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ turn < 2 +++ state !== state 159 +++ state !== state 674 +++ state !== state 185 +++ state !== state 186 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL AIManager.groupCondition("!target.isStateAffected(44) && !target.isStateAffected(118)"): skill 450, lowest mdf
state !== state 160 +++ state !== state 710 +++ state !== state 725 +++ turn < 2 +++ state !== state 159 +++ state !== state 674 +++ state !== state 185 +++ state !== state 186 +++ EVAL AIManager.targetCondition("target.mat > target.atk") +++ EVAL AIManager.targetCondition("target.isStateAffected(44) || target.isStateAffected(118)"): skill 450, lowest mdf
EVAL AIManager.targetCondition("target.friendsUnit().aliveMembers().length > 3"): skill 457

//1

//0
state !== state 160 +++ state !== state 725 +++ EVAL AIManager.targetCondition("target.isStateAffected(260) || target.isStateAffected(262) || target.isStateAffected(266)"): skill 19, lowest mdf
state !== state 160 +++ state !== state 710 +++ state !== state 725: skill 19, lowest mdf
always: skill 19, lowest mdf

</AI Priority>

Simple AI:

Code:
<AI Priority>
always: skill 460
always: skill 457
always: skill 19
</AI Priority>
I don't want to make the AI less complex.
But seems to be the easiest way to fix this issue. Apart from any lag it may or may not create it's just a matter of plain code readability/maintainability. Im not carefully reading all your conditions, but there has to be simpler way to achieve the same or close to the same result. Could you put your AI logic in words? Maybe then we can help you simplifying the code without simplifying the logic.

Maybe the <Ai Priority> can be preloaded, so that the game doesn't lag?
I don't think that can be reasonably done. For example your AI checks the existence of various states. How would you know these states would still exist in the same configuration when it is actually time to execute the skill?

Also as a general tip, look up how to manually update your project's nw.js. That apparently can also vastly improve performance. Though I don't know if it will help in your specific case.
AI programming is one of the most complex tasks available, and so far no one succeeded without taking shortcuts.

So you have to take some shortcuts to reduce the lag you have problems with.
Your only choice is in to which shortcuts to take to pretend to the player that the AI is more complex that its remains really are.

I can't check what your conditions really mean, but I strongly suggest you find a way to simplyfy them.
perhaps moving some of them outside into a troop event to check for the state of the battle at the end of each turn and assigning a lower number of hidden invisible states that are then checked by the AI?
Could you put your AI logic in words? Maybe then we can help you simplifying the code without simplifying the logic.
Considering taunts is a huge thing. I just noticed that it can be done by a simple notetag (<AI Consider Taunt>). Maybe that reduces complexity by about 35% if the notetag works correctly.
Maybe instead of coding like this: state !== state x +++ state !== state y
coding like this reduces lag: EVAL AIManager.targetCondition("!target.isStateAffected(x) && !target.isStateAffected(y)")
coding several evals as one big eval might also reduce lag.

But since the lag is about a full second, it is probably impossible to remove lags entirely by reducing code.
Also as ageneral tip, look up how to manually update your project's nw.js. That apparently can also vastly improve performance. Though I don't know if it will help in your specific case.
How do I update nw.js?? I have seen this in another lag related post too, but I just don't know how to do that! 🙂

Additional thoughts: Maybe I can achieve freezing instead of lagging by removing the short forward movement that is done before using a skill.
Maybe I can achieve no lags by coding a setTimeout at the right place, but whenever I tried using setTimeout everything froze. However Yanfly managed to make a wait-function (wait: 30) in his action sequence plugin that doesn't freeze everything.
How do I update nw.js?? I have seen this in another lag related post too, but I just don't know how to do that! 🙂
Simply googling has the following thread as its topmost result:
I recommend checking that out.
Considering taunts is a huge thing. I just noticed that it can be done by a simple notetag (<AI Consider Taunt>). Maybe that reduces complexity by about 35% if the notetag works correctly.
As far as I know neither the notetag nor complex custom logic are required for the AI to respect taunts. At least I never had to do anything special to make my AI respect taunts. I believe this is because when the notetag is set, the AI plugin will specifically only consider the taunt targets and all alive members otherwise - but the Taunt plugin has overwritten the alive members function to return only the taunt targets.

EVAL AIManager.targetCondition("!target.isStateAffected(x) && !target.isStateAffected(y)")
What eval is this even? Opening Yanfly's AI Core and searching for targetCondition yields no results. Have you defined this function yourself?
In general though you would need to try. Taking several conditions that you would normally chain with +++ makes for fewer function calls (good for performance) but needs to execute the eval function which is an inherently badly performing function. So I'd say there is no way to answer this question except benchmarking.
Let's just take a look at one of your conditions:
Code:
EVAL user === $gameParty.members()[0] && $gameVariables.value(2) > 1 || user === $gameParty.members()[1] && $gameVariables.value(2) > 1 || user === $gameParty.members()[2] && $gameVariables.value(2) > 1 || user === $gameParty.members()[3] && $gameVariables.value(2) > 1 || user === $gameTroop.members()[0] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[1] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[2] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[3] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[4] && $gameVariables.value(5) > 1 || user === $gameTroop.members()[5] && $gameVariables.value(5) > 1: skill 460
Is this even correct? you're going back and forth between the gameParty and the gameTroop. Unless you're using this with the Actor Auto Battle AI extension and having actors on auto battle, it's impossible for the user to be in the gameParty.

If that is correct, I think it would be simpler to make two versions of the skill with actor conditions and enemy conditions.

if you really don't want to do that, it's still hugely overly complicated:
Code:
EVAL if (user.isActor()) $gameVariables.value(2)>1; else $gameVariables.value(5)>1: skill 460

I would've made it even a bit smaller with the ternary operator, but I suspect the colon in it would make the line get parsed incorrectly by the plugin.

That's just an example - as eomere said above, your code is unnecessarily convoluted (which is making your computer do unnecessary processing).
What eval is this even? Opening Yanfly's AI Core and searching for targetCondition yields no results. Have you defined this function yourself?
This has worked without issues for me until now. Now it only works for actors but not for enemies and I just don't understand why. I hope sorting my plugins fixes it. I even made another extension from that extension that worked as well. Now nothing works for enemies. Holy cow, the problem inception is real. I must have installed a plugin that overrides something.
Code:
EVAL if (user.isActor()) $gameVariables.value(2)>1; else $gameVariables.value(5)>1: skill 460
I'll check if that works after I solved the targetCondition issue.
I think if i manage to not use the <AI Priority> notetag at all, but rather get the battlers skills array and then check it against a skillcondition-list that inherits all skillconditions in the game it might be faster and also save me a lot of notetagging work. That will probably take several days but i think it is worth a shot.
But first i will
1. fix EVAL AIManager.targetCondition
2. check if this works: EVAL if (user.isActor()) $gameVariables.value(2)>1; else $gameVariables.value(5)>1: skill 460
3. try to update NW.js
4. check what AI-priority codings cause the most lag.

Thx for the help guys, I will keep you updated! 🙂
Code:
EVAL if (user.isActor()) $gameVariables.value(2)>1; else $gameVariables.value(5)>1: skill 460
This is the correct code:

EVAL user.isEnemy() && $gameVariables.value(5) > 1: skill 460
EVAL user.isActor() && $gameVariables.value(2) > 1: skill 460

Thx for mentioning! 🙂
I am going to reiterate @ATT_Turan 's point: either you are programming an AI for an enemy (in which case the whole second eval can be deleted, since it will never be true) or you are programming an AI for an actor with auto-battle (in which case the whole first eval can be deleted, since it never will be true).
Either way you have your computer performing several function calls to first parse and then execute a line with the (as previously discussed) expensive eval function for a result you know will never be true. I am willing to bet that this kind of programming is a major contributer to the bad performance you are experiencing.

Edit: On another note, what are you tracking with these game variables anyway? Maybe there is a more efficent way to set up your system that doesn't involve first setting and then reading game variables?
I am going to reiterate @ATT_Turan 's point: either you are programming an AI for an enemy (in which case the whole second eval can be deleted, since it will never be true) or you are programming an AI for an actor with auto-battle (in which case the whole first eval can be deleted, since it never will be true).
Either way you have your computer performing several function calls to first parse and then execute a line with the (as previously discussed) expensive eval function for a result you know will never be true. I am willing to bet that this kind of programming is a major contributer to the bad performance you are experiencing.
I actually do use the ai for actors and enemies since it is a monster capturing game.
Edit: On another note, what are you tracking with these game variables anyway? Maybe there is a more efficent way to set up your system that doesn't involve first setting and then reading game variables?
the &gameVariables are used to track the amount of active shieldwalls (states) the $gameParty or $gameTroop have and it works well.

I think this should be fine tbh, it is quite short:
EVAL user.isEnemy() && $gameVariables.value(5) > 1: skill 460
EVAL user.isActor() && $gameVariables.value(2) > 1: skill 460

update:
1. fix EVAL AIManager.targetCondition
i fixed it by sorting my plugins correctly
2. check if this works: EVAL if (user.isActor()) $gameVariables.value(2)>1; else $gameVariables.value(5)>1: skill 460
the code above works fine and is short! 🙂
3. try to update NW.js
i updated to the latest nw.js version, but that didnt fix the performance issue. It probably increased performance tho.

Now I am about to update pixi. And then I am going to check what <AI Priority> codes cause the most performance issue and try to optimize them. Then I am going to try to not use notetags entirely, since they are eval strings and every single notetag uses complex functions and if else statements.
I actually do use the ai for actors and enemies since it is a monster capturing game.
But even then, unless you modified the code to be otherwiseyou either have a Game_Enemy object, initialized from the enemies database tab (and hence using the notefield for the enemy), or a Game_Actor object, initialized from the actors databse tab and using the notefield from there (or in case of Yanfly's Auto Battle AI plugin from the classes database tab). These are different notefields. Put the one for actors in the class notefield and the one for enemies in the enemies notefield.

And then I am going to check what <AI Priority> codes cause the most performance issue and try to optimize them.
That's a good step. As long as we don't have any solid data on which conditions are the actual bottlenecks, anyone trying to help is taking shots in the dark anyway.
This is the correct code:
I mean, I'm glad it works for you, but that isn't any more correct than what I gave you. It's splitting it into two evals which, if they both execute, is more computer-intensive than a single eval with a conditional.

However, I didn't even think about the part that @eomereolsson mentioned, where these notetags aren't going in skills, they're going on the actors and enemies specifically. So it extra doesn't make sense to be checking for either inside of these checks.

You should have one notetag on the enemies that checks for variable 5, one notetag on actors that checks variable 2, that's it.
I just turned all of the Yanfly AI Plugins off and pasted the following code in my own Plugin. It works perfectly. Now I just need to code the targets and the conditions for each skill and it's done. It even saves me time, because I don't need to insert the <AI Priority> at all. I am quite sure that this method will work without any performance issues(future me: it works without performance issues!).

Code:
Game_Enemy.prototype.makeActions = function() {
    Game_Battler.prototype.makeActions.call(this);
        this.action(0);
    var skillIdList = this.skills().map(function(a) {return a.id;});
    var skillTarget = 0;
 
    if (skillIdList.contains(371)) {
    if (this.meetsSkillConditions($dataSkills[371])) {
        this.action(0).setSkill(371);
        this.action(0).setTarget(skillTarget);
this.setActionState('waiting');
return;
    }
    } 
 
    };

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/yep_battleaicore-creates-lag-fps-drop-to-8.150880/