getarg, setarg and numarg
#1

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 ?
Reply
#2

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 
Reply
#3

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
Reply
#4

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
Reply
#5

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 ?
Reply
#6

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
Reply
#7

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 ?
Reply
#8

It's a function, not a stock.

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

Because "new m=getarg(0);"
Reply
#9

Quote:
Originally Posted by SickAttack
View Post
It's a function, not a stock.
Yes! https://sampforum.blast.hk/showthread.php?tid=570635
Reply
#10

Quote:
Originally Posted by GoldenLion
View Post
It's not a requirement. Anything with stock is left out of the compiled source.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)