[Tutorial] [TUT] numargs, getarg and setarg
#1

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:
pawn Код:
stock FunctionName(playerid, {Float,_}:...)
{

}
Let me explain:
You want for example that users can type ex. a Float, and a number.
Then you need:
pawn Код:
{Float,_}:...
But if you want for ex. a Number and a menu:
pawn Код:
{Menu,_}:...
And for only numbers:
pawn Код:
...
For only Menu's and Textdraw's:
pawn Код:
{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:
pawn Код:
native numargs();
native getarg(arg, index=0);
native setarg(arg, index=0, value);
numargs and getarg
Here's a example command:
pawn Код:
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:
pawn Код:
stock TestFunction(playerid, {Menu}:...)
Like i said, this function only allows the type Menu: to be filled in.
pawn Код:
new num = numargs();
With numargs, You can count the number of arguments, filled in.
ex.
pawn Код:
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!
pawn Код:
new start = 1;
This is the start of the loop. It's 1 because we don't need the playerid to be processed.
pawn Код:
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:
pawn Код:
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.
pawn Код:
new num1;
new num2;
new num3;
SetTo15(num1, num2, num3);
num1, num2, num3 are all 15 now.
Explanation:
pawn Код:
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:
pawn Код:
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
pawn Код:
...
the second parameter in setarg and getarg is the index of the array ex.
pawn Код:
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:
pawn Код:
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 ******.
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.
Reply


Messages In This Thread
[TUT] numargs, getarg and setarg - by [nl]daplayer - 08.05.2009, 17:39
Re: [TUT] numargs, getarg and setarg - by Dark_Kostas - 08.05.2009, 17:41
Re: [TUT] numargs, getarg and setarg - by [nl]daplayer - 08.05.2009, 17:56
Re: [TUT] numargs, getarg and setarg - by Dark_Kostas - 08.05.2009, 18:01
Re: [TUT] numargs, getarg and setarg - by [nl]daplayer - 08.05.2009, 18:04
Re: [TUT] numargs, getarg and setarg - by Dark_Kostas - 08.05.2009, 18:07
Re: [TUT] numargs, getarg and setarg - by yom - 08.05.2009, 18:27
Re: [TUT] numargs, getarg and setarg - by Weirdosport - 08.05.2009, 18:51
Re: [TUT] numargs, getarg and setarg - by [nl]daplayer - 08.05.2009, 19:45
Re: [TUT] numargs, getarg and setarg - by Marciii - 28.05.2009, 16:58
Re: [TUT] numargs, getarg and setarg - by miokie - 28.05.2009, 17:25
Re: [TUT] numargs, getarg and setarg - by Nero_3D - 28.05.2009, 18:21
Re: [TUT] numargs, getarg and setarg - by Google63 - 12.10.2009, 18:22
Re: [TUT] numargs, getarg and setarg - by chhris - 27.01.2010, 16:16
Re: [TUT] numargs, getarg and setarg - by Cry_Wolf - 27.01.2010, 17:01
Re: [TUT] numargs, getarg and setarg - by Westie - 27.01.2010, 17:04
Re: [TUT] numargs, getarg and setarg - by [nl]daplayer - 28.01.2010, 13:20
Re: [TUT] numargs, getarg and setarg - by chhris - 28.01.2010, 13:48
Re: [TUT] numargs, getarg and setarg - by Xaparrito - 06.07.2018, 17:56

Forum Jump:


Users browsing this thread: 1 Guest(s)