08.05.2009, 17:39
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:
Let me explain:
You want for example that users can type ex. a Float, and a number.
Then you need:
But if you want for ex. a Number and a menu:
And for only numbers:
For only Menu's and Textdraw's:
Etc..
So, you put your kind of variables that need to be accepted between the 2 brackets.
But now, the functions.
Their natives:
numargs and getarg
Here's a example command:
Here i used only the functions numarg and getarg.
Explanation:
Like i said, this function only allows the type Menu: to be filled in.
With numargs, You can count the number of arguments, filled in.
ex.
If you use it like this, numargs will return 3, because playerid is counted to!
This is the start of the loop. It's 1 because we don't need the playerid to be processed.
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:
This function, Sets all filled parameters to 15.
ex.
num1, num2, num3 are all 15 now.
Explanation:
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:
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
the second parameter in setarg and getarg is the index of the array ex.
This example will return the total length of all the strings together.
You can use this:
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.
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,_}:...)
{
}
You want for example that users can type ex. a Float, and a number.
Then you need:
pawn Код:
{Float,_}:...
pawn Код:
{Menu,_}:...
pawn Код:
...
pawn Код:
{Menu,Text}:...
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);
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;
}
Explanation:
pawn Код:
stock TestFunction(playerid, {Menu}:...)
pawn Код:
new num = numargs();
ex.
pawn Код:
new Menu:menu01;
new Menu:menu02;
TestFunction(0, menu01, menu02);
pawn Код:
new start = 1;
pawn Код:
new Menu:menu = Menu:getarg(i, 0);
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);
}
}
ex.
pawn Код:
new num1;
new num2;
new num3;
SetTo15(num1, num2, num3);
Explanation:
pawn Код:
setarg(i, 0, 15);
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);
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 Код:
...
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;
}
You can use this:
pawn Код:
TotalLength("Hi!", "xD", "lolZ");
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.