SA-MP Forums Archive
getarg, setarg and numarg - 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: getarg, setarg and numarg (/showthread.php?tid=618088)



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 
numargs()-1i!=0i--)
    {
        if(
getarg(i) > mm=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
View Post
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 
numargs()-1i!=0i--)
    {
        if(
getarg(i) > mm=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
View Post
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(argindex=0value
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
View Post
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(argindex=0value
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 numargs() - 1i!=0i--) 
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
View Post
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
View Post
It's not a requirement. Anything with stock is left out of the compiled source.