AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Key value help (https://forums.alliedmods.net/showthread.php?t=134204)

bestmeth0ds 08-02-2010 02:10

Key value help
 
Hi everyone I am having some issues with key values. As im still learning. Here is what I have so far.

Code:

#pragma semicolon 1
#include <sourcemod>

public OnPluginStart()
{
    RegConsoleCmd("tester", Tester);
}

public Action:Tester(client,args)
{
    new Handle:hKV = CreateKeyValues("Tlist");

    FileToKeyValues(hKV, "sm_tlist.ini");
    KvJumpToKey(hKV, "Banned");
    KvGotoFirstSubKey(hKV);
   
    decl String:szAuth[32];
    decl String:szReason[64];
    do
    {
        KvGetSectionName(hKV, szAuth, sizeof(szAuth));
        PrintToChatAll("STEAM_ID: %s - REASON: %i", szAuth, KvGetString(hKV, "reason", szReason, sizeof(szReason)));
    }
    while (KvGotoNextKey(hKV));
    CloseHandle(hKV);
}

This code is outputting this.

Code:

STEAMID: Tlist REASON: 1
I dont want that to happen obviously.

Also here is my sm_tlist.ini layout.

Code:

"Tlist"
{
    "Banned"
    {
        "STEAM_0:0:1234"
        {
            "reason"        "free killing"
        }
        "STEAM_0:0:4321"
        {
            "reason"        "gun planting"
        }
    }
}

Any help would be deeply appreciated:).

Monkeys 08-02-2010 02:38

Re: Key value help
 
KvGetString doesn't return a string but stores one.
So call it before the PrintToChat and use %s (instead of %i) to show the szReason string

bestmeth0ds 08-02-2010 04:22

Re: Key value help
 
Ok thank you. But now how do I navigate through my ini? I cant seem to retrieve the steamid of the user nor the reason. Here is the new code. And my ini.

Code:
Code:

#pragma semicolon 1
#include <sourcemod>

public OnPluginStart()
{
    RegConsoleCmd("tester", Tester);
}

public Action:Tester(client,args)
{
    decl String:szAuth[32];
    decl String:szReason[64];
    new Handle:hKV = CreateKeyValues("Tlist");
    FileToKeyValues(hKV, "sm_tlist.ini");
   
   
    KvGetSectionName(hKV, szAuth, sizeof(szAuth));
    KvGotoFirstSubKey(hKV);
    KvGetString(hKV, "reason", szReason, sizeof(szReason));

    PrintToChatAll("STEAM: %s REASON: %s", szAuth, szReason);

    CloseHandle(hKV);
}

INI
Code:

"Tlist"
{
    "Banned"
    {
        "STEAM_0:0:1234"
        {
            "reason"        "free killing"
        }
        "STEAM_0:0:4321"
        {
            "reason"        "gun planting"
        }
    }
}


Monkeys 08-02-2010 04:46

Re: Key value help
 
http://wiki.alliedmods.net/KeyValues...d_Scripting%29

bestmeth0ds 08-02-2010 05:21

Re: Key value help
 
Yeah trust me I read it a hundred times. :wink: But no matter what I cannot catch the steamID. I tried using
Code:

    KvJumpToKey(hKV, "Banned");
    KvGotoFirstSubKey(hKV);
    KvGetSectionName(hKV, szAuth, sizeof(szAuth));
    KvGetString(hKV, "reason", szReason, sizeof(szReason));

But it keeps reading the section name as "Tlist" when Tlist is my root node. And reason is blank obviously.

dirka_dirka 08-02-2010 10:19

Re: Key value help
 
try this:
PHP Code:

public Action:Tester(clientargs) {
    
decl String:szAuth[32];
    
    new 
Handle:hKV CreateKeyValues("Tlist");
    
FileToKeyValues(hKV"sm_tlist.ini");
    
KvJumpToKey(hKV"Banned");

    if (
KvGotoFirstSubKey(hKV)) {    // if there are no entries - give up
        
decl String:szReason[64];
        do {    
// get reason of current key, and print it
            
KvGetString(hKV"reason"szReasonsizeof(szReason));
            
PrintToChatAll("STEAM: %s REASON: %s"szAuthszReason);
        } while 
KvGotoNextKey(hKV);    // if there is no next key, end the loop
    
}
    
CloseHandle(hKV);


if that dont work, you might need to store the # of banned people to use in a for loop.

bestmeth0ds 08-02-2010 13:57

Re: Key value help
 
Thank you very much. Im going to test it now.

bestmeth0ds 08-02-2010 14:07

Re: Key value help
 
Hmm I tried it and nothing appeared when I activated the command. Would it be easier to just take out the Banned section and move everything up on level? example.

Code:

"Tlist"
{
    "STEAM_0:0:1234"
    {
            "reason"        "free killing"
    }
       
    "STEAM_0:0:4321"
    {
            "reason"        "gun planting"
    }
}

And to get the # of banned people would I have to use something with this?

Code:

KvGetSectionSymbol
Sorry I am still a noob at sourcepawn. :wink:

bestmeth0ds 08-03-2010 01:16

Re: Key value help
 
Bump?? Still cant get this to work. =(

rhelgeby 08-03-2010 06:18

Re: Key value help
 
This is complicated, because you need to keep track of where the pointer is and will be. Some kv functions update the pointer and some don't (check the function api docs).

I don't have time right now to explain in detail, but consider this: You need to use KvGoBack sometimes, and set the keyOnly parameter to false in some calls to KvGotoFirstSubKey.

I have some example code, but it's a bit complex since it use a template and dynamic calls:

ConfigMgr_CacheKv in http://code.google.com/p/zombiereloa...po=zr-dev-base - This function loop through the sections in the root thee (in your case it will hit "Banned" only).

GameRules_Cache in http://code.google.com/p/zombiereloa...po=zr-dev-base - This function is called by ConfigMgr_CacheKv for each section right below the root tree. Again, in your case this will only be called once, so sections in the "Banned" tree will be parsed within GameRules_Cache.

I'll try update this later with a better explanation. No time right now, so maybe studying the code might help.


All times are GMT -4. The time now is 14:46.

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