YEP_X_CounterControl

Hi, so I'm currently encountering difficulties due to some inherent incompatibilities between YEP_X_CounterControl and LeTBS. I think I've found a simple solution, but it would involve making the <Default Counter> notetag useable in more than just actor & enemy noteboxes. At a minimum, I need to be able to use it in the weapons notebox, but other areas could prove useful. Anyways, I have no idea what part of the code dictates where it can be used. Here's the snippet of code for <Default Counter> in YEP_X_CounterControl.js.
JavaScript:
DataManager.processCounterNotetags1 = function(group) {
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    var notedata = obj.note.split(/[\r\n]+/);

    obj.defaultCounter = Yanfly.Param.CounterDefault;
    obj.counterTotal = Yanfly.Param.CounterTotal;

    for (var i = 0; i < notedata.length; i++) {
      var line = notedata[i];
      if (line.match(/<DEFAULT COUNTER:[ ](\d+)>/i)) {
        obj.defaultCounter = parseInt(RegExp.$1);
      } else if (line.match(/<DEFAULT COUNTER:[ ](.*)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var id = Yanfly.SkillIdRef[name];
        if (id) obj.defaultCounter = id;
      }
    }
  }
};

Edit: Could this have something to do with it?
JavaScript:
Yanfly.Counter.DataManager_isDatabaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
  if (!Yanfly.Counter.DataManager_isDatabaseLoaded.call(this)) return false;
  if (!Yanfly._loaded_YEP_X_CounterControl) {
    this.processCounterNotetagsI($dataItems);
    this.processCounterNotetagsW($dataWeapons);
    this.processCounterNotetagsA($dataArmors);
    this.processCounterNotetagsS($dataSkills);
    this.processCounterNotetagsT($dataStates);
    this.processCounterNotetagsSys($dataSystem);
    this.processCounterNotetags1($dataActors);
    this.processCounterNotetags1($dataEnemies);
    this.processCounterNotetags2($dataActors);
    this.processCounterNotetags2($dataClasses);
    this.processCounterNotetags2($dataEnemies);
    this.processCounterNotetags2($dataWeapons);
    this.processCounterNotetags2($dataArmors);
    this.processCounterNotetags2($dataStates);
    this.processCounterNotetags3($dataSkills);
    this.processCounterNotetags4($dataSkills);
    this.processCounterNotetags4($dataItems);
    Yanfly._loaded_YEP_X_CounterControl = true;
  }
  return true;
};
Code:
this.processCounterNotetags1($dataActors);
    this.processCounterNotetags1($dataEnemies);

These lines in DataManager.isDatabaseLoaded are where you want to be looking. If you want to process notetags in more groups than the ones the plugin already does, add them here.

Note that depending on what those notetags end up doing, you may have to tweak other parts of the plugin to deal with the new groups, especially if the notetags cause manipulation of properties unique to the groups in question.
Damn, I was hoping it'd be a simple tweak. I added this.processCounterNotetags1($dataWeapons); but upon doing a counter in battle, it's not using the counter skill I set...hmm.
Do a search in the plugin to see what the defaultCounter property is actually doing.
Besides the code in my OP, the only other times "defaultCounter" come up in the plugin is:
JavaScript:
Game_Actor.prototype.makeCounterSkills = function() {
    Game_Battler.prototype.makeCounterSkills.call(this);
    var length = this.equips().length;
    for (var i = 0; i < length; ++i) {
      var obj = this.equips()[i];
      if (!obj) continue;
      if (obj.counterSkills) {
        Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      }
      this.extendCounterSkillsEval(obj);
    }
    if (this.currentClass().counterSkills) {
      obj = this.currentClass();
      Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      this.extendCounterSkillsEval(obj);
    }
    if (this.actor().counterSkills) {
      obj = this.actor();
      Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      this.extendCounterSkillsEval(obj);
    }
    if (this._counterSkills.length <= 0) {
      this._counterSkills.push(obj.defaultCounter);
    }
};
and also
JavaScript:
Game_Enemy.prototype.makeCounterSkills = function() {
    Game_Battler.prototype.makeCounterSkills.call(this);
    if (this.enemy().counterSkills) {
      obj = this.enemy();
      Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      this.extendCounterSkillsEval(obj);
    }
    if (this._counterSkills.length <= 0) {
      this._counterSkills.push(obj.defaultCounter);
    }
};
Shouldn't the code in my OP be setting obj.defaultCounter to whatever skill ID is in the notetag?
Edit: I'm referring to this...
JavaScript:
DataManager.processCounterNotetags1 = function(group) {
  for (var n = 1; n < group.length; n++) {
    var obj = group[n];
    var notedata = obj.note.split(/[\r\n]+/);

    obj.defaultCounter = Yanfly.Param.CounterDefault;
    obj.counterTotal = Yanfly.Param.CounterTotal;

    for (var i = 0; i < notedata.length; i++) {
      var line = notedata[i];
      if (line.match(/<DEFAULT COUNTER:[ ](\d+)>/i)) {
        obj.defaultCounter = parseInt(RegExp.$1);
      } else if (line.match(/<DEFAULT COUNTER:[ ](.*)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var id = Yanfly.SkillIdRef[name];
        if (id) obj.defaultCounter = id;
      }
    }
  }
};
What is the one for weapons doing?
What is the one for weapons doing?
I just put a simple <Default Counter: 292> notetag in the weapon's notebox. Or do you mean the plugin's code? As far as I'm aware, there's no instance of defaultCounter being used other than in the sections I've posted above. I'm guessing that's my issue huh? I was under the assumption this snippet would set obj.defaultCounter to the ID listed in the notetag.
JavaScript:
      if (line.match(/<DEFAULT COUNTER:[ ](\d+)>/i)) {
        obj.defaultCounter = parseInt(RegExp.$1);
      } else if (line.match(/<DEFAULT COUNTER:[ ](.*)>/i)) {
        var name = String(RegExp.$1).toUpperCase();
        var id = Yanfly.SkillIdRef[name];
        if (id) obj.defaultCounter = id;
I just wanted to make sure the weapon part wasn't overriding that.
Sorry if this is not helpful, but why not just use the custom counter skills lunatic code since it works in the weapon note tag box?

Code:
  <Custom Counter Skills>
   if (user.name() === 'Harold') {
     skills.push(50, 51, 52);
   } else if (user.name() === 'Therese') {
     skills.push(53, 54, 55);
   } else if (user.name() === 'Marsha') {
     skills.push(56, 57, 58);
   } else if (user.name() === 'Lucius') {
     skills.push(59, 60, 61);
   }
  </Custom Counter Skills>

Since it uses skills.push, if you reduce the skill Id to just the one skill you would like to use as the counter, would that not then technically become the 'default' counter associated with that weapon?
Because I'm using LTBS, which totally ignores Yanfly's CounterControl plugin. I was able to do a hackjob with the code so it would at least read defaultCounter. If I could get it to read the counter skill array, then I'd happily do that, but I haven't been able to after several attempts.

I've tried your suggestion at the simplest scale, i.e. <Counter Skills: 3> but it didn't make that the defaultCounter...
Oh, okay, sorry then.
I appreciate the attempt though lol.

---

Ok, instead of getting defaultCounter to work in the Weapons' notetag, I think a different approach would be better. I'm thinking of calling the skill ID of a skill within the array of possible counter skills, as defined by the Yanfly CounterControl plugin. So basically obj.counterSkills instead of obj.defaultCounter. The problem is, I'm currently using $dataSkills[$dataActors[entity.battler().actorId()].defaultCounter]; to call the skill ID of the counter. It fails when I try to sub counterSkills in its place, due to it being an array. How do I have it pick a single skill ID out of that array of skill IDs? Any method will do, even random. I'm probably gonna have to use Math.floor and Math.random to do this, and probably "length", but I'm lost as far as syntax goes...

Edit: I'm also not sure whether I'm supposed to use counterSkills or _counterSkills, whats the difference? Btw, this is the code in YEP_X_CounterControl.js for counter skill creation.
JavaScript:
Game_Battler.prototype.counterSkills = function() {
    if (this._counterSkills === undefined || this._counterSkills === []) {
      return [];
    }
    var array = [];
    var length = this._counterSkills.length;
    for (var i = 0; i < length; ++i) {
      var skillId = this._counterSkills[i];
      var skill = $dataSkills[skillId];
      array.push(skill);
    }
    return array;
};

Game_Battler.prototype.makeCounterSkills = function() {
    this._counterSkills = [];
    var length = this.states().length;
    for (var i = 0; i < length; ++i) {
      var obj = this.states()[i];
      if (!obj) continue;
      if (obj.counterSkills) {
        Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      }
      this.extendCounterSkillsEval(obj);
    }
};
Or it might be this...
JavaScript:
Game_Actor.prototype.makeCounterSkills = function() {
    Game_Battler.prototype.makeCounterSkills.call(this);
    var length = this.equips().length;
    for (var i = 0; i < length; ++i) {
      var obj = this.equips()[i];
      if (!obj) continue;
      if (obj.counterSkills) {
        Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      }
      this.extendCounterSkillsEval(obj);
    }
    if (this.currentClass().counterSkills) {
      obj = this.currentClass();
      Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      this.extendCounterSkillsEval(obj);
    }
    if (this.actor().counterSkills) {
      obj = this.actor();
      Yanfly.Util.extend(this._counterSkills, obj.counterSkills);
      this.extendCounterSkillsEval(obj);
    }
    if (this._counterSkills.length <= 0) {
      this._counterSkills.push(obj.defaultCounter);
    }
};

Edit2: I just tried the following and got "Cannot set property 'commandOn' of undefined"
var counterId = Math.floor(Math.random()*$dataActors[entity.battler().actorId()]counterSkills.length) var skill = $dataSkills[$dataActors[entity.battler().actorId()].counterId];

This was the working code from before:
var skill = $dataSkills[$dataActors[entity.battler().actorId()].defaultCounter];

I also tried this and got the same error (still using same var counterId though)
var skill = $dataSkills[counterId];

Edit3: Lol derp, I forgot a period...ok, so now I'm using this, which at least let's me load into the game. However, when my unit tries to counter-attack, I get "Type Error: Cannot read property 'id' of null".
var counterId = Math.floor(Math.random()*$dataActors[entity.battler().actorId()].counterSkills.length) var skill = $dataSkills[counterId];

The error points to this line of code:
var data = obj.id === battler.attackSkillId() ? entity.getAttackScopeData() : entity.getObjectScopeData(obj);
Which leads me to think I need to include the skill's scope data...but I have no idea how. I find that weird since I didn't need to do so when I was using defaultCounter.

Edit4: Can someone please tell me how to get the skill IDs out of an array of skills? I think that's all I need to proceed.

This is what I got to work with so far:
I know that $dataActors[entity.battler().actorId()] will point to the unit in question, so I just need to tac on .counterSkills or ._counterSkills to the end of that to get the array of skills for countering right? I know I have to use .length somehow, but I don't know how to use this correctly. I know that ._counterSkills.length simply returns the amount of skills in the array, but not the skill ID of those skills themselves. How would I get the skill IDs of said skills? @Trihan, would you know how? Ideally, I'd like to select the skill with the highest skill ID in the counter skills array, so probably Math.max?

So if $dataActors[entity.battler().actorId()]._counterSkills would return an array containing skills 1, 2, & 3...how do I get it to select skill ID 3?

Edit5: What if I use reverse() to flip the array, then use slice(0, 1) to select the (now) 1st item in that array, which should be the highest skill ID. Or is there a simpler way?

Edit6: Ok, it looks like the find() function would better serve my needs. But once I obtain get the value of an element from the array, "return" simply spits it out right there and then, how do I put that into a var?

Original thread: https://forums.rpgmakerweb.com/threads/yep_x_countercontrol.124670/