Question about CHAR ARRAY -
DleyDeR - 20.12.2017
Hi. I using so many arrays; new player[MAX_PLAYERS][value]
example: new player[MAX_PLAYERS][5];
In one of the topics I read that, this is a bad solution, and will be better, if we use char array; new player[MAX_PLAYERS][5 char]
But when I use it, then compiler shows the error: array index out of bounds
Then multiplying the value of the array helps;; player[MAX_PLAYERS][5*2 char]
But whether then it will not be the same as: new player[MAX_PLAYERS][10] ?
Which option is better and more optimal?
Re: Question about CHAR ARRAY -
Jefff - 20.12.2017
You can use [5 char] and then {} not [] in code
player[playerid]{2} = 34;
Re: Question about CHAR ARRAY -
Kaperstone - 20.12.2017
These are packed strings, more about this:
https://sampforum.blast.hk/showthread.php?tid=480529
Re: Question about CHAR ARRAY -
Argument - 20.12.2017
player[playerid]{2} =
34; (
0 to
255 range)
Re: Question about CHAR ARRAY -
DleyDeR - 20.12.2017
Truth.
-
In other arrays, i.e. new playerCar[MAX_PLAYERS] - where; MAX_PLAYERS = 120
Can I use char? : new playerCar[MAX_PLAYERS char]
and in code.. playerCar{playerid}
Does it make sense? ;; It's about the greatest possible code optimization.
Re: Question about CHAR ARRAY -
Jefff - 20.12.2017
It doesnt matter how big is MAX_PLAYERS, you can assign a value between 0-255
and you cant use it as
playerCar{playerid} = CreateVehicle(...); because created vehicle can be 1-1000, char array supports only ids 0-255
Re: Question about CHAR ARRAY -
Misiur - 20.12.2017
And for your original question:
4 chars takes 1 cell, and you can't reserve "quarter of a cell". So
[1 char] takes as much space as [1]
[4 char] ^ [1]
[5 char] ^ [2]
[10 char] ^ [3]
Re: Question about CHAR ARRAY -
Dayrion - 23.12.2017
Why isn't that possible?
PHP код:
new Array[MAX_VEHICLES char][2];
Array[742][0] = 1;
Also, that compile fine too
PHP код:
new Array[MAX_VEHICLES][2 char];
Array[742][0] = 1;
Array[742]{0} = 1;
Is there any benefit to use char on multi-dimensional arrays ?
Re: Question about CHAR ARRAY -
ThePhenix - 23.12.2017
Quote:
Originally Posted by Dayrion
Why isn't that possible?
PHP код:
new Array[MAX_VEHICLES char][2];
Array[742][0] = 1;
Also, that compile fine too
PHP код:
new Array[MAX_VEHICLES][2 char];
Array[742][0] = 1;
Array[742]{0} = 1;
Is there any benefit to use char on multi-dimensional arrays ?
|
The first example you provide is incorrect as you're using the char operator for the first dimension, the char operator returns the number of cells needed to hold a certain number of characters. So, in your case what you're really doing (assuming MAX_VEHICLES is 2000 and taking into account that a cell is worth 4 bytes) is:
PHP код:
new Array[500][2];
That of course, is flawed because you won't be able to access elements at a greater index than 499 and you won't be able to use the array character index for the first dimension '{}'. As the PAWN Language Guide states:
Код:
You can use the “array character index” operator (braces: “{ }” only for the last dimension. For other dimensions, you must use the cell index operator (square brackets: “[ ]”).
Now, considering your question. There is actually a benefit to the usage of the char operator as it would reduce the amount of memory used. There is a drawback though, just as Jefff says you can only store values in the range of 0 to 255 and you can't store negative numbers.
This tutorial by Emmet explains packed strings in great detail:
https://sampforum.blast.hk/showthread.php?tid=480529