SA-MP Forums Archive
Quick question - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Quick question (/showthread.php?tid=502151)



Quick question - mirou123 - 22.03.2014

For example I have this Var[MAX_PLAYERS][Example][5] and Var[playerid][example][1] = 2 and Var[playerid][example][1] = 4 while all the others are 0.How can I find the sum of all these variables without doing this

Var[playerid][example][0] + Var[playerid][example][1] + Var[playerid][example][2].....
Because I use 69 instead of 5 (don't ask) and it will be too long if I use the obvious way


Re: Quick question - Konstantinos - 22.03.2014

By using a loop:
pawn Код:
new
    sum;

for (new i; i != 69; ++i) sum += Var[playerid][example][i];
Be sure that you change 69 with the exact size of the last dimension. A higher value can cause run time error: Array index out of bounds.


Re: Quick question - Sascha - 22.03.2014

pawn Код:
new sum = 0;
for(new n=0; n<69; n++)
{
   sum += Var[playerid][example][n];
}


edit: too late


Re: Quick question - mirou123 - 22.03.2014

Okay thanks I will try it right now. +rep'ed both of you