PDA

View Full Version : getarg, setarg and numarg


NeXoR
30/09/2016, 09:12 PM
Heya, I have to create a stock that will get the highest number of the entered numbers.
Now I realize why RP Gamemodes have a "not for beginners" note, since I started at a roleplay script without learning the basics.
Anyways, can anyone give me a small explanation of how to do it ?

Kaliber
30/09/2016, 09:23 PM
Just like this:

//You cant name this max..cause this name is already used by samp
stock maxi(...)
{
if(!numargs()) return 0; //To provide server crash!
new m=getarg(0);
for(new i = numargs()-1; i!=0; i--)
{
if(getarg(i) > m) m=getarg(i);
}
return m;
}
//Usage:
printf("%d",maxi(1,3,0,5,3,2,1,-1,4)); //returns 5

NeXoR
30/09/2016, 09:25 PM
Just like this:

//You cant name this max..cause this name is already used by samp
stock maxi(...)
{
if(!numargs()) return 0; //To provide server crash!
new m=getarg(0);
for(new i = numargs()-1; i!=0; i--)
{
if(getarg(i) > m) m=getarg(i);
}
return m;
}
//Usage:
printf("%d",maxi(1,3,0,5,3,2,1,-1,4)); //returns 5
Thank you very much, can you explain getarg() and numarg() in like a sentence or two ? so I can understand what it does

Kaliber
30/09/2016, 09:27 PM
For example you call this maxi(1,2,3)

numargs() will count the arguments you pass..in that case..they are 3..numarg() = 3

maxi() if you would call this, numarg() = 0

so and getarg(0) gets the first argument you pass..in that case...1..so:
getarg(0) = 1
getarg(1) = 2
getarg(2) = 3

aaand thats it :)

NeXoR
30/09/2016, 09:30 PM
For example you call this maxi(1,2,3)

numargs() will count the arguments you pass..in that case..they are 3..numarg() = 3

maxi() if you would call this, numarg() = 0

so and getarg(0) gets the first argument you pass..in that case...1..so:
getarg(0) = 1
getarg(1) = 2
getarg(2) = 3

aaand thats it :)

Thank you very much!
Last question, setarg ?

Kaliber
30/09/2016, 09:34 PM
As the function says...it sets the value of an argument..for example:

stock setter(...)
{
setarg(0,0,999);
}

//Usage:
new x=0;
setter(x);
//and now x=999
The usage:

setarg(arg, index=0, value)

First the argument...in this case 0...cause we only pass 1 argument, and we know its always on the first place so 0.
The index of integer Values is always 0 (only for strings to go through an arrays it can be 0-sizeof(arr))
And last the value you wanna set :)

NeXoR
30/09/2016, 09:37 PM
As the function says...it sets the value of an argument..for example:

stock setter(...)
{
setarg(0,0,999);
}

//Usage:
new x=0;
setter(x);
//and now x=999
The usage:

setarg(arg, index=0, value)

First the argument...in this case 0...cause we only pass 1 argument, and we know its always on the first place so 0.
The index of integer Values is always 0 (only for strings to go through an arrays it can be 0-sizeof(arr))
And last the value you wanna set :)

Thank you very much, would have repped you for the three comments but I'm limited to one :)
Last thing, on this:
for(new i = numargs() - 1; i!=0; i--)
Why -1 on the new i ?

SickAttack
30/09/2016, 10:13 PM
It's a function, not a stock.

--------------------------

Because "new m=getarg(0);"

GoldenLion
30/09/2016, 10:14 PM
It's a function, not a stock.
Yes! http://forum.sa-mp.com/showthread.php?t=570635

SickAttack
30/09/2016, 10:15 PM
Yes! http://forum.sa-mp.com/showthread.php?t=570635

It's not a requirement. Anything with stock is left out of the compiled source.

GoldenLion
30/09/2016, 10:17 PM
It's not a requirement. Anything with stock is left out of the compiled source.
I know, I'm just sharing this thread in every thread where people call functions stocks. :)