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

This tutorial will show you how to loop with your server's max players not the default MAX_PLAYERS. also it will show you how to set MAX_PLAYERS Value to another way.

1- Looping With Server's Max Players
PHP код:
    for(new 0GetMaxPlayers(); i++) // GetMaxPlayers(); gets your server's max players, use it instead of MAX_PLAYERS as MAX_PLAYERS is 500 maybe your server is more than 500 or less than it.
    
{
        
//something here...
    

Now, you can loop using your server's max players not the default MAX_PLAYERS value. You can also change the MAX_PLAYERS Value instead.

You can also, loop using the current online players so it's not 100, 500 or even 1000 it will be the amount of your In Game players. Like in the code below

PHP код:
    for(new i=0<= GetPlayerPoolSize(); i++)
    {
        
//something here...
    

Also, you can loop by this way (If to can't just use the above).
PHP код:
for(new 0;  GetPlayerPoolSize(); <= ji++) 
2- Changing MAX_PLAYERS Value
PHP код:
#if defined MAX_PLAYERS //checks if MAX_PLAYERS if defined
    #undef MAX_PLAYERS // undefines max players so we can re define it with the new value we want.
    #define MAX_PLAYERS 100 // This is just an example of max players, change this 100 to whatever you like.
#endif // Now we finished it. 
Now, you can Loop with MAX_PLAYERS, but to be honest GetMaxPlayers(); is better as it will be set on server's Max Players not the default MAX_PLAYERS also, it will make it easier for you than changing the MAX_PLAYERS value everytime.


Some mistakes many beginners do, like using playerid in a loop. You should use the word you used in the loop for example "i" in the code below.
PHP код:
    for(new 0GetMaxPlayers(); i++) // GetMaxPlayers(); gets your server's max players, use it instead of MAX_PLAYERS as MAX_PLAYERS is 500 maybe your server is more than 500 or less than it.
    
{
        
GivePlayerMoney(i4000); // Do NOT Use playerid, in loops you've to use i, or any word you used. (Not Random)
    



This tutorial is 100% Clean after the re wrote of it. Special thanks to Sascha for pointing out some mistakes. Now the tutorial has been re wrote in a shorter way and easier. Without doing anything that may lead to a server crash. NOTE: the tutorial name maybe different to this current tutorial as it has been re wrote with some idea changes. Don't worry the replies below has been fixed.
Reply
#2

I'd be careful doing it this way.
If you create variables that use "MAX_PLAYERS" as array size (which 99% of all scripts do), you are going create a variable with an array size of "0".
Either you have to define it as "new MAX_PLAYERS = 500;" or whichever value you would like to use, or easiest, just re-define MAX_PLAYERS hardcoded with your amount of max player slots


Example:
Код:
new MAX_PLAYERS;
new PlayerLevel[MAX_PLAYERS];  //MAX_PLAYERS is = 0 at this point

public OnGameModeInit()
{
    MAX_PLAYERS = GetMaxPlayers();  //This will change the value assigned to 'MAX_PLAYERS' but it will not change the array dimension of 'PlayerLevel'
    return 1;
}
Reply
#3

EDIT: Thanks for your reply. It will be fixed.
Reply
#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
#5

It's a by far better solution to just redefine it.

#undef MAX_PLAYERS
#define MAX_PLAYERS number
Reply
#6

Yeah. I've understood everything, I knew there was gonna be somehow a problem as in my reading of the code I thought that it was gonna cause a problem. Anyways tutorial has been re wrote in a safer way. A person should learn form his mistakes & I did. No one doesn't do mistakes but the best thing is to solve it. I've never felt bad by doing mistakes, I feel happy because I knew the way to solve it, Thanks.

I knew from first the size of Max players can't be as the max players of the server. But I just was trying maybe it works. Great, now this tutorial is clean guys.
Reply
#7

That loop can easily be optimised as follows:
PHP код:
for(new 0GetPlayerPoolSize(); <= ji++) 
In this slightly optimised loop above, the function 'GetPlayerPoolSize()' will not be called every single iteration. It is called upon creating j -- which is when the loop is initialised -- and therefore will improve its performance. It's an easy optimisation and a good habit.
It's redundant when you have only one loop in your script, but imagine a gamemode with hundreds if not thousands of unoptimised loops.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)