AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Enum's (https://forums.alliedmods.net/showthread.php?t=318430)

LearninG 08-31-2019 02:13

Enum's
 
Hi,
why would this have errors ? :
PHP Code:

#include <amxmodx>

enum _:PlayerInfo
{
    
Player_Name[32] ,
    
Player_Authid[35]
}

new 
g_PlayerData33 ] [ PlayerInfo ]

public 
plugin_init()
{
    
register_clcmd("say /test" "test")
}

public 
test(id)
{
    new 
eInfo[g_PlayerData// difference
    
get_user_name(id eInfo[Player_Name] , charsmax(eInfo[Player_Name]))
    
get_user_authid(id eInfo[Player_Authid] , charsmax(eInfo[Player_Authid]))
    
    
client_print(id print_chat "Your name is : %s" eInfo[Player_Name])
    
client_print(id print_chat "Your steamid is : %s" eInfo[Player_Authid] )


Quote:

error 008: must be a constant expression; assumed zero
error 017: undefined symbol "eInfo"
warning 215: expression has no effect
error 001: expected token: ";", but found "]"
error 029: invalid expression, assumed zero
fatal error 107: too many error messages on one line
however this one compiles and works fine in game.

PHP Code:

#include <amxmodx>

enum _:PlayerInfo
{
    
Player_Name[32] ,
    
Player_Authid[35]
}

new 
g_PlayerData33 ] [ PlayerInfo ]

public 
plugin_init()
{
    
register_clcmd("say /test" "test")
}

public 
test(id)
{
    new 
eInfo[PlayerInfo// difference 
    
get_user_name(id eInfo[Player_Name] , charsmax(eInfo[Player_Name]))
    
get_user_authid(id eInfo[Player_Authid] , charsmax(eInfo[Player_Authid]))

    
client_print(id print_chat "Your name is : %s" eInfo[Player_Name])
    
client_print(id print_chat "Your steamid is : %s" eInfo[Player_Authid] )


Quote:

warning 203: symbol is never used: "g_PlayerData"
Header size: 256 bytes
Code size: 404 bytes
Data size: 240 bytes
Stack/heap size: 16384 bytes
Total requirements: 17284 bytes

1 Warning.
Done.
[Finished in 0.3s]
so if we do not need this here , where we will need it ?
PHP Code:

new g_PlayerData33 ] [ PlayerInfo 


E1_531G 08-31-2019 02:44

Re: Enum's
 
g_PlayerData is an array, and PlayerInfo is an enum.
You cannot create an array using other array as dimension.

Quote:

so if we do not need this here , where we will need it ?
you need it here:
Quote:

get_user_name(id , g_PlayerData[id][Player_Name] , charsmax(g_PlayerData[][Player_Name]))
now you have player's name in this global array, and you can use it anywhere you need it.

Natsheh 08-31-2019 03:15

Re: Enum's
 
You are getting those errors because you are trying to declare an array size with a non constant value/uncomplete memory address of the var, declaring an array size should be done with pre defined size so the compiler would know what size it should be assigned for.

Note:
Also declaring global array variables should always be in the first of the script and thier sizes before them

LearninG 08-31-2019 04:09

Re: Enum's
 
Quote:

Originally Posted by E1_531G (Post 2665346)
g_PlayerData is an array, and PlayerInfo is an enum.
You cannot create an array using other array as dimension.

Quote:

so if we do not need this here , where we will need it ?
you need it here:
Quote:

get_user_name(id , g_PlayerData[id][Player_Name] , charsmax(g_PlayerData[][Player_Name]))
now you have player's name in this global array, and you can use it anywhere you need it.

works , thanks.


Quote:

Originally Posted by Natsheh (Post 2665348)
Note:
Also declaring global array variables should always be in the first of the script and thier sizes before them

like this ?
PHP Code:

#define MAX_PLAYERS   32

enum _:PlayerInfo
{
    
Player_Name[32] ,
    
Player_Authid[35]
}

new 
g_PlayerData[MAX_PLAYERS 1] [ PlayerInfo 

or


PHP Code:

#define G_PLAYER_DATA_SIZE  33

enum _:PlayerInfo
{
    
Player_Name[32] ,
    
Player_Authid[35]
}

new 
g_PlayerData[G_PLAYER_DATA_SIZE] [ PlayerInfo 


Bugsy 08-31-2019 10:33

Re: Enum's
 
Either technically works but I prefer the first since it indicates what you are holding in the array. MAX_PLAYERS is used across the majority of plugins and everyone knows what it corresponds to. G_PLAYER_DATA_SIZE does not tell the same story.


All times are GMT -4. The time now is 17:27.

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