[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
#2

Nice guide! Ill start using it.
Reply
#3

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

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

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

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

Those are functions of the Pawn language, not of SA:MP..
Reply
#8

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

Why:
pawn Код:
new start = 1;
    for(new i=start; i<num; i++)
Why not:
pawn Код:
for(new i=1; i<num; i++)
Reply
#9

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

Why:
pawn Код:
new start = 1;
    for(new i=start; i<num; i++)
Why not:
pawn Код:
for(new i=1; i<num; i++)
so i can explain it easaly, You need core include so actualy it needs a_samp
Reply
#10

REALLY BIG THANX!!!!
Reply
#11

Nice job!
Thanks.
Reply
#12

Good job but to find out about that you just need ****** and the search button of this forum...
Reply
#13

Actually it isn't really infinite, it is limited by allocated memory :P
Reply
#14

Hi guys
[font=Comic Sans MS][b]
I need help with this.
It is very good tutorial but i have small problem and i need resolved this problem.
I hope that you will help me.

Ok
My experience with the stocks are small.
I create new Language System. And I need it to complete.

This is my stock.
Code:
stock SendLanguageMessage(playerid, color, text[], ...)
{
 	new file[256];
	new translation[256];
	new string[256];

	format(file,sizeof(file),"Studio/Languages/%s.txt", Info[playerid][Translation]);


	translation = dini_Get(file,text);

 	format(string,sizeof(string), translation,...);

	SendClientMessage(playerid, color, string);

	return 1;
}
[font=Comic Sans MS][b]
This essentially finds the text of the folder, depending on what language you have chosen. This text is sent with it but at the end that gives infinite parameters.

This is so to shorten this
Code:
new string[256];
format(string, sizeof(string), "Welcome back in ...");
SendClientMessage(playerid, color, string);

I do not know exactly what they should be used.

Please help.
Reply
#15

Thanks man, very usefull tutorial.
I will read this soon :P.
Reply
#16

As far as I know, there's a limit of 128 arguments that can be passed to a function.
Reply
#17

Quote:
Originally Posted by chhris
Hi guys
........
Please help.
Did you actually read this tutorial?, you can't use ... in formats.
If you have read this tutorial you would have known that you must use getarg to get param arguments.

Quote:
Originally Posted by /^We(stie|z+[e|a
r)$/ ]
As far as I know, there's a limit of 128 arguments that can be passed to a function.
Ok, added in tutorial...

Thx.
Reply
#18

I see you're right. But he knew would help me
Reply
#19

stock TestFunction(playerid, {Menu}:...) this one makes, just to set a player online in the server, or am I wrong?. I ґm just reviewing to see the attachments to the scripts of setarg and getarg.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)