Don't feel like you need to apologize! I'm happy to help.
So, that is a "for loop". I'm not sure how much or how little you know about Javascript, so you may or may not know this stuff already. Sorry if I'm covering what you know!
A for loop runs a bunch a code a number of times. I'll break down this code:
Code:
for (var i = 0; i < $gameTroop.aliveMembers().length; ++i) {
if ($gameTroop.aliveMembers()[i].isStateAffected(nState))
n = n + 1;
}
There is a variable, called "i". i is set, initially, to 0. The for loop is set to run over and over again
for each enemy that is alive. So, if there are 3 alive enemies, the for loop will run 3 times.
++i is a phrase that adds 1 to the variable i. That's what makes the loop only happen 3 times, instead of infinitely.
So, to break down the for loop into common language:
Code:
for (variable "i" starts at 0; loop while variable "i" is less than the number of living enemies; add 1 to variable "i" after every loop) {
if (enemy in slot "i" is effected by the chosen state)
add 1 to variable "n";
}
The thing about $gameTroop, is that the 1st enemy is considered Enemy 0, the second enemy is Enemy 1, and so on. That's why the for loop starts at 0, and continues while the variable is
less than the length. Because, if there are three enemies, the final Enemy will be in slot 2, not slot 3.
I hope that helps! And I hope I'm not getting anything wrong! Most of Javascript I learned
through RPG Maker, so I hope I'm no getting anything wrong here!
(And I am repeating the fact that the code I'm giving you is meant to be easy to read and edit. There is much smoother code that you could write, but it gets harder to read. Many times, you can compress multiple lines of code into a single line. I'm not doing that here, to make it all easier to read.)
For more information on for loops, check out this helpful link:
For Loops can execute a block of code a number of times.
www.w3schools.com
That website is great for learning Javascript in general (as well as other programming languages). This specific page is on for loops.
To answer your second question, yes, $gameParty is the actor based equivalent to $gameTroop. In fact, you don't even need to copt the function. You can just use one function, like this:
Code:
twoOrMoreInUnit = function(nState, nUnit) {
var n = 0;
for (var i = 0; i < nUnit.aliveMembers().length; ++i) {
if (nUnit.aliveMembers()[i].isStateAffected(nState))
n = n + 1;
}
if(n >= 2)
return true;
else
return false;
};
This function now can check either $gameTroop or $gameParty. Just insert either you want into the parameters.
So, to check if there are at least 2 enemies effected by state 5, use this:
twoOrMoreInUnit(5, $gameTroop);
Or, to check if there are at least 2 actors in the party effected by state 8, use this:
twoOrMoreInUnit(8, $gameParty);