[Tutorial] Set Max Players to Server's Max Players
#4

Quote:
Originally Posted by illuminati2
Посмотреть сообщение
You can do this instead, (No need to declare player level to set it to 0)
Код:
new MAX_PLAYERS = 0;
You didn't understand what I tried to say...

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]; 
as MAX_PLAYERS shall be the size.
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();

has no effect on the array's dimension size of any variable created as global before..
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. 
As correctly said: checks whther "MAX_PLAYERS" is defined
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 
(your original post didn't say = 0, although it's not making any difference)
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]; 
this creates a global array with the dimension size of the value that is assigned to 'MAX_PLAYERS' *****

PHP код:
public OnGameModeInit() 

    
MAX_PLAYERS GetMaxPlayers(); // This Sets the MAX_PLAYERS value to the current server 
    //max players value. 
    
return 1

At this point the MAX_PLAYERS variable which has the value '0' until now is getting a new number assigned. So at this point we actually assign the correct value of the maximum possible players to the variable


--- 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:
Originally Posted by illuminati2
Посмотреть сообщение
You can do this instead, (No need to declare player level to set it to 0)
Код:
new MAX_PLAYERS = 0;
Edit: The problem is to set the MAX_PLAYERS automatically on server init. And this tutorial explains how to. Thanks for pointing out this. I've made the code above instead. And you can make this
Код:
new MAX_PLAYERS = 32; // The amount of max players of your server.
So you can use it in arrays. The main idea of this tutorial is to stop the abuse of MAX_PLAYERS by setting it to the server max players.
You still can not.
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 
and loops like
PHP код:
for(new i=0j=GetPlayerPoolSize(); i<=ji++) 

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
Reply


Messages In This Thread
Changing MAX_PLAYERS value & Another way to loop. - by illuminati2 - 04.03.2016, 20:11
Re: Set Max Players to Server's Max Players - by Sascha - 04.03.2016, 20:25
Re: Set Max Players to Server's Max Players - by illuminati2 - 04.03.2016, 20:36
Re: Set Max Players to Server's Max Players - by Sascha - 04.03.2016, 20:52
Re: Set Max Players to Server's Max Players - by Crayder - 04.03.2016, 20:57
Re: Set Max Players to Server's Max Players - by illuminati2 - 04.03.2016, 20:58
Re: Set Max Players to Server's Max Players - by AndySedeyn - 05.03.2016, 06:58

Forum Jump:


Users browsing this thread: 1 Guest(s)