Check front position - Row Formation help

Hey, team!

I've been trying to implement a mechanic using Yanfly row formation plugin that is simple in my head, but hard in practice:
  • Each actor/enemy positioned in front of another one will "taunt" the normal attacks from the opposite team.
  • When the actor/enemy in front of another actor/enemy dies, the one backward "assumes" the "taunt" state. And so on...
My battle grid is 3x3, so I have 3 rows with 3 positions.
Row3Pos1 Row2Pos1 Row1Pos1
Row3Pos2 Row2Pos2 Row1Pos2
Row3Pos3 Row2Pos3 Row1Pos3
​
(If the character in R1P1 dies, the R2P1 character assumes the "Taunt" state, even if I have a Character in R1P2 with a "Taunt" state too. They will protect the characters in R3P1 and R2P2 respectively)

The only thing I could do without coding is give the "Taunt" state to everyone in first row. But if I have 2 characters there and 2 in the back rows, I'm unable to reach the backward ones until I kill everyone in Row 1, which is not the purpose...

I've trying this code, but it is definitely not working (everyone is getting the state even with someone in front of them):

JavaScript:
<Custom Row Condition>
if(user.isEnemy()){
var userRow = user.row();
var rowMembers = $gameTroop.rowAliveMembers(user.row - 1);
for (var i = 0; i < rowMembers.length; ++i) {
  var member = members[i];
  if (!member[0]) {
    condition = true;
  }
  else
  condition = false;
}
}
else if(user.isActor()){
var userRow = user.row();
var rowMembers = $gameParty.rowMembers(user.row - 1);
for (var i = 0; i < rowMembers.length; ++i) {
  var member = members[i];
  if (!member[0]) {
    condition = true
  }
}
}
</Custom Row Condition>

Please, can anyone help me?

The logic behind it was:
- Check if the state "user" is an Actor or an Enemy;
- If Enemy: have to use $gameTroop variables;
- If Actor: have to use $gameParty variables;
- Which is the user row?
- Who are the front row members?
- Check if the front row is not empty (or if the members in there aren't dead. I've try the two different versions in the same code just to see if the results change - but they don't);
- Check if the member in the position X in the front row exists/is alive (I created 3 states: each one per one different position in a row)
- If they do: do nothing (they already have the taunt state because it is given to everyone in row 1 by Row Formation plugin parameters - or with a similar code that shoud be working...);
- If they don't: condition is true and the state is activate in user
First quick thing - you don't need to code the whole thing twice for actors and enemies. Instead of referencing $gameParty and $gameTroop separately, you can reference user.friendsUnit() which will automatically point to the correct team.
Code:
for (var i = 0; i < rowMembers.length; ++i) {
  var member = members[i];
  if (!member[0]) {

There are several errors here.
1 - You set member = members[i], but there's no such thing as "members" in your code.

2 - You then reference member[0], which isn't anything because (if you had declared it correctly) member will be an individual battler, not an array.

3 - It's also impossible for member to not exist because your $gameParty/Troop would not have any elements that are undefined.

4 - It's not wrong, in this particular case, but it would be better practice to say i++ rather than ++i in your for loop. It doesn't break anything right here, but since typically ++i gets executed before the rest of the equation, you should get in the mental space of using it places where you do actually want it to happen first.

The logic behind it was:
- Check if the state "user" is an Actor or an Enemy;
- If Enemy: have to use $gameTroop variables;
- If Actor: have to use $gameParty variables;
As I suggested above, not necessary.
- Which is the user row?
- Who are the front row members?
One problem here is I don't see you make any allowance for the user to be in row 1. If they are, your code is going to try to check all of this in row 0, which doesn't exist.
- Check if the front row is not empty (or if the members in there aren't dead. I've try the two different versions in the same code just to see if the results change - but they don't);
Your loop isn't doing either of those things. For the row to be not empty, you would simply check rowMembers.length > 0. Since you already set rowMembers to rowAliveMembers previously, there's no reason to check that they aren't dead.
- Check if the member in the position X in the front row exists/is alive (I created 3 states: each one per one different position in a row)
I haven't played around a lot with the rows plugin, but most states are erased from the actor when they're knocked out, so you typically can't do this.

You can try doing this by changing the rowAliveMembers part, since you want to be seeing if the actor in front of you is knocked out...that's the whole point. Do the rows keep dead bodies in them? If so, this should work.

If not, you may need to look at the actual screen coordinates of other actors to see if someone is in front of you.

So if the rows do keep dead bodies in them, your code should simply be:
Code:
<Custom Row Condition>
condition=user.row()>1 && user.friendsUnit().rowMembers(user.row()-1)[user.friendsUnit().rowMembers(user.row()).findIndex(member => member==user)].isDead();
</Custom Row Condition>
OMG! Thank you so much for your reply!

There are several errors here.
1 - You set member = members[i], but there's no such thing as "members" in your code.

2 - You then reference member[0], which isn't anything because (if you had declared it correctly) member will be an individual battler, not an array.

3 - It's also impossible for member to not exist because your $gameParty/Troop would not have any elements that are undefined.

4 - It's not wrong, in this particular case, but it would be better practice to say i++ rather than ++i in your for loop. It doesn't break anything right here, but since typically ++i gets executed before the rest of the equation, you should get in the mental space of using it places where you do actually want it to happen first.

Indeed, it was late of night when I posted the code, so I hadn't noticed I copied/pasted the wrong old version of it, with the "members" and the "member[0]" errors you pointed out. 😳 Thank you for your feedback!

One problem here is I don't see you make any allowance for the user to be in row 1. If they are, your code is going to try to check all of this in row 0, which doesn't exist.

Oh! Right, I forgot to mention it, but I created 3 different states for each row (and assumed a default row number to each one). The code I used as example was for someone in the row 2 only. For row 1 I have no code at all, since the state it's added to everyone in row 1 by Row Formation plugin parameters.

Your loop isn't doing either of those things. For the row to be not empty, you would simply check rowMembers.length > 0. Since you already set rowMembers to rowAliveMembers previously, there's no reason to check that they aren't dead.

Hmm, got it! I thought I has understood how rowMembers and rowMembersAlive works, but now I know I hadn't :LZSlol: Thank you for your explanation!

So if the rows do keep dead bodies in them, your code should simply be:

Awesome! I'll try this code, later I come back and edit this post to tell if it works. For now, I intend to keep the dead bodies, but in the future (when I implement a Summon system), some characters will disappear after dead. For now, one step at a time, right? :LZSjoy:
For now, I intend to keep the dead bodies
Well, this was me asking you a question about the functionality of the Row Formation plugin - do the dead bodies stay? If someone bites it in row 2, does their body stay there? If so, I think the code I gave you should work.
Well, this was me asking you a question about the functionality of the Row Formation plugin - do the dead bodies stay? If someone bites it in row 2, does their body stay there? If so, I think the code I gave you should work.
Ohh, sorry! Yes, they stay (by default). And the code worked!!!

The only "bug" is: I have 4 characters in my team, disposed like that:
nobody | Row2Pos1
Row1Pos2 | Row2Pos2
nobody | Row2Pos3
​
the character in Row2pos1 is not receiving the state, but the character in Row2pos2 (which has someone in front of it, in Row1pos2) is. Do you know why this is happening?
(no characters are dead. The other character, in Row2pos3 is receiving the state correctly)
Because there is no Row1Pos1 to look at. I guess the formations part of the plugin must use undefined elements to track that.

I can look at the code if this doesn't work, but try this change:
Code:
var front=user.row()>1 ? user.friendsUnit().rowMembers(user.row()-1)[user.friendsUnit().rowMembers(user.row()).findIndex(member => member==user)] : user;
condition=front!=user && (!front || front.isDead());
Because there is no Row1Pos1 to look at. I guess the formations part of the plugin must use undefined elements to track that.

I can look at the code if this doesn't work, but try this change:
Code:
var front=user.row()>1 ? user.friendsUnit().rowMembers(user.row()-1)[user.friendsUnit().rowMembers(user.row()).findIndex(member => member==user)] : user;
condition=front!=user && (!front || front.isDead());
Hmm... Apparently, it produced the same result:

rowposition.png

Original thread: https://forums.rpgmakerweb.com/threads/check-front-position-row-formation-help.146223/