AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Menu showing twice? (https://forums.alliedmods.net/showthread.php?t=325386)

supertrio17 06-19-2020 21:41

Menu showing twice?
 
Hey, so I am making class plugin using OciXCrom's Rank System, and using nVault. Menu should pop once player hit's level 10, and another one on level 20. But for some reason when you hit level 10 or 20, you get menu on spawn, you choose and when you respawn you get that menu once again, and after that, every time you respawn you will get class that you choosed first time menu was created, not second one. Idk if I made a mistake in my code, or nVault works like that, but if anyone has a suggestion, please tell me.
PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <engine>
#include <fun>
#include <crxranks>
#include <nvault>
#include <CromChat>
#include <hamsandwich>

new g_Vault;
new 
g_szAuthID[33][35];
new 
g_Class[33];

const 
Float:DEFAULT_GRAVITY_I        800.0

// LEVEL 10
#define C_ONE_TEN           "\wRogue"           //Change name for first class
#define C_RG_HP             90                    //Rogue HP
#define C_RG_MS             75.0                 //Rogue Added Movespeed || MAX SPEED MUST BE FLOAT!
#define C_RG_GV             750.0                 //Rogue Gravity

#define C_TWO_TEN           "\wDark Knight"     //Change name for second class
#define C_DK_HP             135
#define C_DK_MS             0.0
#define C_DK_GV             800.0

#define C_THREE_TEN         "\wWizard"          //Change name for third class
#define C_WZ_HP             100
#define C_WZ_MS             15.0
#define C_WZ_GV             650.0

// LEVEL 20
#define C_ONE_TW           "\wTrickster"        //Change name for first class
#define C_TK_HP             115
#define C_TK_MS             150.0
#define C_TK_GV             650.0

#define C_TWO_TW            "\wBlade Knight"    //Change name for second class
#define C_BK_HP             185
#define C_BK_MS             35.0
#define C_BK_GV             700.0

#define C_THREE_TW          "\wSoul Master"     //Change name for third class
#define C_SM_HP             125
#define C_SM_MS             50.0
#define C_SM_GV             350.0

public plugin_init()
{
    
register_plugin("Class Menu for CRX""2.0""Mr. Boopsy");
    
register_clcmd("amx_resetclassID",    "reset_id")
    
RegisterHam(Ham_Spawn"player""OnPlayerSpawn"1);
}

public 
plugin_cfg()
{
    new 
g_Vault nvault_open"ClassIdByBoopsy" );

    if ( 
g_Vault == INVALID_HANDLE )
        
set_fail_state"Error opening nVault" );
}

public 
plugin_end()
{
    
nvault_closeg_Vault );
}

public 
client_authorized(id)
{
    
get_user_authid(idg_szAuthID[id], charsmax(g_szAuthID[]));
}

public 
OnPlayerSpawn(id)
{
    if(
is_user_alive(id))
    {
        
LoadClass_Spawn(id);
    }
}

public 
crxranks_user_level_updated(idlevelbool:levelup)
{
    if(
is_user_alive(id))
    {
        new 
szKey[40];
        
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
        new 
iClass nvault_get(g_VaultszKey );
        if( 
iClass )
        {
            switch(
iClass)
            {
                case 
1:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Rogue&x04!");
                }
                case 
2:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Dark Knight&x04!");
                }
                case 
3:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Wizard&x04!");
                }
                case 
4:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Trickster&x04!");
                }
                case 
5:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Blade Knight&x04!");
                }
                case 
6:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Soul Master&x04!");
                }
            }
        }
        else
        {
            new 
cLevel crxranks_get_user_level(id);

            if( 
cLevel == 10 )
            {
                
rank_ten(id);
            }

            else if( 
cLevel == 20 )
            {
                
rank_tw(id);
            }
        }
    }
}

public 
rank_ten(id)
{
    new 
menu_ten menu_create("\r[\wChoose Class\r]\r""menu_ten_f")

    
menu_additem(menu_tenC_ONE_TEN"1"0)
    
menu_additem(menu_tenC_TWO_TEN"2"0)
    
menu_additem(menu_tenC_THREE_TEN"3"0)

    
menu_setprop(menu_tenMPROP_EXITMEXIT_ALL)

    
menu_display(idmenu_ten0)
}

public 
rank_tw(id)
{
    new 
menu_tw menu_create("\r[\wChoose Class\r]\r""menu_tw_f")

    
menu_additem(menu_twC_ONE_TW"1"0)
    
menu_additem(menu_twC_TWO_TW"2"0)
    
menu_additem(menu_twC_THREE_TW"3"0)

    
menu_setprop(menu_twMPROP_EXITMEXIT_ALL)

    
menu_display(idmenu_tw0)
}

public 
menu_ten_f(idmenu_tenitem_ten)
{
    if (
item_ten == MENU_EXIT)
    {
        
menu_destroy(menu_ten)
        return 
PLUGIN_HANDLED
    
}
    new 
data_ten[6], iName_ten[64]
    new 
acces_tencallback_ten
    menu_item_getinfo
(menu_tenitem_tenacces_tendata_ten,5iName_ten63callback_ten)

    new 
key_ten str_to_num(data_ten)

    switch(
key_ten)
    {
       case 
1:
       {
           
rank_ten_c_one(id);
           
g_Class[id] = 1;
           
CC_SendMessage(id"&x01>> &x04You choosed &x07Rogue&x04!");
       }
       case 
2:
       {
           
rank_ten_c_two(id);
           
g_Class[id] = 2;
           
CC_SendMessage(id"&x01>> &x04You choosed &x07Dark Knight&x04!");
       }
       case 
3:
       {
           
rank_ten_c_three(id);
           
g_Class[id] = 3;
           
CC_SendMessage(id"&x01>> &x04You choosed &x07Wizard&x04!");
       }
    }

    
menu_destroy(menu_ten)
    return 
PLUGIN_HANDLED
}

public 
menu_tw_f(idmenu_twitem_tw)
{
    if (
item_tw == MENU_EXIT)
    {
        
menu_destroy(menu_tw)
        return 
PLUGIN_HANDLED
    
}

    new 
data_tw[6], iName_tw[64]
    new 
acces_twcallback_tw

    menu_item_getinfo
(menu_twitem_twacces_twdata_tw,5iName_tw63callback_tw)

    new 
key_tw str_to_num(data_tw)

    switch(
key_tw)
    {
       case 
1:
       {
           
rank_tw_c_one(id);
           
g_Class[id] = 4;
           
CC_SendMessage(id"&x01>> &x04You choosed &x07Trickster&x04!");
       }
       case 
2:
       {
           
rank_tw_c_two(id);
           
g_Class[id] = 5;
           
CC_SendMessage(id"&x01>> &x04You choosed &x07Blade Knight&x04!");
       }
       case 
3:
       {
           
rank_tw_c_three(id);
           
g_Class[id] = 6;
           
CC_SendMessage(id"&x01>> &x04You choosed &x07Soul Master&x04!");
       }
    }

    
menu_destroy(menu_tw)
    return 
PLUGIN_HANDLED
}

public 
rank_ten_c_one(id// Rogue
{
    new 
szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Rogue

    
nvault_set(g_VaultszKeyszClass);

    
set_user_health(idC_RG_HP);
    
set_user_maxspeed(idget_user_maxspeedid ) + C_RG_MS);
    
set_user_gravity(id, ((C_RG_GV) / DEFAULT_GRAVITY_I))
}

public 
rank_ten_c_two(id// Dark Knight
{
    new 
szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Dark Knight

    
nvault_set(g_VaultszKeyszClass);

    
set_user_health(idC_DK_HP);
    
set_user_maxspeed(idget_user_maxspeedid ) + C_DK_MS);
    
set_user_gravity(id, ((C_DK_GV) / DEFAULT_GRAVITY_I))
}

public 
rank_ten_c_three(id// Wizard
{
    new 
szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Wizard

    
nvault_set(g_VaultszKeyszClass);

    
set_user_health(idC_WZ_HP);
    
set_user_maxspeed(idget_user_maxspeedid ) + C_WZ_MS);
    
set_user_gravity(id, ((C_WZ_GV) / DEFAULT_GRAVITY_I))
}

// LEVEL 20
public rank_tw_c_one(id// Trickster
{
    new 
szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Trickster

    
nvault_set(g_VaultszKeyszClass);

    
set_user_health(idC_TK_HP);
    
set_user_maxspeed(idget_user_maxspeedid ) + C_TK_MS);
    
set_user_gravity(id, ((C_TK_GV) / DEFAULT_GRAVITY_I))
}

public 
rank_tw_c_two(id// Blade Knight
{
    new 
szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Blade Knight

    
nvault_set(g_VaultszKeyszClass);

    
set_user_health(idC_BK_HP);
    
set_user_maxspeed(idget_user_maxspeedid ) + C_BK_MS);
    
set_user_gravity(id, ((C_BK_GV) / DEFAULT_GRAVITY_I))
}

public 
rank_tw_c_three(id// Soul Master
{
    new 
szClass[20];
    new 
szKey[40];

    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    
formatex(szClasscharsmaxszClass ), "%d"g_Class[id]); // Soul Master

    
nvault_set(g_VaultszKeyszClass);

    
set_user_health(idC_SM_HP);
    
set_user_maxspeed(idget_user_maxspeedid ) + C_SM_MS);
    
set_user_gravity(id, ((C_SM_GV) / DEFAULT_GRAVITY_I))
}

public 
LoadClass_Spawn(id)
{
    new 
cLevel crxranks_get_user_level(id);
    new 
szKey[40];
    
formatex(szKeycharsmaxszKey ), "%sCLASS"g_szAuthID[id]);
    new 
iClass nvault_get(g_VaultszKey );
    if( 
iClass )
    {
        switch(
iClass)
        {
            case 
1:
            {
                
set_user_health(idC_RG_HP);
                
set_user_maxspeed(idget_user_maxspeedid ) + C_RG_MS);
                
set_user_gravity(id, ((C_RG_GV) / DEFAULT_GRAVITY_I))
                
CC_SendMessage(id"&x01>> &x04Your class was updated to &x07Rogue&x04!");
            }
            case 
2:
            {
                
set_user_health(idC_DK_HP);
                
set_user_maxspeed(idget_user_maxspeedid ) + C_DK_MS);
                
set_user_gravity(id, ((C_DK_GV) / DEFAULT_GRAVITY_I))
                
CC_SendMessage(id"&x01>> &x04Your class was updated to &x07Dark Knight&x04!");
            }
            case 
3:
            {
                
set_user_health(idC_WZ_HP);
                
set_user_maxspeed(idget_user_maxspeedid ) + C_WZ_MS);
                
set_user_gravity(id, ((C_WZ_GV) / DEFAULT_GRAVITY_I))
                
CC_SendMessage(id"&x01>> &x04Your class was updated to &x07Wizard&x04!");
            }
            case 
4:
            {
                
set_user_health(idC_TK_HP);
                
set_user_maxspeed(idget_user_maxspeedid ) + C_TK_MS);
                
set_user_gravity(id, ((C_TK_GV) / DEFAULT_GRAVITY_I))
                
CC_SendMessage(id"&x01>> &x04Your class was updated to &x07Trickster&x04!");
            }
            case 
5:
            {
                
set_user_health(idC_BK_HP);
                
set_user_maxspeed(idget_user_maxspeedid ) + C_BK_MS);
                
set_user_gravity(id, ((C_BK_GV) / DEFAULT_GRAVITY_I))
                
CC_SendMessage(id"&x01>> &x04Your class was updated to &x07Blade Knight&x04!");
            }
            case 
6:
            {
                
set_user_health(idC_SM_HP);
                
set_user_maxspeed(idget_user_maxspeedid ) + C_SM_MS);
                
set_user_gravity(id, ((C_SM_GV) / DEFAULT_GRAVITY_I))
                
CC_SendMessage(id"&x01>> &x04Your class was updated to &x07Soul Master&x04!");
            }
        }
    }
    else if(
cLevel 10 && cLevel 20)
    {
        if( 
iClass )
        {
            switch(
iClass)
            {
                case 
1:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Rogue&x04!");
                }
                case 
2:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Dark Knight&x04!");
                }
                case 
3:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Wizard&x04!");
                }
                case 
4:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Trickster&x04!");
                }
                case 
5:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Blade Knight&x04!");
                }
                case 
6:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Soul Master&x04!");
                }
            }
        }
        else
        {
            
rank_ten(id);
        }
    }
    else if(
cLevel >= 20)
    {
        if( 
iClass )
        {
            switch(
iClass)
            {
                case 
1:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Rogue&x04!");
                }
                case 
2:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Dark Knight&x04!");
                }
                case 
3:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Wizard&x04!");
                }
                case 
4:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Trickster&x04!");
                }
                case 
5:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Blade Knight&x04!");
                }
                case 
6:
                {
                    
CC_SendMessage(id"&x01>> &x04Your class is &x07Soul Master&x04!");
                }
            }
        }
        else
        {
            
rank_tw(id);
        }
    }
}

public 
reset_id(id)
{
    if(
has_flag(id"m"))
    {
        
nvault_prune(g_Vault0get_systime());
        
server_cmd("restart");
    }



fysiks 06-19-2020 22:13

Re: Menu showing twice?
 
Have you done any debugging? What is the value of iClass (Line 337)? Based on what you've described (I think), it's returning a value of 0 every time.

The other thing that I notice is that in your menu handler, you call the rank_tw_c_xxx() function before you set g_Class[id] so when you use g_Class[id] in the rank_tw_c_xxx() function, it will have the old value in it.

Also, lines 388 and 425 ("if( iClass )") will always return false.

It looks like you have several places where you are duplicating code. For example, you set the health, maxspeed, and gravity in the same manner in both the spawn function and the menu handler. You should optimize the duplicate code into its own function.

Overall, I think you might consider rethinking the logic you're using here. I think you're making it more complicated than it needs to be. If I think of a simple example, I'll post it.

fysiks 06-19-2020 22:49

Re: Menu showing twice?
 
So, after looking at it a little longer, I realized that the crxranks plugin actually makes this much simpler than what you were trying to do. You only need to call the menu when the crxranks_user_level_updated() gets triggered and the level is 10 or 20. Then, in the spawn function, you simply need to set the settings that apply to the player's class. Here is a quick version of your plugin that I crated (you'll need to add the includes and constants and stuff and maybe fix some syntax or other minor issues):

Code:


public plugin_init()
{
        RegisterHam(Ham_Spawn, "player", "OnPlayerSpawn", 1);
}

public OnPlayerSpawn(id)
{
        ApplyClassSettings(id)
}

public crxranks_user_level_updated(id, level, bool:levelup)
{
        switch( level )
        {
                case 10: rank_ten(id)
                case 20: rank_tw(id)
        }
}

SetPlayerClass(id, class)
{
        g_Class = class
        ApplyClassSettings(id, class)
}

ApplyClassSettings(id)
{
        if( is_user_alive(id) )
        {
                switch(g_Class)
                {
                        case 1:
                        {
                                set_user_health(id, C_RG_HP);
                                set_user_maxspeed(id, get_user_maxspeed( id ) + C_RG_MS);
                                set_user_gravity(id, ((C_RG_GV) / DEFAULT_GRAVITY_I))
                                CC_SendMessage(id, "&x01>> &x04Your class was updated to &x07Rogue&x04!");
                        }
                        case 2:
                        {
                                set_user_health(id, C_DK_HP);
                                set_user_maxspeed(id, get_user_maxspeed( id ) + C_DK_MS);
                                set_user_gravity(id, ((C_DK_GV) / DEFAULT_GRAVITY_I))
                                CC_SendMessage(id, "&x01>> &x04Your class was updated to &x07Dark Knight&x04!");
                        }
                        case 3:
                        {
                                set_user_health(id, C_WZ_HP);
                                set_user_maxspeed(id, get_user_maxspeed( id ) + C_WZ_MS);
                                set_user_gravity(id, ((C_WZ_GV) / DEFAULT_GRAVITY_I))
                                CC_SendMessage(id, "&x01>> &x04Your class was updated to &x07Wizard&x04!");
                        }
                        case 4:
                        {
                                set_user_health(id, C_TK_HP);
                                set_user_maxspeed(id, get_user_maxspeed( id ) + C_TK_MS);
                                set_user_gravity(id, ((C_TK_GV) / DEFAULT_GRAVITY_I))
                                CC_SendMessage(id, "&x01>> &x04Your class was updated to &x07Trickster&x04!");
                        }
                        case 5:
                        {
                                set_user_health(id, C_BK_HP);
                                set_user_maxspeed(id, get_user_maxspeed( id ) + C_BK_MS);
                                set_user_gravity(id, ((C_BK_GV) / DEFAULT_GRAVITY_I))
                                CC_SendMessage(id, "&x01>> &x04Your class was updated to &x07Blade Knight&x04!");
                        }
                        case 6:
                        {
                                set_user_health(id, C_SM_HP);
                                set_user_maxspeed(id, get_user_maxspeed( id ) + C_SM_MS);
                                set_user_gravity(id, ((C_SM_GV) / DEFAULT_GRAVITY_I))
                                CC_SendMessage(id, "&x01>> &x04Your class was updated to &x07Soul Master&x04!");
                        }
                }
        }
}

public rank_ten(id)
{
        new menu_ten = menu_create("\r[\wChoose Class\r]\r", "menu_ten_f")

        menu_additem(menu_ten, C_ONE_TEN, "1", 0)
        menu_additem(menu_ten, C_TWO_TEN, "2", 0)
        menu_additem(menu_ten, C_THREE_TEN, "3", 0)

        menu_setprop(menu_ten, MPROP_EXIT, MEXIT_ALL)

        menu_display(id, menu_ten, 0)
}

public rank_tw(id)
{
        new menu_tw = menu_create("\r[\wChoose Class\r]\r", "menu_tw_f")

        menu_additem(menu_tw, C_ONE_TW, "1", 0)
        menu_additem(menu_tw, C_TWO_TW, "2", 0)
        menu_additem(menu_tw, C_THREE_TW, "3", 0)

        menu_setprop(menu_tw, MPROP_EXIT, MEXIT_ALL)

        menu_display(id, menu_tw, 0)
}

public menu_ten_f(id, menu_ten, item_ten)
{
        if( item_ten == MENU_EXIT )
        {
                menu_destroy(menu_ten)
                return PLUGIN_HANDLED
        }
        new data_ten[6], iName_ten[64]
        new acces_ten, callback_ten
        menu_item_getinfo(menu_ten, item_ten, acces_ten, data_ten,5, iName_ten, 63, callback_ten)

        new key_ten = str_to_num(data_ten)

        switch( key_ten )
        {
                case 1: SetPlayerClass(id, 1)
                case 2: SetPlayerClass(id, 2)
                case 3: SetPlayerClass(id, 3)
        }

        menu_destroy(menu_ten)
        return PLUGIN_HANDLED
}

public menu_tw_f(id, menu_tw, item_tw)
{
        if( item_tw == MENU_EXIT )
        {
                menu_destroy(menu_tw)
                return PLUGIN_HANDLED
        }

        new data_tw[6], iName_tw[64]
        new acces_tw, callback_tw

        menu_item_getinfo(menu_tw, item_tw, acces_tw, data_tw,5, iName_tw, 63, callback_tw)

        new key_tw = str_to_num(data_tw)

        switch( key_tw )
        {
                case 1: SetPlayerClass(id, 4)
                case 2: SetPlayerClass(id, 5)
                case 3: SetPlayerClass(id, 6)
        }

        menu_destroy(menu_tw)
        return PLUGIN_HANDLED
}


supertrio17 06-19-2020 22:58

Re: Menu showing twice?
 
Sorry if I'm being rude, but I don't really see how is this helpful, you did make my code look prettier, but that won't change the income.

fysiks 06-19-2020 23:00

Re: Menu showing twice?
 
The code I posted behaves very differently from yours, I removed many mistakes and created more modular code. Try it out and see for yourself. Once you see that it's better, you can add back in your nVault stuff if you really think you want it.

supertrio17 06-19-2020 23:09

Re: Menu showing twice?
 
Quote:

Originally Posted by fysiks (Post 2706488)
The code I posted behaves very differently from yours, I removed many mistakes and created more modular code. Try it out and see for yourself. Once you see that it's better, you can add back in your nVault stuff if you really think you want it.

I looked at your code, and I don't really see anything different work wise, and I didn't use "level" for a reason, it doesn't work.

fysiks 06-19-2020 23:19

Re: Menu showing twice?
 
Quote:

Originally Posted by supertrio17 (Post 2706490)
I looked at your code, and I don't really see anything different work wise

Then you didn't look hard enough. It is radically different from your code.

Quote:

Originally Posted by supertrio17 (Post 2706490)
I didn't use "level" for a reason, it doesn't work.

If "level" provided by crxranks_user_level_updated() doesn't work then that's a bug with the crxranks plugin. You can easily replace it with crxranks_get_user_level(). If crxranks_user_level_updated() doesn't work as it was designed, that's not my fault. You should report these issues to the author, probably best to create an issue here.

ZaX 06-20-2020 08:11

Re: Menu showing twice?
 
Quote:

Originally Posted by fysiks (Post 2706492)
Then you didn't look hard enough. It is radically different from your code.



If "level" provided by crxranks_user_level_updated() doesn't work then that's a bug with the crxranks plugin. You can easily replace it with crxranks_get_user_level(). If crxranks_user_level_updated() doesn't work as it was designed, that's not my fault. You should report these issues to the author, probably best to create an issue here.

He is using nvault to save users choosen character from menu and load it every map. im not sure though but thats what i understood

OciXCrom 06-20-2020 08:22

Re: Menu showing twice?
 
The "level" parameter passed in "crxranks_user_level_updated" works just fine. None of the sub-plugins which you're probably using at the moment wouldn't have worked if the parameter was wrong.

Natsheh 06-20-2020 09:05

Re: Menu showing twice?
 
Since you've alot of similarity in the code you can really shorten your code it will help to understand the code more easily.

And also optimize the code by creating the menu just once


All times are GMT -4. The time now is 16:54.

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