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

Solved FormatPlayerMenu()


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-12-2020 , 06:47   FormatPlayerMenu()
Reply With Quote #1

Title says it, i'm looking for a function that puts all this code, in that one single function.

PHP Code:
CreateTempArray(250);
    new 
iPlayerMenu;

    
#if AMXX_VERSION_NUM < 183
    
formatex(szTempcharsmax(szTemp), "%L"LANG_PLAYER"CHOOSE_PLAYER_REMOVE");
    
iPlayerMenu menu_create(szTemp"RemovePlayerHandler");
    
#else
    
iPlayerMenu menu_create(fmt("%L"LANG_PLAYER"CHOOSE_PLAYER_REMOVE"), "RemovePlayerHandler");
    
#endif 
Currently, i have this, but it's not working out the way i want it to.

PHP Code:
new iPlayerMenu;
    
FormatPlayerMenu(iPlayerMenu"%L""CHOOSE_PLAYER_REMOVE""RemovePlayerHandler"); 
PHP Code:
public FormatPlayerMenu(iPlayerMenuszHeader[], szData[], szHandler[]) {
    
CreateTempArray(250);

    
#if AMXX_VERSION_NUM < 183
    
formatex(szTempcharsmax(szTemp), szHeaderLANG_PLAYERszData);
    
iPlayerMenu menu_create(szTempszHandler);
    
#else
    
iPlayerMenu menu_create(fmt(szHeaderLANG_PLAYERszData), szHandler);
    
#endif

PHP Code:
public CreateTempArray(iSize) {
    
#if AMXX_VERSION_NUM < 183
    
new szTemp[iSize]
    
#endif

__________________

Last edited by Napoleon_be; 04-12-2020 at 16:30.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-12-2020 , 07:09   Re: FormatPlayerMenu()
Reply With Quote #2

nvm, found my solution
PHP Code:
public CreateTempArray(iSize) {
    
#if AMXX_VERSION_NUM < 183
    
new szTemp[iSize]
    
#endif
}

public 
FormatPlayerMenu(idiPlayerMenuszHeader[], szData[], szHandler[]) {
    
CreateTempArray(250);

    
#if AMXX_VERSION_NUM < 183
    
formatex(szTempcharsmax(szTemp), szHeaderLANG_PLAYERszData);
    
iPlayerMenu menu_create(szTempszHandler);
    
#else
    
iPlayerMenu menu_create(fmt(szHeaderLANG_PLAYERszData), szHandler);
    
#endif

    
new iPlayers[32], iNumszName[33], szUserId[32];
    
get_players(iPlayersiNum"a");

    for(new 
iiNumi++) {
        
get_user_name(iPlayers[i], szNamecharsmax(szName));
        
formatex(szUserIdcharsmax(szUserId), "%d"get_user_userid(iPlayers[i]));
        
menu_additem(iPlayerMenuszNameszUserId);
    }
    
menu_display(idiPlayerMenu);

__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-12-2020 , 08:47   Re: FormatPlayerMenu()
Reply With Quote #3

That's not really valid. You're creating the "szTemp" variable in one function and trying to use it in another. The code won't even compile under AMXX < 1.8.3. There's no need for the "CreateTempArray()" function, simply register the variable directly in "FormatPlayerMenu()" if "AMXX_VERSION_NUM < 183".

"LANG_PLAYER" is not valid here. This is used when sending a message to all players and when the function it's used in contains a client index (e.g. "client_print"). You need to use "id" instead of "LANG_PLAYER".

The max name length is 32, not 33.

Code:
formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(iPlayers[i]));

num_to_str()?
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-12-2020 , 08:57   Re: FormatPlayerMenu()
Reply With Quote #4

Quote:
Originally Posted by OciXCrom View Post
That's not really valid. You're creating the "szTemp" variable in one function and trying to use it in another. The code won't even compile under AMXX < 1.8.3. There's no need for the "CreateTempArray()" function, simply register the variable directly in "FormatPlayerMenu()" if "AMXX_VERSION_NUM < 183".

"LANG_PLAYER" is not valid here. This is used when sending a message to all players and when the function it's used in contains a client index (e.g. "client_print"). You need to use "id" instead of "LANG_PLAYER".

The max name length is 32, not 33.

Code:
formatex(szUserId, charsmax(szUserId), "%d", get_user_userid(iPlayers[i]));

num_to_str()?
Then how should i format my menu's here?

PHP Code:
ReadFile() {
    
#if AMXX_VERSION_NUM < 183
    
formatex(szTempcharsmax(szTemp), "%L"LANG_PLAYER"MENU_CHOOSE_COLOR");
    
iColorMenu menu_create(szTemp"ColorMenuHandler");
    
#else
    
iColorMenu menu_create(fmt("%L"LANG_PLAYER"MENU_CHOOSE_COLOR"), "ColorMenuHandler");
    
#endif

    
new iFilePointer fopen("addons/amxmodx/configs/GlowMenu.txt""rt");
    new 
szLine[64], szText[32], szColor[16];

    while(
fgets(iFilePointerszLinecharsmax(szText))) {
        
trim(szLine);

        if(
szLine[0] == EOS || szLine[0] == ';') {
            continue;
        }

        
split(szLineszColorcharsmax(szColor), szLinecharsmax(szLine), " ");
        
remove_quotes(szColor);
        
format(szTextcharsmax(szText), "%s"szColor);
        
add(szLinecharsmax(szLine), szColor);

        
menu_additem(iColorMenuszTextszLine);
        
log_amx("Color: %s - RGB: %i"szTextszLine);
    }
    
fclose(iFilePointer);

__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-12-2020 , 09:11   Re: FormatPlayerMenu()
Reply With Quote #5

Declare the "szTemp" variable right under the first version check.

Unfortunately I don't think there's a way you can use multilingual here, at least not with a global static menu in 1.8.2.
In 1.8.3 and above there's an aditional argument in "menu_create" that allows you to use multilingual title and items which automatically handles the language every time the menu is displayed.

Code:
native menu_create(const title[], const handler[], bool:ml = false);

So you can do this in 1.8.3:

Code:
iColorMenu = menu_create("MENU_CHOOSE_COLOR", "ColorMenuHandler", true);

Same goes when adding the items - only pass in the language keys as item names.

So, you should either convert this into a dynamic menu (creating it every time a player wants to use it) or limit the plugin only for 1.9.
__________________

Last edited by OciXCrom; 04-12-2020 at 09:15.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-12-2020 , 09:31   Re: FormatPlayerMenu()
Reply With Quote #6

Quote:
Originally Posted by OciXCrom View Post
Declare the "szTemp" variable right under the first version check.

Unfortunately I don't think there's a way you can use multilingual here, at least not with a global static menu in 1.8.2.
In 1.8.3 and above there's an aditional argument in "menu_create" that allows you to use multilingual title and items which automatically handles the language every time the menu is displayed.

Code:
native menu_create(const title[], const handler[], bool:ml = false);

So you can do this in 1.8.3:

Code:
iColorMenu = menu_create("MENU_CHOOSE_COLOR", "ColorMenuHandler", true);

Same goes when adding the items - only pass in the language keys as item names.

So, you should either convert this into a dynamic menu (creating it every time a player wants to use it) or limit the plugin only for 1.9.
Okay i understand, but why is the data transfered correctly and shown perfectly on the menu's? Seems like it is all working well, is it because i'm running 1.9.0?
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-12-2020 , 09:59   Re: FormatPlayerMenu()
Reply With Quote #7

Because either you're testing by yourself and your language doesn't change or everyone in the server uses the same language.
__________________

Last edited by OciXCrom; 04-12-2020 at 09:59.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-12-2020 , 10:24   Re: FormatPlayerMenu()
Reply With Quote #8

Quote:
Originally Posted by Napoleon_be View Post
nvm, found my solution
PHP Code:
public CreateTempArray(iSize) {
    
#if AMXX_VERSION_NUM < 183
    
new szTemp[iSize]
    
#endif
}

public 
FormatPlayerMenu(idiPlayerMenuszHeader[], szData[], szHandler[]) {
    
CreateTempArray(250);

    
#if AMXX_VERSION_NUM < 183
    
formatex(szTempcharsmax(szTemp), szHeaderLANG_PLAYERszData);
    
iPlayerMenu menu_create(szTempszHandler);
    
#else
    
iPlayerMenu menu_create(fmt(szHeaderLANG_PLAYERszData), szHandler);
    
#endif

    
new iPlayers[32], iNumszName[33], szUserId[32];
    
get_players(iPlayersiNum"a");

    for(new 
iiNumi++) {
        
get_user_name(iPlayers[i], szNamecharsmax(szName));
        
formatex(szUserIdcharsmax(szUserId), "%d"get_user_userid(iPlayers[i]));
        
menu_additem(iPlayerMenuszNameszUserId);
    }
    
menu_display(idiPlayerMenu);

"szTemp" is local inside CreateTempArray. The stack will be cleared once CreateTempArray returns and therefore it is not accessible from other functions. Why you thought it would be accessible from FormatPlayerMenu is a mystery.
__________________

Last edited by HamletEagle; 04-12-2020 at 10:25.
HamletEagle is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-12-2020 , 10:27   Re: FormatPlayerMenu()
Reply With Quote #9

Quote:
Originally Posted by HamletEagle View Post
"szTemp" is local inside CreateTempArray. The stack will be cleared once CreateTempArray returns and therefore it is not accessible from other functions. Why you thought it would be accessible from FormatPlayerMenu is a mystery.
Can i fix this by declaring szTemp[] globally?

EDIT: I have fixed everything, except for the ReadFile() color menu. I don't think it's smart reading the file every single time you want to open the menu, but on the other hand i want this plugin to have ML Support, any feedback?
__________________

Last edited by Napoleon_be; 04-12-2020 at 11:04.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-12-2020 , 11:06   Re: FormatPlayerMenu()
Reply With Quote #10

Read the file in a data structure(like an array/dynamic array/trie depending on the data and how it will be used) and use the structure to build the menu.
I/O is slow so you should interact with files as few times as possible.
__________________
HamletEagle is offline
Reply


Thread Tools
Display Modes

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 21:44.


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