Author Topic: applying getRandom to specific members of a client group?  (Read 2120 times)

How do I store the candidates in an array?  I don't think I have needed to use arrays before, so I don't know how to add this into my code.

Code: [Select]
function pickWinner() {
    for(%i = 0; %i < clientGroup.getCount(); %i++) {
        %client = clientGroup.getObject(%i);
        if(passesClientTest(%client))
             %candidates[(%candidates++)-1] = %client;
    }

    return %candidates[getRandom(%candidates-1)];
}

EDIT: Whoops, thought I could save using unnecessary arithmetic but upon sitting back down I realized that was incorrect.
« Last Edit: June 14, 2015, 05:47:26 PM by $trinick »

A bit off topic, but I hate how %array[%num++] (with both undefined/blank) sets index 1 instead of 0. I guess the TGE devs didn't implement the postfix increment operator properly either.

Code: [Select]
function pickWinner() {
    for(%i = 0; %i < clientGroup.getCount(); %i++) {
        %client = clientGroup.getObject(%i);
        if(passesClientTest(%client))
             %candidates[(%candidates++)-1] = %client;
    }

    return %candidates[getRandom(%candidates-1)];
}

EDIT: Whoops, thought I could save using unnecessary arithmetic but upon sitting back down I realized that was incorrect.

I see is you changed it to -1?  Why do people add or subtract 1 when they do getRandoms on variables?

How do I detect if there are no available candidates?
« Last Edit: June 14, 2015, 06:34:05 PM by Tezuni 2.0 »

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 2


Counting in programming starts at 0 by convention, so you want the first entry to be %variable[0].

So you could either do this:

Code: [Select]
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.
« Last Edit: June 14, 2015, 06:50:11 PM by $trinick »