AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Why 33+32? (https://forums.alliedmods.net/showthread.php?t=329586)

nG_getwreck 12-30-2020 11:49

Why 33+32?
 
Hello, i'm just confused.

Why plugin authors use [33][32]? It's should [32][32] right? Since maxplayers in server is only 32.

Code example: The code is from admin check plugin
https://cdn.discordapp.com/attachmen...66/unknown.png

new adminnames[33][32]
this matrix is created with 33 rows and 32 columns to store names. Since max players are 32, the column 32 represents each player’s name. Why need 33 rows and why need 2D array?

Bugsy 12-30-2020 11:58

Re: Why 33+32?
 
It must be 33 as the elements in the array sized at 32 are accessed with 0 to 31. If you size it at 32, you will get index out of bounds if a player connects on the last slot (32).

sizing at 33 allows slots of 0 to 32. The first slot of 0 is technically a waste but this required to allow 32 to be a valid slot.

The second part of your example variable is sized at 32 for the max length of a string value.

Napoleon_be 12-30-2020 12:04

Re: Why 33+32?
 
I'm not quite sure on how to answer your first question, as it just goes in my head without even thinking on how to create those arrays. This might help you a bit https://wiki.alliedmods.net/Pawn_Tutorial#Arrays
https://wiki.alliedmods.net/index.ph...nsional_Arrays

The nessecarity of the 2D array depends on how it's used in the code. Mostly you're creating 2D arrays to avoid using multiple arrays for the same purpose. (storing weapon info, players info, menu items, menu prices, etc...)

There's also something i found coded by bugsy which could help you understand on how and why these arrays are declared this way. (Look at how their array sizes are different, and why they are.)

https://forums.alliedmods.net/showpo...80&postcount=5

HamletEagle 01-03-2021 05:22

Re: Why 33+32?
 
Quote:

Originally Posted by nG_getwreck (Post 2730705)
new adminnames[33][32]
this matrix is created with 33 rows and 32 columns to store names. Since max players are 32, the column 32 represents each player’s name. Why need 33 rows and why need 2D array?

Bugsy already explained why we use 33 instead of 32 so I'm going to explain why you need a 2D array.

For a moment, let's assume you are building an XP system and you want to store how many points each player has. For this you would use an array like this:
PHP Code:

new playerPoints[33

Each cell in this array is able to store a 4 bytes integer, meaning you can do things like:
PHP Code:

//give 3200 points to the player with index "id".
playerPoints[id] = 3200 

Now, imagine you also want to store rank(where each rank is a string like "Rank1", "Rank2"). What happens if you try the following:
PHP Code:

new playerRank[33]
playerRank[id] = "Rank1" 

Remember I said each cell can hold 4 bytes of data. How many bytes do you need in order to store "Rank1"? In pawn, a string is an array of characters. "Rank1" contains 5 characters, so 5 characters * 4 bytes = 20 bytes.
Oops, your playerRank array can only hold 4 bytes in each cell, but your strings have 20 bytes, you need more space!

The solution is to use a 2D array or a matrix.
PHP Code:

new playerRank[33][5

Intuitively, what happens here is now each slot in the array(0, 1, ..., 32) is made up of 5 cells instead of only one. Therefore, each slot can now hold 5 cells * 4 bytes each cell = 20 bytes(just perfect for your rank strings).

To visualize this:
When you do playerRank[33] you end up with something like this:
Code:

playerRank[0] -> [3200]
playerRank[1] -> [1500]
...
playerRank[32] -> []
([] means a space where you can put a value)

When you do playerRank[33][5] you have something like:
Code:

playerRank[0] -> [][][][][]
playerRank[1] -> [R][a][n][k][2]
...
playerRank[32] -> [][][][][]

Someone who knows C may notice I'm dancing around the notion of "array of pointers to arrays". But if you do know C, I believe this is the best way to understand this.
View the 2D array as an array where each slot is a pointer(or link) to an array of size 5.

Another way is to view the matrix in one line(called a linearised matrix):
Code:

playerRank[0][0] playerRank[0][1] ... playerRank[0][4] | playerRank[1][0] playerRank[1][1] ... playerRank[1][4] | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4]
first row                                                            second row                                                            last row

(each row has 5 slots and can store a string of 5 characters or less).

If you did:
PHP Code:

copy(playerRank[0], "Rank1")
copy(playerRank[1], "Rank2")

//which is the same as:
playerRank[0][0] = 'R'
playerRank[0][1] = 'a'
playerRank[0][2] = 'n'
playerRank[0][3] = 'k'
playerRank[0][4] = '1'

playerRank[1][0] = 'R'
playerRank[1][1] = 'a'
playerRank[1][2] = 'n'
playerRank[1][3] = 'k'
playerRank[1][4] = '2' 

The memory would look like:
Code:

'R' 'a' 'n' 'k' '1' | 'R' 'a' 'n' 'k' '2' | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4]
first row              second row                                                            last row

For your original example, replace 5 with 32.

fysiks 01-04-2021 00:31

Re: Why 33+32?
 
For more info, take a look at the programming tutorials here.


All times are GMT -4. The time now is 14:12.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.