Conditional weapon range with Yanfly's Plugins

Hi, everyone

I currently make a project ONLY with Yanfly's plugins.
I use many of the plugins, but I currently have troubles setting conditional weapon range based on the row the battler in.
I think it should be related with YEP Target Core, Selection Control, and Row Formation.

I use 2 rows in my project
So... the effect I'm looking for is:
1. If spear-wielding battler is in the front row, he can attack any enemy, but if he's at the back row, he can only attack front row.
2. If bow/gun-wielding battler is in the front row, he can only attack back row, but if he's at the back row, he can attack any enemy.
I think it should be related with <Custom Select Condition> note tag, but I'm not proficient in JS. I prefer making different skills for each weapon range since the name differs, but if the <Custom Select Condition> works, I'll go with it.

So far, I try using Weapon Unleash's lunatic mode <Custom Replace Attack> tags, but it didn't work and shows the basic attack instead.
Spoiler: Spear note tags
<Custom Replace Attack>

if (user.battler.row() === 1) {

id = 21;

} else {

id = 22;

}

</Custom Replace Attack>

21 is all enemy and 22 is front row only

Spoiler: Bow/Gun note tags
<Custom Replace Attack>

if (user.battler.row() === 1) {

id = 23;

} else {

id = 24;

}

23 is back row only and 24 is all enemy
</Custom Replace Attack>

Also, since I have skills whose range is based on equipped weapons', which solution is best?

UPDATE: I figured out that the 'battler' should be replaced by user OR target and it works perfectly, just as I want, so there's no need to answer my question 🙂 Cheers!

EDIT AGAIN: it seems that I'm stuck at using the <Custom Select Condition> note tags since some of my skills are generic and their range is based on weapons... I try

<Custom Select Condition>
if (user.row() === 1) {
condition = Any Row;
} else {
condition = Front Row Only;
}
</Custom Select Condition>

for spear but nothing changes... can anyone help?
EDIT AGAIN: it seems that I'm stuck at using the <Custom Select Condition> note tags since some of my skills are generic and their range is based on weapons... I try

<Custom Select Condition>
if (user.row() === 1) {
condition = Any Row;
} else {
condition = Front Row Only;
}
</Custom Select Condition>

for spear but nothing changes... can anyone help?
Yes, you'll need the script equivalent of those labels, "Any Row" and "Front Row Only" don't mean anything out of context. 😅

I haven't tested, but maybe try something like this?
Code:
<Custom Select Condition>
if (user.row() === 1) {
  // Front row, all targets selectable
  condition = true;
} else {
  // Only row-1 targets selectable
  condition = target.row() === 1;
}
</Custom Select Condition>
Also note that this notetag is valid only on skills and items~

Edit: alternatively you may be able to use this for the front-row check:
Code:
  condition = this.selectConditionFrontRow(target);
I think that'll count rows behind row 1 if row 1 is empty. ^_^
Oops -_- I guess this is why it doesn't work for weapons (I put the tags in weapon, silly me), so I'm stuck to the weapon unleash (since it works well). Should I put the tags in the universal weapons I have? How to put it there? I think it's simpler if the game checks if user have certain skill since I literally have different skill for each kind of attacks due to weapon's unleash.
I'm not sure what the issue is? Can't you just change the selection condition for each type of weapon skill? Or are the Weapon Unleash tags not working as expected? 😅

You can check whether the skill user (actors only) has a certain skill:
Code:
user.isLearnedSkill(52)
Just replace 52 with the skill ID.
The weapon unleash works perfectly. I have physical-based elemental skill whose range is dependent on the weapon. since skills are bound to accessory, I need to make them dependent on weapon's range, thus requiring 'if' condition. The physical classes can use any of those skills, only different in term of range.

Code:
<Custom Select Condition>
if (user.row() === 1)
  if (user.isWtypeEquipped(2 || 3 || 4 || 13) {
    condition = Front Row Only;
  } else if (user.isWtypeEquipped(1 || 5 || 6 || 14) {
    condition = Any Row;
  } else {
    condition = Back Row Only;
  }
  else
    if (user.isWtypeEquipped(2 || 3 || 4 || 13) {
    condition = false;
  } else if (user.isWtypeEquipped(1 || 5 || 6 || 14) {
    condition = Front Row Only;
  } else {
    condition = Any Row;
</Custom Select Condition>

I've tried the above tag, but no luck either. Does the skill which replaces normal attack doesn't count as learnt skill? so as other skills learnt through trait?

If nothing works I'll just let them attack row-unspecific 1 random enemy with the skill...
OK, so there are a few mistakes in that script:
  1. Code:
    2 || 3 || 4 || 13
    This will return 2, since the logical "or" operator stops when it finds a "truthy" value (e.g. a non-zero number). This means the condition reduces to "is weapon type 2 equipped?". You're only calling the function once, so it only runs once.
  2. As mentioned, "Front Row Only" isn't recognised as you seem to think it is. The compiler will see it as three (undefined) variables one after the other with no instructions on how to combine them, which will cause an error. 😅
  3. You've also forgotten a few { opening and closing } braces on your if blocks. 😛
Try this instead: 😤
Spoiler
Code:
<Custom Select Condition>
if ([2,3,4,13].some(function(w) { return user.isWtypeEquipped(w); }) {
  if (user.row() === 1) {  // Front Row
    condition = this.selectConditionFrontRow(target);
  } else {                 // None
    condition = false;
  }
} else if ([1,5,6,14].some(function(w) { return user.isWtypeEquipped(w); }) {
  if (user.row() === 1) {  // Any Row
    condition = true;
  } else {                 // Front Row
    condition = this.selectConditionFrontRow(target);
  }
} else {
  if (user.row() === 1) {  // Back Row
    condition = this.selectConditionBackRow(target);
  } else {                 // Any Row
    condition = true;
  }
}
</Custom Select Condition>
I've tried to copy your structure in the conditions. Basically, in this tag the weapon type is checked multiple times using the Array.some function (true if any one is true). I've also swapped the "Front Row Only" stuff for actual script calls. Haven't tested, but I think it should be OK? 😅
nope... didn't work... the second statement is for middle-ranged weapons and the user still can't target back row from front row. I've even tried this:
Code:
<Custom Select Condition>
if ([2,3,4,13].some(function(w) { return user.isWtypeEquipped(w); }) {
  if (user.row() === 1) {  // Front Row
    condition = this.selectConditionFrontRow(target);
  } else {                 // None
    condition = false;
  }
} else if ([1,5,6,14].some(function(w) { return user.isWtypeEquipped(w); }) {
  if (user.row() === 1) {  // Any Row
    condition = this.selectConditionAnyRow(target);
  } else {                 // Front Row
    condition = this.selectConditionFrontRow(target);
  }
} else {
  if (user.row() === 1) {  // Back Row
    condition = this.selectConditionBackRow(target);
  } else {                 // Any Row
    condition = this.selectConditionAnyRow(target);
  }
}
</Custom Select Condition>

Didn't work either...
Okies I've had a chance to test this one out and it works for me: 😂
Spoiler
Code:
<Custom Select Condition>
if (!user.isActor()) {     // Enemies don't have weapons
  condition = true;
} else if ([2,3,4,13].some(function(w) { return user.isWtypeEquipped(w); })) {
  if (user.row() === 1) {  // Target front row
    console.log('target front row!');
    condition = this.selectConditionFrontRow(target);
  } else {                 // Cannot target any
    console.log('target no-one! D:');
    condition = false;
  }
} else if ([1,5,6,14].some(function(w) { return user.isWtypeEquipped(w); })) {
  if (user.row() === 1) {  // Target any row
    console.log('target any row! :D');
    condition = true;
  } else {                 // Target front row
    console.log('target front row!');
    condition = this.selectConditionFrontRow(target);
  }
} else {
  if (user.row() === 1) {  // Target back row
    console.log('target back row!');
    condition = this.selectConditionBackRow(target);
  } else {                 // Target any row
    console.log('target any row! :D');
    condition = true;
  }
}
</Custom Select Condition>
Basically, I added a failsafe for enemies (they don't use weapons so don't have an isWtypeEquipped function) and added a couple of closing brackets I missed before. 😴

I'd also suggest setting the Selection Control plugin's Weapon Range parameter to false, otherwise both restrictions will apply. 😅

The console.log lines are optional, you can delete them if you like. They just let you know what the notetag is doing (press F8 to view the console window during test-play).

If you still have problems, double-check you're using all the required plugins (Battle Engine Core, Target Core, Selection Control, Row Formation), that they're up-to-date, and in the correct order: https://yanflyengineplugins.itch.io/yanfly-engine-plugins-full-collection
Yayy 😂it worksss!!!!! Thanks a bunch!
(Probably my last question about this matter) But how to implement this into the enemies (using their IDs, which will be long...) since some enemies have visual weapons (say, enemy archers have bows) even though they have nothing officially equipped. Since I only do this with physical weapons, and have no plan to add more, could I do it on them too? I may need to make a copy for the enemy's skills...

Thanks a lot 😢 You've helped me well...
Maybe I'm overcomplicating it, but one option could be to add passive states (e.g. with Yanfly's Auto Passive States plugin) to all weapons and check the states instead of the equipped weapon type:
Code:
if ([42,43,44].some(function(s) { user.isStateAffected(s); })) {
Might be able to directly check the ID of the weapon image instead, but I'm not sure how. 😅

Like you suggest, it will probably easier all round to just have separate skills for actors and enemies. 😮‍💨

Original thread: https://forums.rpgmakerweb.com/threads/conditional-weapon-range-with-yanflys-plugins.107781/