[MAX_PLAYERS] or not?
#1

I ask myself everytime i call a new variable what i have to use.

When do I have to use something like this:

pawn Код:
new Variable[MAX_PLAYERS];
or when I should use:

pawn Код:
new Variable;
It would be helpful if someone could explain that to me. :)

PS.: Sorry if a thread like this exists, but i didnt find it.
Reply
#2

When you use MAX_PLAYERS you use variable as Variable[playerid], and is for a player, and if you dosent it is global variable...
Reply
#3

if you want to save something thats related to each player individually then use [MAX_PLAYERS].
eg: when u wanna save each players money
Money[playerid] = GetPlayerMoney <== his money will be saved to his playerid slot of Money var


if you want to save something thats common for all like max player count, use new variable;


Im a troll in explaining -.- but i know how to use them
Reply
#4

thx for the fast help. =)
Reply
#5

If you want the variable to be for a special purpose for example if you want it to be used by the server or by only one client then you go with
pawn Код:
new Variable;
If you want it to be used by every player or some other purposes for example using it for more than one purpose you go with
pawn Код:
new Var[MAX_PLAYERS]; // or
new Var[size];
I know I can't explain good.
Reply
#6

Quote:
Originally Posted by suhrab_mujeeb
Посмотреть сообщение
If you want the variable to be for a special purpose for example if you want it to be used by the server or by only one client then you go with
pawn Код:
new Variable;
If you want it to be used by every player or some other purposes for example using it for more than one purpose you go with
pawn Код:
new Var[MAX_PLAYERS]; // or
new Var[size];
I know I can't explain good.
Correct, I'll expand on it though as your description is slightly vague.

Declaring a variable such as:
pawn Код:
new Var;
is simple a single variable. You can only store a single piece of data in this variable (without messing about with binary and stuff).

Declaring a variable as:
pawn Код:
new Var[MAX_PLAYERS];
is called an array. An array can store multiple pieces of data related in 'slots' relative to the array itself. It's a little like doing:
pawn Код:
new Var1;
new Var2;
new Var3;
new Var4;
new Var5;
//etc...
(but, it uses slightly less memory if I remember correctly).
You can access (and set) data from/to an array by knowing the 'slot'. This could be stored in a variable or otherwise. Example:
pawn Код:
Var[playerid] = 1; //Where playerid is declared as an integer.
//Make Var[playerid] become 1.
pawn Код:
Var[0] = 0;
//Make the slot 0 of var equal to 0.
pawn Код:
for(new i; i < sizeof(Var); i++) //In this example, "sizeof(Var)" gets the array size.
{
    Var[i] = 100;
}
//This loop will make all the slots in the array called "Var" equal 100.
I hope you can understand better now.
Reply
#7

Quote:

I hope you can understand better now.

Yes I do, now. thanks a lot for all help =)
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)