char arrays
#1

I read this tutorial: https://sampforum.blast.hk/showthread.php?tid=216730
And I didnt quite understand.
If I use this:
pawn Code:
static gTeam[MAX_PLAYERS char];
instead of:
pawn Code:
static gTeam[MAX_PLAYERS];
how can it save me memory?
+If I use char arrays,to what I change the []? also to {}
for ex:
pawn Code:
if(gTeam[playerid] == TEAM_Mafia) { SetPlayerColor(playerid,TEAM_Mafia_COLOR); }
    else if(gTeam[playerid] == TEAM_Dealers) { SetPlayerColor(playerid,TEAM_Dealers_COLOR); }
Thanks 4 helpers
Reply
#2

bump..
Reply
#3

Bump...
Reply
#4

A normal variable can hold values that are between -2,147,483,648 and 2,147,483,647 (as in Slice's explanation)
This makes the variable have a size of 4 bytes in each cell.

But a char variable can ONLY hold values within the range of 0 and 255. So, it has a smaller size of 1 byte per cell.

Try this code:

pawn Code:
public OnFilterScriptInit()
{
    new cell_Variable[2];
   
    cell_Variable[0] = 2147483647;
    cell_Variable[1] = -2147483647;
   
    printf("CELL: %i || %i ", cell_Variable[0], cell_Variable[1]);
    // It shows that it hold such huge values.
   
    new char_Variable[2 char];
   
    char_Variable{0} = 255; // This var cannot hold any value more that 255 (like a normal one)
    char_Variable{1} = 0; //And also no negative values, Remember (0-255)
   
    printf("CHAR: %i || %i ", char_Variable{0}, char_Variable{1});
   
    return true;
}
pawn Code:
new Variable[2048]; //It has a size of 2048 * 4 bytes = 8192 bytes.
   
    new char_Variable[2048 char];  //It has a size of 2048 * 1 byte = 2048 bytes.
    //But it has a limitation in the value it holds...
So, you should use char arrays only when you are sure that the value an index is holding doesn't exceed 255 or get less than 0.

List of things you can & cannot do using char arrays:
- Admin levels 0-5 (not 1337 though) : Yes!
- Vip Ranks : Yes!
- Money : No, it exceeds the value of 255
- Score : Nope.
- As Textdraw vars : No, Textdraw Ids can go beyond 255 too.

Now you can think of the rest!
Reply
#5

Alright,I understand now
Thanks a lot,+Rep
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)