SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 30/09/2016, 09:12 PM   #1
NeXoR
Gangsta
 
Join Date: Jun 2013
Posts: 577
Reputation: 20
Default getarg, setarg and numarg

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 ?
NeXoR is offline   Reply With Quote
Old 30/09/2016, 09:23 PM   #2
Kaliber
High-roller
 
Kaliber's Avatar
 
Join Date: Jun 2012
Location: Localhost
Posts: 1,092
Reputation: 156
Default Re: getarg, setarg and numarg

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 
__________________
Kaliber is offline   Reply With Quote
Old 30/09/2016, 09:25 PM   #3
NeXoR
Gangsta
 
Join Date: Jun 2013
Posts: 577
Reputation: 20
Default Re: getarg, setarg and numarg

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
NeXoR is offline   Reply With Quote
Old 30/09/2016, 09:27 PM   #4
Kaliber
High-roller
 
Kaliber's Avatar
 
Join Date: Jun 2012
Location: Localhost
Posts: 1,092
Reputation: 156
Default Re: getarg, setarg and numarg

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
__________________
Kaliber is offline   Reply With Quote
Old 30/09/2016, 09:30 PM   #5
NeXoR
Gangsta
 
Join Date: Jun 2013
Posts: 577
Reputation: 20
Default Re: getarg, setarg and numarg

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 ?
NeXoR is offline   Reply With Quote
Old 30/09/2016, 09:34 PM   #6
Kaliber
High-roller
 
Kaliber's Avatar
 
Join Date: Jun 2012
Location: Localhost
Posts: 1,092
Reputation: 156
Default Re: getarg, setarg and numarg

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
__________________
Kaliber is offline   Reply With Quote
Old 30/09/2016, 09:37 PM   #7
NeXoR
Gangsta
 
Join Date: Jun 2013
Posts: 577
Reputation: 20
Default Re: getarg, setarg and numarg

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 ?
NeXoR is offline   Reply With Quote
Old 30/09/2016, 10:13 PM   #8
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,761
Reputation: 754
Default Re: getarg, setarg and numarg

It's a function, not a stock.

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

Because "new m=getarg(0);"
SickAttack is offline   Reply With Quote
Old 30/09/2016, 10:14 PM   #9
GoldenLion
Gangsta
 
GoldenLion's Avatar
 
Join Date: Sep 2014
Location: Estonia
Posts: 944
Reputation: 142
Default Re: getarg, setarg and numarg

Quote:
Originally Posted by SickAttack View Post
It's a function, not a stock.
Yes! http://forum.sa-mp.com/showthread.php?t=570635
GoldenLion is offline   Reply With Quote
Old 30/09/2016, 10:15 PM   #10
SickAttack
High-roller
 
Join Date: Dec 2013
Posts: 4,761
Reputation: 754
Default Re: getarg, setarg and numarg

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

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] [TUT] numargs, getarg and setarg [nl]daplayer Tutorials 18 06/07/2018 05:56 PM
Can someone explain this? (getarg) sammp Scripting Help 2 22/02/2016 02:45 PM
getarg - function Kyle Scripting Help 4 19/11/2012 02:21 PM
getarg help armyoftwo Help Archive 4 26/12/2010 01:19 PM


All times are GMT. The time now is 04:43 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.