View Single Post
Author Message
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 01-18-2020 , 10:54   KeyValues to menu writing
Reply With Quote #1

Hey!

I tried to get values from the kv file and add them as item in menu but didn't work!

I have this code:
PHP Code:
bool g_isPlayerOn[MAXPLAYERS 1];

char clientSound[512];

char file[512];

...

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_music"Command_Music);
    
RegConsoleCmd("sm_stop"Command_Stop);
    
    
BuildPath(Path_SMfilesizeof(file), "configs/musics.txt");
}

public 
Action Command_Music(int clientint args)
{
    
musicsMenu().Display(clientMENU_TIME_FOREVER);
    return 
Plugin_Handled;
}

Menu musicsMenu()
{
    
Menu menu = new Menu(MusicHandleMENU_ACTIONS_ALL);
    
menu.SetTitle("Choose a type of song:");
    
    
KeyValues kv = new KeyValues("Musics");
    
kv.ImportFromFile(file);
    
    
char SongNumber[32];
    
char SongName[128];
    
    do
    {
        
KvGetSectionName(kvSongNumbersizeof(SongNumber));
        
KvGetString(kv"name"SongNamesizeof(SongName));
        
        
//Add all songs to the menu
        
menu.AddItem(SongNumberSongName);
    } while (
KvGotoNextKey(kv));
    
    
delete kv;
    
menu.ExitButton true;
    
menu.ExitBackButton true;
    
    return 
menu;
}

public 
int MusicHandle(Menu menuMenuAction actionint clientint item)
{
    if(
action == MenuAction_Select)
    {
        if(
g_isPlayerOn[client])
        {
            
PrintToChat(client"[SpirT] Tens de parar a música primeiro usando o !stop");
        }
        
        
char choice[32];
        
char buffer[32];
        
        
KeyValues kv = new KeyValues("Musics");
        
kv.ImportFromFile(file);
        
        if(!
KvGotoFirstSubKey(kv))
        {
            
delete menu;
        }
        
        
menu.GetItem(itemchoicesizeof(choice));
        
        do
        {
            
KvGetSectionName(kvbuffersizeof(buffer));
            
            if(
StrEqual(choicebuffer))    
            {    
                
char soundPath[512];
                
KvGetString(kv"file"soundPathsizeof(soundPath));
                
EmitSoundToClient(clientsoundPath);
                
soundPath clientSound;
                
g_isPlayerOn[client] = true;
            }    
        } while (
KvGotoNextKey(kv));
    }
}

public 
Action Command_Stop(int clientint args)
{
    if(!
g_isPlayerOn[client])
    {
        
PrintToChat(client"[SpirT] Nenhuma música está a tocar!");
        return 
Plugin_Handled;
    }
    
    
StopSound(clientSNDCHAN_AUTOclientSound);
    
g_isPlayerOn[client] = false;
    return 
Plugin_Handled;

Now the data on the file (musics.txt):
Code:
"Musics"
{
	"1"
	{
		"name" "PRIMEIRA"
	}
	"2"
	{
		"name" "SEGUNDA"
	}
}
What can I do to solve my problem?

Best Regards,

SpirT.
__________________

Last edited by SpirT; 01-18-2020 at 14:11. Reason: Keep going...
SpirT is offline