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? |
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. |
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 |
Re: Why 33+32?
Quote:
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:
PHP Code:
PHP Code:
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:
To visualize this: When you do playerRank[33] you end up with something like this: Code:
playerRank[0] -> [3200]Code:
playerRank[0] -> [][][][][]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]If you did: PHP Code:
Code:
'R' 'a' 'n' 'k' '1' | 'R' 'a' 'n' 'k' '2' | ... | playerRank[32][0] playerRank[32][1] ... playerRank[32][4] |
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.