getarg, setarg and numarg -
NeXoR - 30.09.2016
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 ?
Re: getarg, setarg and numarg -
Kaliber - 30.09.2016
Just like this:
PHP Code:
//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
Re: getarg, setarg and numarg -
NeXoR - 30.09.2016
Quote:
Originally Posted by Kaliber
Just like this:
PHP Code:
//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
Re: getarg, setarg and numarg -
Kaliber - 30.09.2016
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
Re: getarg, setarg and numarg -
NeXoR - 30.09.2016
Quote:
Originally Posted by Kaliber
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 ?
Re: getarg, setarg and numarg -
Kaliber - 30.09.2016
As the function says...it sets the value of an argument..for example:
PHP Code:
stock setter(...)
{
setarg(0,0,999);
}
//Usage:
new x=0;
setter(x);
//and now x=999
The usage:
PHP Code:
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
Re: getarg, setarg and numarg -
NeXoR - 30.09.2016
Quote:
Originally Posted by Kaliber
As the function says...it sets the value of an argument..for example:
PHP Code:
stock setter(...)
{
setarg(0,0,999);
}
//Usage:
new x=0;
setter(x);
//and now x=999
The usage:
PHP Code:
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:
PHP Code:
for(new i = numargs() - 1; i!=0; i--)
Why -1 on the new i ?
Re: getarg, setarg and numarg -
SickAttack - 30.09.2016
It's a function, not a stock.
--------------------------
Because "new m=getarg(0);"
Re: getarg, setarg and numarg -
GoldenLion - 30.09.2016
Quote:
Originally Posted by SickAttack
It's a function, not a stock.
|
Yes!
https://sampforum.blast.hk/showthread.php?tid=570635
Re: getarg, setarg and numarg -
SickAttack - 30.09.2016
Quote:
Originally Posted by GoldenLion
|
It's not a requirement. Anything with stock is left out of the compiled source.