I see is you changed it to -1? Why do people add or subtract 1 when they do getRandoms on variables?
%variable++ increases the value by 1 in
%variable.
So, without the -1, it would look like this:
%variable[1] = client 0
%variable[2] = client 1
%variable[3] = client 2Counting in programming starts at 0 by convention, so you want the first entry to be
%variable[0].
So you could either do this:
function pickWinner() {
%candidates = 0; // initialize value as zero so first time it's used it will be 0 instead of ""
for(%i = 0; %i < clientGroup.getCount(); %i++) {
%client = clientGroup.getObject(%i);
if(passesClientTest(%client)) {
%candidates[%candidates] = %client; // set as current candidates value
%candidates++; // then increase it
}
}
return %candidates[getRandom(%candidates-1)];
}
Which increases the length of the script by 3 lines, or you can increase the variable on the same line (which forces it to become a number so you don't have to initialize it) and just subtract one.
Then in
getRandom you subtract one because it's from 0 to that number. So if you have 3 candidates, %candidates will be set to 3 because it increased once each time you added one. So you'll get a number 0-3, but you only have 0-2.
How do I detect if there are no available candidates?
add
if(%candidates != 0) before the return line and anything after that return line will only happen if there are no candidates.