Raised This Month: $51 Target: $400
 12% 

Why are global variables always array of [33]?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
QuantumMekkanics
New Member
Join Date: Apr 2021
Old 04-20-2021 , 20:40   Why are global variables always array of [33]?
Reply With Quote #1

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!
QuantumMekkanics is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-20-2021 , 21:01   Re: Why are global variables always array of [33]?
Reply With Quote #2

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:
//31 will not work as it gives you 0-30
//32 will not work as it gives you 0-31
//33 works, giving you 0-32

new PlayerArray31 ]; 
PlayerArray32 ] = 5
__________________

Last edited by Bugsy; 04-20-2021 at 21:06.
Bugsy is offline
QuantumMekkanics
New Member
Join Date: Apr 2021
Old 04-20-2021 , 21:53   Re: Why are global variables always array of [33]?
Reply With Quote #3

Mhm, I get it now. So basically

PHP Code:
new array[33];

array[
33] = 1// invalid

array[0] = 1// valid
// ...
array[32] = 1// valid 
So when I want to loop it I should do

PHP Code:
#define MAX_PLAYERS 32

new array[MAX_PLAYERS 1];

for (new 
id 1id MAX_PLAYERSid++) {
  array[
id] = something;

?

@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!!

Last edited by QuantumMekkanics; 04-20-2021 at 22:26. Reason: lots of typos :D
QuantumMekkanics is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-20-2021 , 22:53   Re: Why are global variables always array of [33]?
Reply With Quote #4

Good, except it should be 'id <= maxplayers'. Remember, you want it to iterate 1 to 32, not 1 to 31.

PHP Code:
for (new id 1id <= MAX_PLAYERSid++) 
I personally use AMX-X Studio, have used it since I started scripting in 2005. I believe you can configure other editors like Notepad++ to compile sma's. I think there are some tutorials floating around.
__________________

Last edited by Bugsy; 04-20-2021 at 22:55.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-21-2021 , 01:33   Re: Why are global variables always array of [33]?
Reply With Quote #5

Quote:
Originally Posted by QuantumMekkanics View Post
PHP Code:
#define MAX_PLAYERS 32

new array[MAX_PLAYERS 1];

for (new 
id 1id MAX_PLAYERSid++) {
  array[
id] = something;

Looping over all player slots is almost never needed and often a waste of resources. Most often, this is done by using get_players() to get various sets of players and then you would loop over only those players:

Code:
new iPlayers[MAX_PLAYERS + 1], iNumPlayers, id
get_players(iPlayers, iNumPlayers)

for( new i = 0, i < iNumPlayers, i++ )
{
	id = iPlayers[i]

	// Do stuff with 'id' here
}



Quote:
Originally Posted by QuantumMekkanics View Post
@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!!
I use VS Code and I have the "AMXXPawn Language" extension by Klippy installed and it has a compiler configuration feature. However, I've never used it. I have my own external script that I use for compiling. If you are having issues with it you should post here.
__________________
fysiks is offline
QuantumMekkanics
New Member
Join Date: Apr 2021
Old 04-23-2021 , 00:26   Re: Why are global variables always array of [33]?
Reply With Quote #6

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:
new arr[32];
arr[32] = true // invalid 
By the way, I couldn't get VSCode to compile the .sma file, so I just did what you do fsyks and made a script to my desktop that compiles the code for me. At least I don't have to launch AMX Studio just to click the green compile button every time I want to compile

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:
itemnameName "name";
itemnameDamage 3;
itemnamePrice 1;
// ... 
But that doesn't feel right and becomes spaghetti pretty quickly... I was thinking of having something like:

PHP Code:
#define ITEM_1 1;
itemNames[][] = {"name""name2", ...};
itemDamages[][] = {24, ...};

itemnames[ITEM_1];
itemDamages[ITEM_1]; 
But just wondering if there is some standard you prefer.

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!!
QuantumMekkanics is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-23-2021 , 00:53   Re: Why are global variables always array of [33]?
Reply With Quote #7

#define vs variable is explained here.

Pawn is not object oriented and does not have the concept of structs. However, you can create a sort of struct as shown in an enum tutorial here (see the "Data Structures" section).
__________________
fysiks is offline
QuantumMekkanics
New Member
Join Date: Apr 2021
Old 04-23-2021 , 17:05   Re: Why are global variables always array of [33]?
Reply With Quote #8

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! ^^
QuantumMekkanics is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:30.


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