Why are global variables always array of [33]?
Hello AlliedModders forum (again). My old account is MiloSx7 but I haven't been doing AMX for 8 years I can't remember the email or password for the life of me, sorry for creating new account :\
I just have one quick question for now, I'm looking at lots of .sma files right now and everyone makes global arrays that store all players data with [33]. Since there is max 32 players on server allowed, and arrays start from index 0, shouldn't they be defined with [31]? I have been doing javascript for a very long time now, this looks more like functional programming but the rules should stay the same right? So, why [33]? Thanks! |
Re: Why are global variables always array of [33]?
Because this allows you to pass the player id to the array, which is 1 to 32. This requires an array sized at 33 which gives you an array with indexes 0 to 32. The first array index of 0 is technically wasted memory. It's better practice to use MAX_PLAYERS + 1 to size the array, so it makes sense to others reading your code.
Try this and you will see why your thinking will not work: PHP Code:
|
Re: Why are global variables always array of [33]?
Mhm, I get it now. So basically
PHP Code:
PHP Code:
@edit: Sorry for going a little off topic but here goes nothing: Do you maybe know how to setup VScode to compile sma files? I've set up include paths correctly but can't seem to find anything online regarding compilation... Is it even possible? Thanks!! |
Re: Why are global variables always array of [33]?
Good, except it should be 'id <= maxplayers'. Remember, you want it to iterate 1 to 32, not 1 to 31.
PHP Code:
|
Re: Why are global variables always array of [33]?
Quote:
Code:
new iPlayers[MAX_PLAYERS + 1], iNumPlayers, idQuote:
|
Re: Why are global variables always array of [33]?
Ah, I finally get it. When I am the only one on my server and client_print the ID in the chat I get 1. So 32rd players ID would be 32, and so
PHP Code:
I've got one more question for you good folks, while building games in javascript I used array of objects, a lot. As far as I can tell there are no objects in pawn, so what trick can I do to achieve having something similar to array of objects which can store, for example, items name, damage, price, etc. I can have globals like PHP Code:
PHP Code:
And lastly, whats the difference between using #define or just using new? Is it just preference or does one have an advantage over the other, and in which case? Thanks!! |
Re: Why are global variables always array of [33]?
|
Re: Why are global variables always array of [33]?
All right, thank you both very much.
That should be enough to get me started. If I have any more questions down the road I'll open a new thread, this one can be locked. have a nice day! ^^ |
| All times are GMT -4. The time now is 02:38. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.