SA-MP Forums Archive
Can someone explain this? (getarg) - 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: Can someone explain this? (getarg) (/showthread.php?tid=601500)



Can someone explain this? (getarg) - sammp - 22.02.2016

Код:
stock SubtractNumber(...)
	{
		new result = 0;
		for(new i = 0; i < numargs(); ++i)
		{
			result = result - getarg(i);

			printf("Arg %d = %d", i,getarg(i));
		}
		return result;
	}
If I input 15, 5 and 4, it outputs -24. So it basically just adds them and inverts it. Why does it do this? (This function is just for testing purposes)


Re: Can someone explain this? (getarg) - Chump - 22.02.2016

(0 - (15 - 5) - 4) = -24.

"result" starts from zero and subtracts from there, which is the reason why you're getting -24.

To start at 15 you could do:

pawn Код:
stock SubtractNumber(start, ...)
{
    new result = start;
    for(new i = 1, l = numargs(); i < l; ++i)
    {
        result = result - getarg(i);

        printf("Arg %d = %d", i,getarg(i));
    }
    return result;
}
pawn Код:
printf("%i", SubtractNumber(15, 5, 4));
Outputs:

pawn Код:
6



Re: Can someone explain this? (getarg) - sammp - 22.02.2016

I'm in idiot haha, didn't think of that.