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:
(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.
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.
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
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