Question about CHAR ARRAY
#1

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?
Reply
#2

You can use [5 char] and then {} not [] in code

player[playerid]{2} = 34;
Reply
#3

These are packed strings, more about this:
https://sampforum.blast.hk/showthread.php?tid=480529
Reply
#4

player[playerid]{2} = 34; (0 to 255 range)
Reply
#5

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.
Reply
#6

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
Reply
#7

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]
Reply
#8

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 ?
Reply
#9

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)