23.12.2013, 05:37
(
Last edited by Swedky; 23/12/2013 at 11:47 AM.
)
Well, these are some forms that I know to optimize our GM, and clearly, to improve the server.
We will begin with the inconveniences that the Users have:
If we use Timer's for restrictions, I refer that a user could not use a command for 'X' time, a Timer is the last thing that they should use. One of the best methods is to use the include created by so called DesingMyCry: a_tiempos.inc - he handles your times to whim. It is %100 effective and it will help us very much to remove unnecessary Timer's since it uses the SA-MP's native ones. ('Gettime').
Since the tutorial is in Spanish and his codes also, I it will translate to the Englishman in order that they deal:
Good, I will leave an example them of since using it:
StarTimer: It begins the restriction.
ObtainTimer: It obtains the quantity of time that is absent in order that it finishes the timer.
TimerHasHappened: It detects if the Timer already happened.
It is strange that a GM does not use gratings and/or inner doors in movement. Mainly, to close the grating they use a Timer, but there is another method: https://sampwiki.blast.hk/wiki/OnObjectMoved - It is called when an object stops moving.
Probably some persons format many cells unnecessarily, I refer that they put more than it will be formatted.
Example:
THIS IS NOT RECOMMENDED:
BUT YES THIS:
What did I do there? It was to lower the cells and to use only the necessary ones.
Note: Bear in mind that if they format the name of the player, they must add '+24' cells to the array.
Note 2: The Hexadecimal colors also will store '+8' cells. (Writing the entire hexadecimal).
Note 3: Remember '+1' use cell for the void character.
What I want to say, is that we must use static only global type, and new only local type, and they will wonder: why?. Simply because when the server initiates, in static the variable/array is created only once until you close the server, besides whom this one spends fewer resources and there is no need to global type be creating it again and again like in case of new.
And we use new locally, since on having closed the keys it resigns of the memory.
Example:
Some of the examples that it could give, it is of that the persons use often the conditional ones, instead of using switch.
For example:
INCORRECT:
CORRECT:
It would return the value 'Random' and our code would be more optimized, besides that switch always reads directly the codes that it uses in of his keys, whereas if verifies one for one.
return can return binary, entire numbers/decimals, chains of text, etc.
Another example is:
It notices: Some stock's sometimes must not return at least that we put a value. (Also the callback's that we create).
Y_Commands - https://sampforum.blast.hk/showthread.php?tid=169029.
ZCMD - https://sampforum.blast.hk/showthread.php?tid=91354.
Some of them use 500 cells for which they use the array with the constant 'MAX_PLAYERS', being that our server not reaches not even 50/70 users.
We must use the necessary, at the time quantity, what we must do? To go below of the included bookshop 'a_samp' and to re-define the above mentioned constant; therefore, it would be like that:
When we use a curl for all the players, the best option is to use 'foreach' for ******. What this include does is to create a curl only for the connected players, and is %100 exactly.
I will leave the link them of of the same one: [Include] foreach - 0.3 compatible - updated 19/12/12.
_____________________
Well, it was the whole tutorial.
I am new doing tutoriales, if they have some doubt or I was wrong in something, please, warn me.
Excuse the evil Englishman, I am of the Spanish section and do not remove very well with the Englishman that we say.
Regards.
We will begin with the inconveniences that the Users have:
Timer's:
Something that always the Ping raises in our server. I will give them forms of how solving it.Timer for restrictions.
If we use Timer's for restrictions, I refer that a user could not use a command for 'X' time, a Timer is the last thing that they should use. One of the best methods is to use the include created by so called DesingMyCry: a_tiempos.inc - he handles your times to whim. It is %100 effective and it will help us very much to remove unnecessary Timer's since it uses the SA-MP's native ones. ('Gettime').
Since the tutorial is in Spanish and his codes also, I it will translate to the Englishman in order that they deal:
pawn Code:
#define SecondsToMinutes(%1) ((%1)*(60))
#define SecondToHour(%1) (((%1)*(60))*(60))
#define SecondToDay(%1) ((((%1)*(24))*(60))*(60))
stock Timer:operator = (t) return Timer:t;
stock StartTimer(&Timer:timer, const TimerTime)
{
timer = ((gettime())+(TimerTime));
return 1;
}
stock ObtainTimer(Timer:timer)
{
new CurrentTime = gettime();
return (_:timer-CurrentTime <= 0) ? (0) : (_:timer-CurrentTime);
}
stock bool:TimerHasHappened(Timer:timer)
{
return (ObtainTimer(_:timer) <= 0) ? (true) : (false);
}
pawn Code:
#include <a_samp>
#include <a_timers> // Here the name of the include.
new Timer:RestrictionCMD[MAX_PLAYERS];
public OnPlayerCommandText(playerid, cmdtext[])
{
//if(TimerHasHappened(RestrictionCMD[playerid]) == true)
if(TimerHasHappened(RestrictionCMD[playerid]))
{
StartTime(RestrictionCMD[playerid], SecondsToMinute(5));
return SendClientMessage(playerid, -1, "It used the command correctly. Now it waits 5 minutes to return to use any other one.");
}
else
{
new string[40];
format(strimg, sizeof(string), "Wait %i seconds before using a command.", ObtainTimer(RestrictionCMD[playerid]));
SendClientMessage(playerid, -1, string);
}
return 0;
}
ObtainTimer: It obtains the quantity of time that is absent in order that it finishes the timer.
TimerHasHappened: It detects if the Timer already happened.
Gratings.
It is strange that a GM does not use gratings and/or inner doors in movement. Mainly, to close the grating they use a Timer, but there is another method: https://sampwiki.blast.hk/wiki/OnObjectMoved - It is called when an object stops moving.
Cells.
Probably some persons format many cells unnecessarily, I refer that they put more than it will be formatted.
Example:
THIS IS NOT RECOMMENDED:
pawn Code:
new string[144];
format(string, sizeof(string), "* Your principal information: %s[%d].", GetName(playerid), playerid);
SendClientMessage(playerid, -1, string);
BUT YES THIS:
pawn Code:
new string[61]; /* *Thanks to Bark for the notice.* */
format(string, sizeof(string), "* Your principal information: %s[%d].", GetName(playerid), playerid);
SendClientMessage(playerid, -1, string);
What did I do there? It was to lower the cells and to use only the necessary ones.
Note: Bear in mind that if they format the name of the player, they must add '+24' cells to the array.
Note 2: The Hexadecimal colors also will store '+8' cells. (Writing the entire hexadecimal).
Note 3: Remember '+1' use cell for the void character.
When to use static and when to use new?.
What I want to say, is that we must use static only global type, and new only local type, and they will wonder: why?. Simply because when the server initiates, in static the variable/array is created only once until you close the server, besides whom this one spends fewer resources and there is no need to global type be creating it again and again like in case of new.
And we use new locally, since on having closed the keys it resigns of the memory.
Example:
pawn Code:
static Team[MAX_PLAYERS]; // We declare that we will use this array in the whole GM.
public OnPlayerSpawn(playerid)
{
new string[70]; // We declare that this array will be used in the whole callack.
format(string, sizeof(string), "* %s[%d] It appeared and it is ready to play.", GetName(playerid), playerid);
SendClientMessageToAll(0xF10000FF, string);
if(Team[playerid] > 0)
{
new string2[39]; // We declare that we will use this array only in conditional this one.
format(string2, sizeof(string2), "* You are of the Equipment number: %i.", Team[playerid]);
SendClientMessageToAll(0xF10000FF, string2);
} // We close the keys and erase the array 'string2' of the memory.
return 1;
} // We close the keys of the callback and erase the array 'string' of the memory.
To optimize codes.
Some of the examples that it could give, it is of that the persons use often the conditional ones, instead of using switch.
For example:
INCORRECT:
pawn Code:
stock ProvedAtRandom()
{
new Random = random(3);
if(random == 0)
{
SendRconCommand("gmx");
}
else if(random == 1)
{
SendRconCommand("pasword Tutorial.");
}
else if(random == 2)
{
SendRconCommand("reloadfs Tutorial");
}
}
pawn Code:
stock ProvedAtRandom()
{
new Random = random(3);
switch(Random)
{
case 0: SendRconCommand("gmx");
case 1: SendRconCommand("password Tutorial.");
case 2: SendRconCommand("reloadfs Tutorial");
}
return Random;
}
return can return binary, entire numbers/decimals, chains of text, etc.
Another example is:
pawn Code:
stock GetName(playerid) // Code used in previous examples in this tutorial.
{
new Nick[MAX_PLAYER_NAME];
GetPlayerName(playerid, Nick, sizeof(Nick));
return Nick; // He returns a chain of text.
}
Processors of Commands.
Always commands are used in our Server, and a way of optimizing our GM is to use a processor of Commands, more acquaintances it is ZCMD for Zeex and Y_Commands for ******.Y_Commands - https://sampforum.blast.hk/showthread.php?tid=169029.
ZCMD - https://sampforum.blast.hk/showthread.php?tid=91354.
Others.
Some of them use 500 cells for which they use the array with the constant 'MAX_PLAYERS', being that our server not reaches not even 50/70 users.
We must use the necessary, at the time quantity, what we must do? To go below of the included bookshop 'a_samp' and to re-define the above mentioned constant; therefore, it would be like that:
pawn Code:
#include <a_samp>
#undef MAX_PLAYERS
#define MAX_PLAYERS Quantity // We change 'quantity' into the quantity of slot's that you use in your Server.
Loop's.
Always we create curls, already be for all the players or to repeat 'X' code. When we use a curl for all the players, the best option is to use 'foreach' for ******. What this include does is to create a curl only for the connected players, and is %100 exactly.
I will leave the link them of of the same one: [Include] foreach - 0.3 compatible - updated 19/12/12.
_____________________
Well, it was the whole tutorial.
I am new doing tutoriales, if they have some doubt or I was wrong in something, please, warn me.
Excuse the evil Englishman, I am of the Spanish section and do not remove very well with the Englishman that we say.
Regards.