Raised This Month: $2 Target: $400
 0% 

Menu just fade out after chosing an option.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JPTRON
Junior Member
Join Date: Jun 2020
Old 06-15-2020 , 14:19   Menu just fade out after chosing an option.
Reply With Quote #1

Hi,

I'm trying to code a basic paged menu where people can choose a knife to play with. (no skins)

When I call the command the menu shows on, but when I select something, it just fades out. I can't even change the page.

PHP Code:
enum Slots
{
    
Slot_Primary,
    
Slot_Secondary,
    
Slot_Knife,
    
Slot_Grenade,
    
Slot_C4,
    
Slot_None
};

Menu g_KnifeMenu null;

stock bool IsValidClient(int client)
{
    return (
client >= && client <= MaxClients && !IsFakeClient(client) && IsClientConnected(client) && IsClientInGame(client) && !IsClientSourceTV(client));
}

stock bool RemoveWeaponBySlot(int clientSlots slot)
{
    
int entity_index GetPlayerWeaponSlot(clientslot);
    if(
entity_index 0)
    {
        
RemovePlayerItem(cliententity_index);
        
AcceptEntityInput(entity_index"Kill");
        return 
true;
    }
    return 
false;
}

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_knife"cmd_knife);
}

public 
Action cmd_knife(int clientint args)
{
    if(
IsValidClient(client) && IsPlayerAlive(client) && GetClientTeam(client) == CS_TEAM_CT)
    {    
        
g_KnifeMenu BuildKnifeMenu();
        
g_KnifeMenu.Display(clientMENU_TIME_FOREVER);
    }
    
    
delete g_KnifeMenu;
    return 
Plugin_Handled;
}

Menu BuildKnifeMenu()
{
    
File file OpenFile("knives.txt""rt");
    if(
file == null)
    {
        return 
null;
    }
    
    
Menu menu = new Menu(ChangeKnifeMenu);
    
char knifename[255];
    
    while(!
file.EndOfFile() && file.ReadLine(knifenamesizeof(knifename)))
    {
        if (
knifename[0] == ';' || !IsCharAlpha(knifename[0]))
        {
            continue;
        }
        
        
int len strlen(knifename);
        
        for (
int i 0leni++)
        {
            if (
IsCharSpace(knifename[i]))
            {
                
knifename[i] = '\0';
                break;
            }
        }
        
        
menu.AddItem(knifenameknifename);
    }
    
    
file.Close();
    
    
menu.SetTitle("Knife:");
    
    return 
menu;
}

public 
int ChangeKnifeMenu(Menu menuMenuAction actionint param1int param2)
{
    if (
action == MenuAction_Select)
    {
        
char info[32];
        
char knife[32];
        
char givestring[32];
 
        
bool found menu.GetItem(param2infosizeof(info));
 
        
PrintToChat(param1"\x01 \x0E%s \x08equipped"info);
         
         
RemoveWeaponBySlot(param1Slot_Knife);
     
             if(
StrEqual(info,"CT"))
             {
                 
GivePlayerItem(param1"weapon_knife");
             }
             else if(
StrEqual(info,"T"))
             {
                 
GivePlayerItem(param1"weapon_knife_t");
             }
             else if(
StrEqual(info,"Classic"))
             {
                 
knife "css";
             }
             else if(
StrEqual(info,"m9 bayonet"))
             {
                 
knife "m9_bayonet";
             }
             else if(
StrEqual(info,"huntsman"))
             {
                 
knife "tactical";
             }
             else if(
StrEqual(info,"shadow daggers"))
             {
                 
knife "push";
             }
             else if(
StrEqual(info,"golden"))
             {
                 
knife "knifegg";
             }
             else if(
StrEqual(info,"bowie"))
             {
                 
knife "survival_bowie";
             }
             else if(
StrEqual(info,"navaja"))
             {
                 
knife "gypsy_jackknife";
             }
             else if(
StrEqual(info,"talon"))
             {
                 
knife "widowmaker";
             }
             else if(
StrEqual(info,"spectral"))
             {
                 
knife "ghost";
             }
             else if(
StrEqual(info,"survival"))
             {
                 
knife "canis";
             }
             else if(
StrEqual(info,"paracord"))
             {
                 
knife "cord";
             }
             else if(
StrEqual(info,"nomad"))
             {
                 
knife "outdoor";
             }
                             
         
Format(givestringsizeof(givestring), "weapon_knife_%s"knife);
         
         if(!
StrEqual(info,"CT") && !StrEqual(info,"T"))
         {
             
GivePlayerItem(param1givestring);
         }       
    }

JPTRON is offline
kratoss1812
Senior Member
Join Date: May 2018
Location: Romānia
Old 06-15-2020 , 16:06   Re: Menu just fade out after chosing an option.
Reply With Quote #2

Ok, first of all, remember that reading a file is an expensive operation and you should create your menu OnMapStart().
My guess is that you are deleting your menu after you displayed it so you are deleting the handler as well?

any way try this, didn't tested it.
PHP Code:
Menu g_KnifeMenu;

stock bool IsValidClient(int client)
{
    return (
client >= && client <= MaxClients && !IsFakeClient(client) && IsClientConnected(client) && IsClientInGame(client) && !IsClientSourceTV(client));
}

stock bool RemoveWeaponBySlot(int clientSlots slot)
{
    
int entity_index GetPlayerWeaponSlot(clientslot);
    if(
entity_index 0)
    {
        
RemovePlayerItem(cliententity_index);
        
AcceptEntityInput(entity_index"Kill");
        return 
true;
    }
    return 
false;
}

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_knife"cmd_knife);
}

public 
void OnMapStart()
{
    
g_KnifeMenu BuildKnifeMenu();
}

public 
Action cmd_knife(int clientint args)
{
    if(
IsValidClient(client) && IsPlayerAlive(client) && GetClientTeam(client) == CS_TEAM_CT)
    {    
        
g_KnifeMenu.Display(clientMENU_TIME_FOREVER);
    }
    return 
Plugin_Handled;
}

Menu BuildKnifeMenu()
{
    
File file OpenFile("knives.txt""rt");
    if(
file == null)
    {
        return 
null;
    }
    
    
Menu menu = new Menu(ChangeKnifeMenu);
    
char knifename[255];
    
    while(!
file.EndOfFile() && file.ReadLine(knifenamesizeof(knifename)))
    {
        if (
knifename[0] == ';' || !IsCharAlpha(knifename[0]))
        {
            continue;
        }
        
        
int len strlen(knifename);
        
        for (
int i 0leni++)
        {
            if (
IsCharSpace(knifename[i]))
            {
                
knifename[i] = '\0';
                break;
            }
        }
        
        
menu.AddItem(knifenameknifename);
    }
    
    
file.Close();
    
    
menu.SetTitle("Knife:");
    
    return 
menu;
}

public 
int ChangeKnifeMenu(Menu menuMenuAction actionint param1int param2)
{
    if (
action == MenuAction_Select)
    {
        
char info[32];
        
char knife[32];
        
char givestring[32];
        
        
bool found menu.GetItem(param2infosizeof(info));
        
        
PrintToChat(param1"\x01 \x0E%s \x08equipped"info);
        
        
RemoveWeaponBySlot(param1Slot_Knife);
        
         if(
StrEqual(info,"CT"))
         {
             
GivePlayerItem(param1"weapon_knife");
         }
         else if(
StrEqual(info,"T"))
         {
             
GivePlayerItem(param1"weapon_knife_t");
         }
         else if(
StrEqual(info,"Classic"))
         {
             
knife "css";
         }
         else if(
StrEqual(info,"m9 bayonet"))
         {
             
knife "m9_bayonet";
         }
         else if(
StrEqual(info,"huntsman"))
         {
             
knife "tactical";
         }
         else if(
StrEqual(info,"shadow daggers"))
         {
             
knife "push";
         }
         else if(
StrEqual(info,"golden"))
         {
             
knife "knifegg";
         }
         else if(
StrEqual(info,"bowie"))
         {
             
knife "survival_bowie";
         }
         else if(
StrEqual(info,"navaja"))
         {
             
knife "gypsy_jackknife";
         }
         else if(
StrEqual(info,"talon"))
         {
             
knife "widowmaker";
         }
         else if(
StrEqual(info,"spectral"))
         {
             
knife "ghost";
         }
         else if(
StrEqual(info,"survival"))
         {
             
knife "canis";
         }
         else if(
StrEqual(info,"paracord"))
         {
             
knife "cord";
         }
         else if(
StrEqual(info,"nomad"))
         {
             
knife "outdoor";
         }
                         
        
Format(givestringsizeof(givestring), "weapon_knife_%s"knife);
        
        if(!
StrEqual(info,"CT") && !StrEqual(info,"T"))
        {
             
GivePlayerItem(param1givestring);
        }       
    }

__________________
kratoss1812 is offline
JPTRON
Junior Member
Join Date: Jun 2020
Old 06-15-2020 , 17:36   Re: Menu just fade out after chosing an option.
Reply With Quote #3

Quote:
Originally Posted by kratoss1812 View Post
Ok, first of all, remember that reading a file is an expensive operation and you should create your menu OnMapStart().
My guess is that you are deleting your menu after you displayed it so you are deleting the handler as well?

any way try this, didn't tested it.
PHP Code:
Menu g_KnifeMenu;

stock bool IsValidClient(int client)
{
    return (
client >= && client <= MaxClients && !IsFakeClient(client) && IsClientConnected(client) && IsClientInGame(client) && !IsClientSourceTV(client));
}

stock bool RemoveWeaponBySlot(int clientSlots slot)
{
    
int entity_index GetPlayerWeaponSlot(clientslot);
    if(
entity_index 0)
    {
        
RemovePlayerItem(cliententity_index);
        
AcceptEntityInput(entity_index"Kill");
        return 
true;
    }
    return 
false;
}

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_knife"cmd_knife);
}

public 
void OnMapStart()
{
    
g_KnifeMenu BuildKnifeMenu();
}

public 
Action cmd_knife(int clientint args)
{
    if(
IsValidClient(client) && IsPlayerAlive(client) && GetClientTeam(client) == CS_TEAM_CT)
    {    
        
g_KnifeMenu.Display(clientMENU_TIME_FOREVER);
    }
    return 
Plugin_Handled;
}

Menu BuildKnifeMenu()
{
    
File file OpenFile("knives.txt""rt");
    if(
file == null)
    {
        return 
null;
    }
    
    
Menu menu = new Menu(ChangeKnifeMenu);
    
char knifename[255];
    
    while(!
file.EndOfFile() && file.ReadLine(knifenamesizeof(knifename)))
    {
        if (
knifename[0] == ';' || !IsCharAlpha(knifename[0]))
        {
            continue;
        }
        
        
int len strlen(knifename);
        
        for (
int i 0leni++)
        {
            if (
IsCharSpace(knifename[i]))
            {
                
knifename[i] = '\0';
                break;
            }
        }
        
        
menu.AddItem(knifenameknifename);
    }
    
    
file.Close();
    
    
menu.SetTitle("Knife:");
    
    return 
menu;
}

public 
int ChangeKnifeMenu(Menu menuMenuAction actionint param1int param2)
{
    if (
action == MenuAction_Select)
    {
        
char info[32];
        
char knife[32];
        
char givestring[32];
        
        
bool found menu.GetItem(param2infosizeof(info));
        
        
PrintToChat(param1"\x01 \x0E%s \x08equipped"info);
        
        
RemoveWeaponBySlot(param1Slot_Knife);
        
         if(
StrEqual(info,"CT"))
         {
             
GivePlayerItem(param1"weapon_knife");
         }
         else if(
StrEqual(info,"T"))
         {
             
GivePlayerItem(param1"weapon_knife_t");
         }
         else if(
StrEqual(info,"Classic"))
         {
             
knife "css";
         }
         else if(
StrEqual(info,"m9 bayonet"))
         {
             
knife "m9_bayonet";
         }
         else if(
StrEqual(info,"huntsman"))
         {
             
knife "tactical";
         }
         else if(
StrEqual(info,"shadow daggers"))
         {
             
knife "push";
         }
         else if(
StrEqual(info,"golden"))
         {
             
knife "knifegg";
         }
         else if(
StrEqual(info,"bowie"))
         {
             
knife "survival_bowie";
         }
         else if(
StrEqual(info,"navaja"))
         {
             
knife "gypsy_jackknife";
         }
         else if(
StrEqual(info,"talon"))
         {
             
knife "widowmaker";
         }
         else if(
StrEqual(info,"spectral"))
         {
             
knife "ghost";
         }
         else if(
StrEqual(info,"survival"))
         {
             
knife "canis";
         }
         else if(
StrEqual(info,"paracord"))
         {
             
knife "cord";
         }
         else if(
StrEqual(info,"nomad"))
         {
             
knife "outdoor";
         }
                         
        
Format(givestringsizeof(givestring), "weapon_knife_%s"knife);
        
        if(!
StrEqual(info,"CT") && !StrEqual(info,"T"))
        {
             
GivePlayerItem(param1givestring);
        }       
    }

Wow, that helped me a lot, I thought that "OnMapStart" wouldn't be necessary, I was wrong.
So, now it lets me change the page and when I select the knife, shows the message, but doesn't give it to me.
I assume that the problem is here:
PHP Code:
Format(givestringsizeof(givestring), "weapon_knife_%s"knife);
        
        if(!
StrEqual(info,"CT") && !StrEqual(info,"T"))
        {
             
GivePlayerItem(param1givestring);
        } 
Even "CT" knife that I just need to give "weapon_knife" isn't working.
JPTRON is offline
kratoss1812
Senior Member
Join Date: May 2018
Location: Romānia
Old 06-15-2020 , 18:02   Re: Menu just fade out after chosing an option.
Reply With Quote #4

you could help me to understant about this situation by providing me the cfg you're creating your menu from, also the chat message would be usefull

check in your core.cfg if you have the "FollowCSGOServerGuidelines" setted as "no" and "BlockBadPlugins" also on "no"

Edit, I just saw that you are not equipting player the given weapon
try this
PHP Code:
int ItemIndex;
if(
StrEqual(info,"CT"))
{
    
ItemIndex GivePlayerItem(param1"weapon_knife");
    if(
ItemIndex != -1)
         
EquipPlayerWeapon(param1ItemIndex);

__________________

Last edited by kratoss1812; 06-15-2020 at 18:04.
kratoss1812 is offline
JPTRON
Junior Member
Join Date: Jun 2020
Old 06-15-2020 , 19:08   Re: Menu just fade out after chosing an option.
Reply With Quote #5

Quote:
Originally Posted by kratoss1812 View Post
you could help me to understant about this situation by providing me the cfg you're creating your menu from, also the chat message would be usefull

check in your core.cfg if you have the "FollowCSGOServerGuidelines" setted as "no" and "BlockBadPlugins" also on "no"

Edit, I just saw that you are not equipting player the given weapon
try this
PHP Code:
int ItemIndex;
if(
StrEqual(info,"CT"))
{
    
ItemIndex GivePlayerItem(param1"weapon_knife");
    if(
ItemIndex != -1)
         
EquipPlayerWeapon(param1ItemIndex);

Adding that code logs an error:
Code:
Attempted to create unknown entity type weapon_knife_!
NULL Ent in GiveNamedItem!
Seems that the plugin does not concatenate the strings using the "Format" function...

EDIT:
It gives the CT and T knives.

EDIT2:
I changed the code to this:
PHP Code:
if(StrEqual(info,"CT"))
         {
             
ItemIndex GivePlayerItem(param1"weapon_knife");
             if(
ItemIndex != -1){
                
EquipPlayerWeapon(param1ItemIndex);
            }  
         }
         else if(
StrEqual(info,"T"))
         {
             
knife "t";
         }
         else if(
StrEqual(info,"Classic"))
         {
             
knife "css";
         }
         else if(
StrEqual(info,"m9 bayonet"))
         {
             
knife "m9_bayonet";
         }
         else if(
StrEqual(info,"huntsman"))
         {
             
knife "tactical";
         }
         else if(
StrEqual(info,"shadow daggers"))
         {
             
knife "push";
         }
         else if(
StrEqual(info,"bowie"))
         {
             
knife "survival_bowie";
         }
         else if(
StrEqual(info,"navaja"))
         {
             
knife "gypsy_jackknife";
         }
         else if(
StrEqual(info,"talon"))
         {
             
knife "widowmaker";
         }
         else if(
StrEqual(info,"survival"))
         {
             
knife "canis";
         }
         else if(
StrEqual(info,"paracord"))
         {
             
knife "cord";
         }
         else if(
StrEqual(info,"nomad"))
         {
             
knife "outdoor";
         }
                         
        
Format(givestringsizeof(givestring), "weapon_knife_%s"knife);
        
        if(!
StrEqual(info,"CT"))
        {
             
ItemIndex GivePlayerItem(param1givestring);
             if(
ItemIndex != -1){
                
EquipPlayerWeapon(param1ItemIndex);
            } 
        } 
Weird because "T" knife keeps being given but not the others...

Last edited by JPTRON; 06-15-2020 at 19:18.
JPTRON is offline
kratoss1812
Senior Member
Join Date: May 2018
Location: Romānia
Old 06-16-2020 , 04:26   Re: Menu just fade out after chosing an option.
Reply With Quote #6

debug it
__________________
kratoss1812 is offline
Silvers
AlliedModders Donor
Join Date: Aug 2010
Location: SpaceX
Old 06-16-2020 , 05:01   Re: Menu just fade out after chosing an option.
Reply With Quote #7

Quote:
Originally Posted by kratoss1812 View Post
and "BlockBadPlugins" also on "no"
Why would you recommend this? That's bad advice. BlockBadPlugins prevents servers loading known plugins that contain backdoors or other malicious code. Keep this option set to "Yes".


If your menu is static (unchanging) then just create once and store the menu handle in a global Menu variable, don't delete the handle in the menu callback, and when an option is selected .Display() the menu again to the client. If it's dynamic, you should still only load the config once and store the data somehow, or possibly create the menu and as before store in a global variable making changes to the menu as you need without destroying it, for example hiding elements, or renaming parts before displaying again. Haven't looked through the source, just saying.
__________________

Last edited by Silvers; 06-16-2020 at 05:03.
Silvers is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 06-16-2020 , 05:45   Re: Menu just fade out after chosing an option.
Reply With Quote #8

You can also use Menu.DisplayAt inside MenuAction_Select action so your menu will display again on current page.

Code:
public int MenuHandler_Test(Menu menu, MenuAction action, int param1, int param2) {
	if ( action == MenuAction_Select ) { // param1 in this action is client index and param2 is pressed item index
		// your code
		menu.DisplayAt( param1, menu.Selection, MENU_TIME_FOREVER ); // .Selection returns the first item index on the page
	} else if ( action == MenuAction_End ) { // param1 in this action is MenuEnd_* reason
		if ( param1 != MenuEnd_Selected ) // We don't want to delete our menu when client pressed on any item (excluding Exit and ExitBack buttons)
			menu.Close();
	}
}
__________________
MAGNAT2645 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 11:29.


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