MV Adding "Targeted?" function - related but maybe not central to Battle AI Core

One of the things I'm trying to add to my game to make enemies a bit smarter is a function to check if another enemy has already decided to use a skill on a given target. And I think it almost works, but a couple weird things are going on. I assume it's a misunderstanding on my part on how one thing or another works and it may be simple, but basically it works relatively well on the first two enemies in a troop, but then it does not work for the enemies with index 2 and after. Additionally, when logging some things to the console, it becomes apparent that the likely reason for this is that it's checking the later enemies twice instead of once. Thing is, I don't see where I could have written anything in the function to do that. The function is below:

Code:
Game_Battler.prototype.isTargetedBy = function(skillId, userObj) {
  var unit = $gameTroop.aliveMembers()
  for (i=0;i<unit.length;i++){
    var member = unit[i]
    console.log("This evaluation is for user " + userObj.index() + " compared to member " + member.index() + " for target " + this.index())
    if (member!==userObj&&member._actions.length>0){
      console.log(member._actions[0]._item._itemId)
      if (member._actions[0]._item._itemId === skillId){
      console.log("They are using this skill, and... ")
      console.log(this.index())
      console.log(member._actions[0]._subjectEnemyIndex)
      if (member._actions[0]._subjectEnemyIndex === this.index()){
        console.log("they are targeting me!")
        console.log("the evaluation for target " + this.index() + " is true")
        return true
      }
    }
    } else if (member===userObj){
      console.log("Same guy!")
      continue
    }
  }
  console.log("the evaluation for target " + this.index() + " is false")
    return false
}

(skillId would be replaced with the skill id of the skill checked on, userObj is generally replaced by the variable user, to ensure that the function doesn't give a false positive for the user's own targeting)

The eval I use for Battle AI Core to test this out is pretty simple:
Eval !target.isTargetedBy(47, user) +++ State !== state 24: Coo, Highest ATK
(I've added Turan's edit to Battle AI Core, so target is a valid variable to use in this case)

I've also put the console log below, mostly just to show what I mean when I say that its checking the enemy with an index of 2 twice instead of once. Totally understand if no one has an answer, but I'm at a loss and if its something very simple that I'm overlooking I'd love to know.
Spoiler: Big Console Log
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 0 for target 0
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 1 for target 0
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 2 for target 0
YEP_BattleAICore.js:687 the evaluation for target 0 is false
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 0 for target 1
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 1 for target 1
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 2 for target 1
YEP_BattleAICore.js:687 the evaluation for target 1 is false
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 0 for target 2
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 1 for target 2
YEP_BattleAICore.js:669 This evaluation is for user 0 compared to member 2 for target 2
YEP_BattleAICore.js:687 the evaluation for target 2 is false
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 0 for target 0
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 0
YEP_BattleAICore.js:675 0
YEP_BattleAICore.js:677 they are targeting me!
YEP_BattleAICore.js:678 the evaluation for target 0 is true
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 0 for target 1
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 1
YEP_BattleAICore.js:675 0
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 1 for target 1
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 2 for target 1
YEP_BattleAICore.js:687 the evaluation for target 1 is false
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 0 for target 2
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 2
YEP_BattleAICore.js:675 0
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 1 for target 2
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:669 This evaluation is for user 1 compared to member 2 for target 2
YEP_BattleAICore.js:687 the evaluation for target 2 is false
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 0 for target 0
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 0
YEP_BattleAICore.js:675 0
YEP_BattleAICore.js:677 they are targeting me!
YEP_BattleAICore.js:678 the evaluation for target 0 is true
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 0 for target 1
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 1
YEP_BattleAICore.js:675 0
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 1 for target 1
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 1
YEP_BattleAICore.js:675 1
YEP_BattleAICore.js:677 they are targeting me!
YEP_BattleAICore.js:678 the evaluation for target 1 is true
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 0 for target 2
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 2
YEP_BattleAICore.js:675 0
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 1 for target 2
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:673 They are using this skill, and...
YEP_BattleAICore.js:674 2
YEP_BattleAICore.js:675 1
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 2 for target 2
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:687 the evaluation for target 2 is false
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 0 for target 0
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 1 for target 0
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 2 for target 0
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:687 the evaluation for target 0 is false
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 0 for target 1
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 1 for target 1
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 2 for target 1
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:687 the evaluation for target 1 is false
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 0 for target 2
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 1 for target 2
YEP_BattleAICore.js:671 47
YEP_BattleAICore.js:669 This evaluation is for user 2 compared to member 2 for target 2
YEP_BattleAICore.js:683 Same guy!
YEP_BattleAICore.js:687 the evaluation for target 2 is false
Hmmm, not sure if I thought that properly through, but when you're checking
member._actions[0]._subjectEnemyIndex === this.index()

Shouldn't you instead check
member._actions[0]._targetIndex === this.index()
Hmmm, not sure if I thought that properly through, but when you're checking
member._actions[0]._subjectEnemyIndex === this.index()

Shouldn't you instead check
member._actions[0]._targetIndex === this.index()
Changed it just now just to make sure, but so far it's doing the same thing. I think _subjectEnemyIndex and _targetIndex just return the same number, maybe they're different in various contexts though.
Huh? Isn't subject the one acting? And target the one targeted? Haven't checked at what point the _targetIndex is set though.
You're possibly seeing it twice because they "simulate" their possible actions to determine the best one, and then choose what they're doing.

If that enemy has 2 possible actions and the other only does 1 thing, I'd bet money on that being it. It's easy to test, though: add more actions to each enemy's action list and see if you get more log messages.
You're possibly seeing it twice because they "simulate" their possible actions to determine the best one, and then choose what they're doing.

If that enemy has 2 possible actions and the other only does 1 thing, I'd bet money on that being it. It's easy to test, though: add more actions to each enemy's action list and see if you get more log messages.
Okay, I think this is a lead, thank you! It WAS showing up because of another move, but all three enemies were the same so I thought that wouldn't be it. I BELIEVE the reason the last one was moving on to the next move when the others do not is because for some reason the last enemy is returning the function as "true" in a way that the others are not. There are three actors involved in the combat, so there should be one for each enemy to target. Each enemy runs the function three times, so in the console they return the function as false/false/false, then true/false/false, and the last comes up as true/true/false. That should really be expected, and the enemy should target the last target showing up as "false" but it's still not doing it.

That being said, I've found a possible breakthrough when messing with the moves and testing your idea. Which is that apparently changing the targeting condition in the notetag from "Highest ATK" to "First" breaks the function's intention. ALL enemies now use the Coo skill, as I wanted actually, but ALL now target the actor with an index of 0, which is the opposite of what I wanted (though I am okay with this if its only a problem with First, I don't intend to use First as a condition outside of testing). While set to Highest ATK, the two enemies that DID use the Coo skill split their targets as expected, it was just that the last enemy didn't use Coo. So whatever is screwing with this probably has something to do with the targeting condition in the notetag and how it's processed.
But these are the codes from the core files:

Game_Action.prototype.initialize = function(subject, forcing) {
this._subjectActorId = 0;
this._subjectEnemyIndex = -1;
this._forcing = forcing || false;
this.setSubject(subject);
this.clear();
};

Game_Action.prototype.setSubject = function(subject) {
if (subject.isActor()) {
this._subjectActorId = subject.actorId();
this._subjectEnemyIndex = -1;
} else {
this._subjectEnemyIndex = subject.index();
this._subjectActorId = 0;
}
};

So am I confused here or isn't your code just comparing enemy index to actor index?

Are you using a system where the targets have even been decided?
But these are the codes from the core files:

Game_Action.prototype.initialize = function(subject, forcing) {
this._subjectActorId = 0;
this._subjectEnemyIndex = -1;
this._forcing = forcing || false;
this.setSubject(subject);
this.clear();
};

Game_Action.prototype.setSubject = function(subject) {
if (subject.isActor()) {
this._subjectActorId = subject.actorId();
this._subjectEnemyIndex = -1;
} else {
this._subjectEnemyIndex = subject.index();
this._subjectActorId = 0;
}
};

So am I confused here or isn't your code just comparing enemy index to actor index?

Are you using a system where the targets have even been decided?
I'm pretty sure its not just doing that, as it is partially working if it wasn't for the weird last enemy interaction. I'll show what I mean in a video, the function has not been changed from my above code (and it does the same thing with _targetIndex or _subjectEnemyIndex used):

A plugin of mine is probably changing what _targetIndex is doing, but _subjectEnemyIndex doesn't appear in a search of my plugins. In any case, those targets have to be decided before they are performed because of the ATB system I'm using. I figured to use _targetIndex or (what I went with, but so far interchangeable for this purpose) _subjectEnemyIndex because they were changing based on the targets in the action object when I was logging them on the console. I will probably change it to _targetIndex though, because after looking into it, it does seem to be referenced far more often.

todd.jpg
Okay, I figured it out guys. The solution was really simple, it was just a matter of noticing the damn thing right in front of my face.

As I said earlier, I am using Turan's tweak for Battle AI Core, which makes "target" a valid variable in its Eval condition. Without going into too much detail, one of the checks that tweak does when checking for a target is check if group.length > 1. So if there's only one valid target, it won't work, which is the case of course when all other actors are being targeted. I changed it to checking if group.length >= 1 and it works perfectly.

Most people won't care, but I'm updating this thread that its solved just in case anyone wants to use my function as an eval in their own games. Just make sure to add that = after the > on that line in Turan's tweak, (and maybe remove all the logs to the console, as they are not necessary) and you'll have a function to make your enemies a bit smarter.

Original thread: https://forums.rpgmakerweb.com/threads/adding-targeted-function-related-but-maybe-not-central-to-battle-ai-core.181397/