SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 08/05/2009, 05:39 PM   #1
[nl]daplayer
Huge Clucker
 
Join Date: Dec 2007
Posts: 291
Reputation: 25
Default [TUT] numargs, getarg and setarg

numargs, setarg and getarg
setarg and getarg are functions default in sa-mp.
With this functions you can create a custom function with infinite parameters.
To create a function like this, you need to use this as a parameter:
Code:
stock FunctionName(playerid, {Float,_}:...)
{

}
Let me explain:
You want for example that users can type ex. a Float, and a number.
Then you need:
Code:
{Float,_}:...
But if you want for ex. a Number and a menu:
Code:
{Menu,_}:...
And for only numbers:
Code:
...
For only Menu's and Textdraw's:
Code:
{Menu,Text}:...
Etc..
So, you put your kind of variables that need to be accepted between the 2 brackets.
But now, the functions.
Their natives:
Code:
native numargs();
native getarg(arg, index=0);
native setarg(arg, index=0, value);

numargs and getarg
Here's a example command:
Code:
stock TestFunction(playerid, {Menu}:...)
{
	new num = numargs();
	new start = 1;
	for(new i=start; i<num; i++)
	{
	  new Menu:menu = Menu:getarg(i, 0);
	  AddMenuItem(menu, 0, "Testing");
	}
	SendClientMessage(playerid, 0xFF0000FF, "Menu Items added");
	return 1;
}
Here i used only the functions numarg and getarg.
Explanation:
Code:
stock TestFunction(playerid, {Menu}:...)
Like i said, this function only allows the type Menu: to be filled in.
Code:
new num = numargs();
With numargs, You can count the number of arguments, filled in.
ex.
Code:
new Menu:menu01;
new Menu:menu02;
TestFunction(0, menu01, menu02);
If you use it like this, numargs will return 3, because playerid is counted to!
Code:
new start = 1;
This is the start of the loop. It's 1 because we don't need the playerid to be processed.
Code:
new Menu:menu = Menu:getarg(i, 0);
This will create a new menu variable, we will put here the processed menu, i = the argument needed to be processed, and that zero there is later explained for using this for arrays and strings.

setarg
setarg is used to set a argument. Example function:
Code:
stock SetTo15(...)
{
	new num = numargs();
	new start = 0;
	for(new i=start; i<num; i++)
	{
	  setarg(i, 0, 15);
	}
}
This function, Sets all filled parameters to 15.
ex.
Code:
new num1;
new num2;
new num3;
SetTo15(num1, num2, num3);
num1, num2, num3 are all 15 now.
Explanation:
Code:
setarg(i, 0, 15);
i = the loop variable.
0 = Later used for arrays and stings
15 = the value to set.

Use setarg and getarg with arrays and strings
String is the same as an array actually, because every item of the string will be set to a number.
that number will be converted to a char by the system.
ex:
Code:
new array[5];
array[0] = 103;
array[1] = 111;
array[2] = 111;
array[3] = 100;
array[4] = 0;
print(array);
This will become "good".
Watch the fourth index of the array. It's set to 0 because every string needs to end with that.
It's kind a string ended symbol.
An array is an group of integers together.
So you can use
Code:
...
the second parameter in setarg and getarg is the index of the array ex.
Code:
stock TotalLength(...)
{
  new len = 0;
  new num = numargs();
  for(new i=0; i<num; i++)
  {
    new index = 0;
    while(!false)
    {
      new chari = getarg(i, index++);
      if (chari == 0) break; // 0 is end of string
      len++;
    }
  }
  return len;
}
This example will return the total length of all the strings together.
You can use this:
Code:
TotalLength("Hi!", "xD", "lolZ");
This will return 9 because the first string has a length of 3 chars, the second got 2 characters and the last got 4 characters, all these together will become 9.

End of the tutorial
Try to experiment with these 3 functions, use this as a guide (do not take over the examples, but learn from them)

Why i wrote this
Because i searched almost 3 months a tutorial how to use these functions, i finally found it out to looking in the sscanf code created by y_less.
And i wanted to help others.

Help to make this tutorial better
Is there something that i didn't mention, or do i explained something wrong. I am open for suggestions.
[nl]daplayer is offline   Reply With Quote
Old 08/05/2009, 05:41 PM   #2
Dark_Kostas
Gangsta
 
Join Date: Dec 2007
Location: Greece, Athens
Posts: 763
Reputation: 14
Default Re: [TUT] numargs, getarg and setarg

Nice guide! Ill start using it.
__________________
Quote:
Help != Scripting for you.

Give a man a fish he eats for a day, teach a man to fish he eats for a life time.
Dark_Kostas is offline   Reply With Quote
Old 08/05/2009, 05:56 PM   #3
[nl]daplayer
Huge Clucker
 
Join Date: Dec 2007
Posts: 291
Reputation: 25
Default Re: [TUT] numargs, getarg and setarg

Quote:
Originally Posted by [DK
AzaxYo ]
Nice guide! Ill start using it.
Thx, if you find any typo's or things that doesn't work, please post it here. so i can fix it :P
[nl]daplayer is offline   Reply With Quote
Old 08/05/2009, 06:01 PM   #4
Dark_Kostas
Gangsta
 
Join Date: Dec 2007
Location: Greece, Athens
Posts: 763
Reputation: 14
Default Re: [TUT] numargs, getarg and setarg

Quote:
Originally Posted by .:NoZer0:.
Quote:
Originally Posted by [DK
AzaxYo ]
Nice guide! Ill start using it.
Thx, if you find any typo's or things that doesn't work, please post it here. so i can fix it :P
haha here is one small type

Like i sad, this function only allows the type Menu: to be filled in.
__________________
Quote:
Help != Scripting for you.

Give a man a fish he eats for a day, teach a man to fish he eats for a life time.
Dark_Kostas is offline   Reply With Quote
Old 08/05/2009, 06:04 PM   #5
[nl]daplayer
Huge Clucker
 
Join Date: Dec 2007
Posts: 291
Reputation: 25
Default Re: [TUT] numargs, getarg and setarg

Quote:
Originally Posted by [DK
AzaxYo ]
Quote:
Originally Posted by .:NoZer0:.
Quote:
Originally Posted by [DK
AzaxYo ]
Nice guide! Ill start using it.
Thx, if you find any typo's or things that doesn't work, please post it here. so i can fix it :P
haha here is one small type

Like i sad, this function only allows the type Menu: to be filled in.
Fixed!, Thx for reporting.
[nl]daplayer is offline   Reply With Quote
Old 08/05/2009, 06:07 PM   #6
Dark_Kostas
Gangsta
 
Join Date: Dec 2007
Location: Greece, Athens
Posts: 763
Reputation: 14
Default Re: [TUT] numargs, getarg and setarg

Also all proccesed are with one "c" and two "s"

This is the start of the loop. It's 1 because we don't need the playerid to be proccesed. <--- processed
__________________
Quote:
Help != Scripting for you.

Give a man a fish he eats for a day, teach a man to fish he eats for a life time.
Dark_Kostas is offline   Reply With Quote
Old 08/05/2009, 06:27 PM   #7
yom
High-roller
 
Join Date: Jul 2006
Posts: 2,005
Reputation: 6
Default Re: [TUT] numargs, getarg and setarg

Those are functions of the Pawn language, not of SA:MP..
yom is offline   Reply With Quote
Old 08/05/2009, 06:51 PM   #8
Weirdosport
High-roller
 
Join Date: Jul 2008
Posts: 1,304
Reputation: 0
Default Re: [TUT] numargs, getarg and setarg

Yeah, hence they don't need the <a_samp> (I think).

Why:
Code:
	new start = 1;
	for(new i=start; i<num; i++)

Why not:
Code:
	for(new i=1; i<num; i++)
Weirdosport is offline   Reply With Quote
Old 08/05/2009, 07:45 PM   #9
[nl]daplayer
Huge Clucker
 
Join Date: Dec 2007
Posts: 291
Reputation: 25
Default Re: [TUT] numargs, getarg and setarg

Quote:
Originally Posted by Weirdosport
Yeah, hence they don't need the <a_samp> (I think).

Why:
Code:
	new start = 1;
	for(new i=start; i<num; i++)

Why not:
Code:
	for(new i=1; i<num; i++)
so i can explain it easaly, You need core include so actualy it needs a_samp
[nl]daplayer is offline   Reply With Quote
Old 28/05/2009, 04:58 PM   #10
Marciii
Little Clucker
 
Join Date: May 2009
Posts: 13
Reputation: 0
Default Re: [TUT] numargs, getarg and setarg

REALLY BIG THANX!!!!
Marciii 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
getarg with OnTimerEx bug? Bpb Help Archive 2 08/10/2009 12:05 PM


All times are GMT. The time now is 06:22 PM.


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