04.03.2016, 20:52
Quote:
You can do this instead, (No need to declare player level to set it to 0)
Код:
new MAX_PLAYERS = 0; |
Example:
Let's say I have a variable that should save the level for each player, what saves whatever per player...
These variables are going to be declared at the top of your script, with a dimension set to it.
Everyone will declare such a variable like this:
PHP код:
new PlayerLevels[MAX_PLAYERS];
Thing now is - if you do it the way you just suggested (undefining MAX_PLAYERS and then creating a new variable instead) - that it is not going to work out anymore.
At the point where you declare the PlayerLevels dimension as 'MAX_PLAYERS' the value of 'MAX_PLAYERS' equals zero!
You are assigning the variable at "OnGameModeInit", although OnGameModeInit is actually called AFTER you created all the global variables.
So
PHP код:
public OnGameModeInit()
{
MAX_PLAYERS = GetMaxPlayers();
}
So what your code does is the following:
[php]
#include <a_samp> [/php
include the basic samp functions obviously.
PHP код:
#if defined MAX_PLAYERS // Checks if MAX_PLAYERS is Defined.
#undef MAX_PLAYERS // Undefines MAX_PLAYERS if it's Defined, so we can declare it without errors.
#endif // Ends the if line to avoid errors.
if it is 'destroy' the definition -> At this point the server doesn't know the telling 'MAX_PLAYERS' anymore
PHP код:
new MAX_PLAYERS = 0; // declares MAX_PLAYERS
this creates a new global variable called 'MAX_PLAYERS' (from now on the server knows the telling again) and assignes the value "0" to it
Now nearly each server has something like
PHP код:
new PlayerLevel[MAX_PLAYERS];
PHP код:
public OnGameModeInit()
{
MAX_PLAYERS = GetMaxPlayers(); // This Sets the MAX_PLAYERS value to the current server
//max players value.
return 1;
}
--- NOTHING MORE ---
In this order...
As you see: You create a global array with the size 0!
Assigning the correct value at "OnGameModeInit" does NOT change the dimension size of the array!
I highly recomment you not to do it this way.
What you can do, to save some resources in loops is either just directly to use "GetMaxPlayers();" or even "GetPlayerPoolSize()", or to create a new variable called "A_MAX_PLAYERS" which you can use for loops. But completely destroying the actual definition and then creating a zero pointing variable will not do the job for 90% of code structures.
Quote:
You can do this instead, (No need to declare player level to set it to 0)
Код:
new MAX_PLAYERS = 0; Код:
new MAX_PLAYERS = 32; // The amount of max players of your server. |
The size will now be 32, and it will stay like it! If you have a higher player limit than 32 (or whatever you write instead of 32) you will get an "index out of bounds" crash whenever the variable gets called for a player that has a higher playerid.
What you can do to stop the "exploiting" is
PHP код:
#if defined MAX_PLAYERS
#undef MAX_PLAYERS
#endif
#define MAX_PLAYERS YOUR_ACTUAL_PLAYER_LIMIT_HERE
PHP код:
for(new i=0, j=GetPlayerPoolSize(); i<=j; i++)
No person that doesn't know what he/she is doing should ever follow the proceedure you just explained here. If you are adressing this to beginners, you should tell them a safe way to do it, not a way that will crash their server and then 'cause a bunch of Scripting Help topics telling: halp me server crash I dunno what happen