30.06.2011, 22:55
(
Last edited by krogsgaard20; 09/01/2012 at 02:27 PM.
)
Contents
In this tutorial you will learn how to format strings in the best optimized way.
I wrote this tutorial because I have seen a lot of people doing this in a lot of horrible ways.
I will now show you how to make a simple message when a player joins the server, showing the player's name.
This is just used as an example on how to format strings. Strings are some of the most common code bits in pawn scripting, and can be used in a lot of different ways. The purpose with this tutorial is not to learn you how to make a join message, but how to format strings correctly and as optimized as possible.
- Now let's get started! We will use the "OnPlayerConnect" callback.
Declaration
We will start by declaring 2 variables.
The variable called "name[MAX_PLAYER_NAME]" will hold the name of the player. The variable has an array (the "[MAX_PLAYER_NAME]"). MAX_PLAYER_NAME is the same as 24, which is the max length of player names in SA-MP.
The variable "string[23 + MAX_PLAYER_NAME]" will hold the message, that will be sent when a player joins. It has the array "[23 + MAX_PLAYER_NAME]", which is the same as 47 (23+24) <-- Explaination later
Getting the players name
Now we want to tell the script to get the name of the player who just connected.
The "GetPlayerName" function is just perfect for this part.
We just told the script to get the player's name, and store it in the variable called "name" which we declared above.
The "sizeof(name)" is, as the word almost says itself - it gets the size of the array in the variable called "name" which is MAX_PLAYER_NAME (24). This will represent the length of the playername.
We don't have to use the "sizeof" function, we could also do like this:
but i prefer using "sizeof".
We now have the player's name stored in the variable "name".
Formatting the message
Ok, now we have to format the message that will be sent when a player connects to the server.
This line will tell the script to format and store the message in the wariable "string" which we declared above.
I already explained what the "sizeof" function does - tells the length of the message is the same as the array size of the variable "string" which is 23 + MAX_PLAYER_NAME (23+24=47).
The reason why the array size is 23+24 is that "%s has joined the server" contains 22 characters including spaces, but excluding "%s". When formatting, there is something called "the null terminator" which means messages have the character '\0' (NULL) at the end. Thats why we need the array size to be bigger than the actual message (23 in stead of 22 - one for the null terminator).
The "%s" is a placeholder for the playername, which has the length of MAX_PLAYER_NAME. thats why we add "+ MAX_PLAYER_NAME" in the array, so the message has a limit of 47 characters when showed in-game, and when playernames can't be longer than 24 characters, the message will never need more than 47 characters.
In the end we are adding ", name" (which is the variable containing the playername).
This tells the script that the "%s" is a placeholder for the playername.
The last thing we need to do now is sending the message to all connected players.
This line will send a client message to all players with our formatted message, stored in the variable "string".
The message will now look like this in-game when a player joins the server:
(If the playername is krogsgaard20, of course)
Other placeholders
The reason that we used "%s" as placeholder, is because our message is a string. A string is a special type of array, one which is used to hold multiple characters to create a word or sentence or other human readable text.
Here is a list of all placeholders:
String manipulation
(Thanks to Schurman)
There are many functions to manipulate strings, and also format them. Here's a list:
Final result
Here is the final result of the script that we created in this tutorial:
References
https://sampwiki.blast.hk/wiki/Format
Conclusion
Remember to always make array sizes as small as possible.
An array is memory that can be accessed dynamically, and unused arrays will cause CPU or RAM lagg on the server.
What i am trying with this tutorial is getting rid of those horrible scripts with array sizes way too big.
A lot of people are using 256 in all their arrays, which makes no sense, because the maximum length of client messages is 128, but do not use 128 if the message you want to format is shorter.
- Introduction
- Declaration
- Getting the players name
- Formatting the message
- Other placeholders
- String manipulation
- Final result
- References
- Conclusion
In this tutorial you will learn how to format strings in the best optimized way.
I wrote this tutorial because I have seen a lot of people doing this in a lot of horrible ways.
I will now show you how to make a simple message when a player joins the server, showing the player's name.
This is just used as an example on how to format strings. Strings are some of the most common code bits in pawn scripting, and can be used in a lot of different ways. The purpose with this tutorial is not to learn you how to make a join message, but how to format strings correctly and as optimized as possible.
- Now let's get started! We will use the "OnPlayerConnect" callback.
Declaration
We will start by declaring 2 variables.
pawn Code:
new name[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME];
The variable "string[23 + MAX_PLAYER_NAME]" will hold the message, that will be sent when a player joins. It has the array "[23 + MAX_PLAYER_NAME]", which is the same as 47 (23+24) <-- Explaination later
Getting the players name
Now we want to tell the script to get the name of the player who just connected.
The "GetPlayerName" function is just perfect for this part.
pawn Code:
GetPlayerName(playerid, name, sizeof(name));
The "sizeof(name)" is, as the word almost says itself - it gets the size of the array in the variable called "name" which is MAX_PLAYER_NAME (24). This will represent the length of the playername.
We don't have to use the "sizeof" function, we could also do like this:
pawn Code:
GetPlayerName(playerid, name, 24);
We now have the player's name stored in the variable "name".
Formatting the message
Ok, now we have to format the message that will be sent when a player connects to the server.
pawn Code:
format(string, sizeof(string), "%s has joined the server", name);
I already explained what the "sizeof" function does - tells the length of the message is the same as the array size of the variable "string" which is 23 + MAX_PLAYER_NAME (23+24=47).
The reason why the array size is 23+24 is that "%s has joined the server" contains 22 characters including spaces, but excluding "%s". When formatting, there is something called "the null terminator" which means messages have the character '\0' (NULL) at the end. Thats why we need the array size to be bigger than the actual message (23 in stead of 22 - one for the null terminator).
The "%s" is a placeholder for the playername, which has the length of MAX_PLAYER_NAME. thats why we add "+ MAX_PLAYER_NAME" in the array, so the message has a limit of 47 characters when showed in-game, and when playernames can't be longer than 24 characters, the message will never need more than 47 characters.
In the end we are adding ", name" (which is the variable containing the playername).
This tells the script that the "%s" is a placeholder for the playername.
The last thing we need to do now is sending the message to all connected players.
pawn Code:
SendClientMessageToAll(0xFFFF00FF, string);
The message will now look like this in-game when a player joins the server:
Code:
krogsgaard20 has joined the server
Other placeholders
The reason that we used "%s" as placeholder, is because our message is a string. A string is a special type of array, one which is used to hold multiple characters to create a word or sentence or other human readable text.
Here is a list of all placeholders:
Quote:
%b Inserts a number at this position in binary radix %c Inserts a single character. %d Inserts an integer (whole) number %f Inserts a floating point number. %i Inserts an integer. %s Inserts a string. %x Inserts a number in hexadecimal notation. %% Inserts the literal '%' |
(Thanks to Schurman)
There are many functions to manipulate strings, and also format them. Here's a list:
pawn Code:
bool: ispacked(const string[])
memcpy(dest[], const source[], index=0, numbytes, maxlength=sizeof dest)
strcat(dest[], const source[], maxlength=sizeof dest)
strcmp(const string1[], const string2[],bool: ignorecase=false, length=cellmax)
strcopy(dest[], const source[], maxlength=sizeof dest)
bool: strdel(string[], start, end)
bool: strequal(const string1[], const string2[],bool: ignorecase=false,length=cellmax)
strfind(const string[], const sub[],bool: ignorecase=false, index=0)
strformat(dest[], size=sizeof dest, bool: pack=false, const format[], . . . )
bool: strins(string[], const substr[], index,maxlength=sizeof string)
strlen(const string[])
strmid(dest[], const source[],start=0, end=cellmax,maxlength=sizeof dest)
strpack(dest[], const source[],maxlength=sizeof dest)
strunpack(dest[], const source[],maxlength=sizeof dest)
strval(const string[], index=0)
uudecode(dest[], const source[], maxlength=sizeof dest)
uuencode(dest[], const source[], numbytes, maxlength=sizeof dest)
valstr(dest[], value, bool: pack=false)
Here is the final result of the script that we created in this tutorial:
pawn Code:
public OnPlayerConnect(playerid)
{
new name[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof(name));
format(string, sizeof(string), "%s has joined the server", name);
SendClientMessageToAll(0xFFFF00FF, string);
return 1;
}
https://sampwiki.blast.hk/wiki/Format
Conclusion
Remember to always make array sizes as small as possible.
An array is memory that can be accessed dynamically, and unused arrays will cause CPU or RAM lagg on the server.
What i am trying with this tutorial is getting rid of those horrible scripts with array sizes way too big.
A lot of people are using 256 in all their arrays, which makes no sense, because the maximum length of client messages is 128, but do not use 128 if the message you want to format is shorter.