15.01.2013, 08:12
(
Last edited by RajatPawar; 16/01/2013 at 05:19 AM.
)
Hello guys!
USEFUL LINKS:
Scripting basics.
Scripting functions.
Scripting Callbacks.
Tutorials.
*Scripting callbacks are things which get called after the action specified in the callback is done. For example OnPlayerSpawn will be 'called' when the player spawns. If you insert Kick(playerid) under OnPlayerSpawn, the player would get kicked just as he spawns.*
This is a tutorial which instructs you on how to create custom functions, set timers and PVars.
*MORE PVar examples to be added.*
Custom functions:
Suppose you have a server which you are scripting, and you need to create join and leave messages.
What would you do?
First of all you need to Get the player's name who is joining the server/leaving the server and then use it in a sentence to send messages to other people.
First, you would get names.
Most of the newer people would do this: (assumptions, could be wrong.)
This would store the player's name in the array 'name.'
Mistake 1: You need not use 128 as array size because by default, in sa-mp, 24 is the max player name (in characters) you can use. A player joining a server with a name like 'sagahnjanhjsahjasjhijasujhuasjhuasuhuhah' would be automatically kicked.
Then you would use it in a format sentence.
----------------------------------
format - It is a function that allows you to input CUSTOM DATA in a FIXED sentence.
Example: [not pawn]
If I wanted to send invitations to people.
Hello *insert name here*, come to my party.
Here, if I was using this sentence in pawn, I would do this:
Suppose I put this under OnPlayerConnect. So this message would be sent to every person with the '' %s '' part (which means custom string (specifiers)) replaced by their name. If I connected, it would send me:
"Hello Rajat_Pawar, come to my party."
There are lots of specifiers like %d, %f, etc. %d - int. %f - float.
Would send me '50.'
-----------------------------------
Now you would format the name, each time you create a join message/leave message. Instead, you could create a stock, a function that would do the work for you each time, within a few words, instead of typing new string.....GetPlayerName.. each time!
new - declaring the variable.
GetPlayerName - has 3 parameters, or... compulsory options - playerid, the variable to store his name in and the size of the variable.
return name; Means after this is executed, the name is stored in the 'name' variable, it will RETURN or SEND me the value of name then.
This if defined in the script, you can use it:
It makes life easier. A lot. If you use custom functions or stocks or forwards your life will be made a lot easier.
-------------------------
You can create custom functions in two ways:
forward it and then public functionname(params)...
create it using stocks.
Difference between stocks and publics is:
If you don't use a stock, you don't get a warning.
The proper way of forwarding is:
forward public Function();
*Thanks to Superviper.
-------------------------
Another example of custom function.
You must have seen forward before every custom function is declared. Why?
It tells the compiler that a custom function exists in the script and it is COMING LATER in the script. So that the compiler would give 'unknown' error! (a, b) are variables that we are GOING TO USE in the function. If I use something like 'playerid' in the function, it would give me an error. If I want to use playerid, it would be:
Add(playerid, a , b);
Anyways:
public Add(a, b)
{
return (a+b);
}
So I can use it anywhere:
Add(5,4);
Would give me: 9!
Good eh?
-----------------------------------------------------------------
Timers:
Timers are something that count down from the moment you set a timer and then after the specified amount of time, the code in the timer gets EXECUTED.
Example:
Now:
SetTimer - has 3 parameters.
"cfunction" - The function that is to be executed after the given time period. (here, 5000 - 5 seconds (5000 milliseconds)).
"5000" - Time interval after which function is to be executed. Is in milliseconds. 5000 = 5 seconds.
"0" - Means, will the timer be set every time the function is executed or should it be executed only once? Here by 0, I want to execute once and die.
Then I forwarded the custom function.
So all in all, it would print "Test" in the console after 5 seconds when the player spawns!
If you want to use playerid, in a SetTimer, like kicking player after 5 seconds, use SetTimerEx("Kickplayerafter5secs",5000,0,playerid. ...)
------------------------------
PVars.
They are like player variables. Making life easier.
*PVars should not be used unless absolutely necessary (cross-script communication).*
Thanks to superviper.
Say you want to save a player's location and use it in another function. You could declare global variables, bla bla..but there's a simpler way.
You can now use them anywhere in the script. Awesome, eh?
For example:
Cool eh? Very simple!
I just made this, it's a little something something, I don't expect you pros to like this![Smiley](images/smilies/smile.png)
Thanks.
USEFUL LINKS:
Scripting basics.
Scripting functions.
Scripting Callbacks.
Tutorials.
*Scripting callbacks are things which get called after the action specified in the callback is done. For example OnPlayerSpawn will be 'called' when the player spawns. If you insert Kick(playerid) under OnPlayerSpawn, the player would get kicked just as he spawns.*
This is a tutorial which instructs you on how to create custom functions, set timers and PVars.
*MORE PVar examples to be added.*
Custom functions:
Suppose you have a server which you are scripting, and you need to create join and leave messages.
What would you do?
First of all you need to Get the player's name who is joining the server/leaving the server and then use it in a sentence to send messages to other people.
First, you would get names.
Most of the newer people would do this: (assumptions, could be wrong.)
pawn Code:
new name[128];
GetPlayerName(playerid,name,128);
Mistake 1: You need not use 128 as array size because by default, in sa-mp, 24 is the max player name (in characters) you can use. A player joining a server with a name like 'sagahnjanhjsahjasjhijasujhuasjhuasuhuhah' would be automatically kicked.
Then you would use it in a format sentence.
----------------------------------
format - It is a function that allows you to input CUSTOM DATA in a FIXED sentence.
Example: [not pawn]
If I wanted to send invitations to people.
Hello *insert name here*, come to my party.
Here, if I was using this sentence in pawn, I would do this:
pawn Code:
new string[50]; new name[MAX_PLAYER_NAME] //Max_player_name is just another name for 24.
format(string,sizeof(string),"Hello %s, come to my party.",GetPlayerName(playerid,name,MAX_PLAYER_NAME);
"Hello Rajat_Pawar, come to my party."
There are lots of specifiers like %d, %f, etc. %d - int. %f - float.
pawn Code:
new string[50];
format(string,sizeof(string),"%d",50);
SendClientMessage(playerid, -1, string);
-----------------------------------
Now you would format the name, each time you create a join message/leave message. Instead, you could create a stock, a function that would do the work for you each time, within a few words, instead of typing new string.....GetPlayerName.. each time!
pawn Code:
stock GetName(playerid)
{
new name[24];
GetPlayerName(playerid,name,24);
return name;
}
GetPlayerName - has 3 parameters, or... compulsory options - playerid, the variable to store his name in and the size of the variable.
return name; Means after this is executed, the name is stored in the 'name' variable, it will RETURN or SEND me the value of name then.
This if defined in the script, you can use it:
pawn Code:
format(string, 128, "Hello %s", GetName(playerid));
-------------------------
You can create custom functions in two ways:
forward it and then public functionname(params)...
create it using stocks.
Difference between stocks and publics is:
If you don't use a stock, you don't get a warning.
The proper way of forwarding is:
forward public Function();
*Thanks to Superviper.
-------------------------
Another example of custom function.
pawn Code:
forward Add(a, b);
It tells the compiler that a custom function exists in the script and it is COMING LATER in the script. So that the compiler would give 'unknown' error! (a, b) are variables that we are GOING TO USE in the function. If I use something like 'playerid' in the function, it would give me an error. If I want to use playerid, it would be:
Add(playerid, a , b);
Anyways:
public Add(a, b)
{
return (a+b);
}
So I can use it anywhere:
Add(5,4);
Would give me: 9!
Good eh?
-----------------------------------------------------------------
Timers:
Timers are something that count down from the moment you set a timer and then after the specified amount of time, the code in the timer gets EXECUTED.
Example:
pawn Code:
public OnPlayerSpawn(playerid)
{
SetTimer("cfunction",5000,0);
return 1;
}
forward cfunction();
public cfunction()
{
print("Test.");
return 1;
}
SetTimer - has 3 parameters.
"cfunction" - The function that is to be executed after the given time period. (here, 5000 - 5 seconds (5000 milliseconds)).
"5000" - Time interval after which function is to be executed. Is in milliseconds. 5000 = 5 seconds.
"0" - Means, will the timer be set every time the function is executed or should it be executed only once? Here by 0, I want to execute once and die.
Then I forwarded the custom function.
So all in all, it would print "Test" in the console after 5 seconds when the player spawns!
If you want to use playerid, in a SetTimer, like kicking player after 5 seconds, use SetTimerEx("Kickplayerafter5secs",5000,0,playerid. ...)
------------------------------
PVars.
They are like player variables. Making life easier.
*PVars should not be used unless absolutely necessary (cross-script communication).*
Thanks to superviper.
Say you want to save a player's location and use it in another function. You could declare global variables, bla bla..but there's a simpler way.
pawn Code:
new Float:x, Float:y, Float:z; //declares new floats x, y, z.
GetPlayerPos(playerid,x,y,z); Stores players X, Y, Z positions in x, y, z.
SetPVarFloat(playerid,"PlayerXposition",x); Set's Player variable "PlayerXposition" with value of x.
SetPVarFloat(playerid,"PlayerYposition",y); Set's Player variable "PlayerYposition" with value of y.
SetPVarFloat(playerid,"PlayerZposition",z); Set's Player variable "PlayerZposition" with value of z.
For example:
pawn Code:
SetPlayerPos(playerid, GetPVarFloat(playerid,"PlayerXposition") ..// and so on...
I just made this, it's a little something something, I don't expect you pros to like this
![Smiley](images/smilies/smile.png)
Thanks.