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

[Question] Keyvalues GRRR!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 09-13-2007 , 12:25   [Question] Keyvalues GRRR!
Reply With Quote #1

Heya,
I've been having difficulties with keyvalues ( still trying to learn how to use them in SourceMod ). What I'm trying to do is write each new player that joins the game during a map to a file, so I have a list of players and the needed relevant info for those players in a keyvalue file. The problem I'm experiencing is this:

1). It's not writing into a subgroup. Instead it's just writing to the root.

This is what I want it to do
Code:
"roundPlayers"
{
    "apocalyptica:24"
    {
        "IsInfected"        "0"
    }
    "apocalyptica:25"
    {
        "IsInfected"        "0"
    }
    "apocalyptica:26"
    {
        "IsInfected"        "0"
    }
}
and this is what it's doing

Code:
"apocalyptica:26"
{
    "IsInfected"        "0"
}
2). It's overwriting each previous player with the new player. Example, there's 5 players that joined and the keyvalues file is saying the following.
Code:
"apocalyptica:26"
{
    "IsInfected"        "0"
}
This is my code. I just have it hooking player_activate right now.

Code:
public Action:HookPlayerActivate(Handle:event, const String:name[], bool:dontBroadcast)
{
    new userID = GetEventInt(event,"userid");

    
    decl String:userString[64];
    Format(userString,sizeof(userString),"apocalyptica:%i",userID);
    
    
    new Handle:roundPlayersConfig = CreateKeyValues("roundPlayers");
    decl String:path[PLATFORM_MAX_PATH];
    BuildPath(Path_SM,path,PLATFORM_MAX_PATH,"configs/km/es_round_players.txt");
    FileToKeyValues(roundPlayersConfig,path);
    
    KvGotoNextKey(roundPlayersConfig);
    if(KvJumpToKey(roundPlayersConfig,userString,false))
        return;
    
    KvJumpToKey(roundPlayersConfig,userString,true);
    KvSetNum(roundPlayersConfig,"IsInfected",0);
    KeyValuesToFile(roundPlayersConfig,path); 
    CloseHandle(roundPlayersConfig);
}
Anywho I was hoping someone could please tell me what I'm doing wrong.
Thanks for your time.

Last edited by SamuraiBarbi; 09-13-2007 at 12:29.
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
dalto
Veteran Member
Join Date: Jul 2007
Old 09-13-2007 , 12:35   Re: [Question] Keyvalues GRRR!
Reply With Quote #2

So, your problems are here.

Code:
if(KvJumpToKey(roundPlayersConfig,userString,false))
        return;
This says if the key does not exist than exit. There are two problems with this. First, if the key does not exist then you will exit the function and never get to the code that creates it. Second, you are not closing the roundPlayersConfig handle in this case and will leak handles.

Just remove the above code. It is not needed since the next line
Code:
KvJumpToKey(roundPlayersConfig,userString,true);
Does exactly what you want. It goes to the key if it exists and creates it if it doesn't.

Also, one a side note, do you want to store steam id's instead of userid's?
dalto is offline
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 09-13-2007 , 13:28   Re: [Question] Keyvalues GRRR!
Reply With Quote #3

Oooo, ok dalto, I'll give that a shot. Woot! For this particular list I cannot use steamids because it must keep track of the bot players too and the bot's steam id's are all BOT. I'll let ya know how this turns out. =D
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 09-13-2007 , 14:15   Re: [Question] Keyvalues GRRR!
Reply With Quote #4

Alrighty, I made the changes you suggested and while they solved the problems you mentioned, I'm still experiencing the problems in my original post.

Here's the updated code:

Code:
public Action:HookPlayerActivate(Handle:event, const String:name[], bool:dontBroadcast)
{
    new userID = GetEventInt(event,"userid");

    
    decl String:userString[64];
    Format(userString,sizeof(userString),"apocalyptica:%i",userID);
    
    
    new Handle:roundPlayersConfig = CreateKeyValues("roundPlayers");
    decl String:path[PLATFORM_MAX_PATH];
    BuildPath(Path_SM,path,PLATFORM_MAX_PATH,"configs/km/es_round_players.txt");
    FileToKeyValues(roundPlayersConfig,path);
    
    KvGotoNextKey(roundPlayersConfig);
    
    KvJumpToKey(roundPlayersConfig,userString,true);
    KvSetNum(roundPlayersConfig,"IsInfected",0);
    KeyValuesToFile(roundPlayersConfig,path); 
    CloseHandle(roundPlayersConfig);
}
Any ideas?
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
dalto
Veteran Member
Join Date: Jul 2007
Old 09-13-2007 , 16:49   Re: [Question] Keyvalues GRRR!
Reply With Quote #5

Sorry, I misread your post. Unload the plugin, delete the keyvalues file and remove this line:

Code:
KvGotoNextKey(roundPlayersConfig);
Also, just a thought, but do you really want to load the file into memory and then put it back to disk everytime that event is called? I would consider just loading it OnPluginStart() and saving it to disk periodically.

Last edited by dalto; 09-13-2007 at 16:51.
dalto is offline
BAILOPAN
Join Date: Jan 2004
Old 09-13-2007 , 23:14   Re: [Question] Keyvalues GRRR!
Reply With Quote #6

player_activate isn't too bad of an event as long as they file isn't very big.
__________________
egg
BAILOPAN is offline
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 09-14-2007 , 01:28   Re: [Question] Keyvalues GRRR!
Reply With Quote #7

i don't mean to be a pain guys but i still can't get rid of the problems i stated in my first post. is there something that got overlooked?
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
SamuraiBarbi
Senior Member
Join Date: Aug 2006
Location: United States
Old 09-22-2007 , 14:24   Re: [Question] Keyvalues GRRR!
Reply With Quote #8

I'm still having the same problem from the original post. Is there anyone that could please try the code out and see if they know why it's still being a bugger?
SamuraiBarbi is offline
Send a message via AIM to SamuraiBarbi Send a message via MSN to SamuraiBarbi Send a message via Yahoo to SamuraiBarbi
Nican
Veteran Member
Join Date: Jan 2006
Location: NY
Old 09-22-2007 , 14:43   Re: [Question] Keyvalues GRRR!
Reply With Quote #9

http://wiki.alliedmods.net/KeyValues...d_Scripting%29

PHP Code:
public Action:HookPlayerActivate(Handle:event, const String:name[], bool:dontBroadcast){
    new 
userID GetEventInt(event,"userid");

    
decl String:userString[64];
    
Format(userString,sizeof(userString),"apocalyptica:%i",userID);
    
    
    new 
Handle:roundPlayersConfig CreateKeyValues("roundPlayers");
    
decl String:path[PLATFORM_MAX_PATH];
    
BuildPath(Path_SM,path,PLATFORM_MAX_PATH,"configs/km/es_round_players.txt");
    
FileToKeyValues(roundPlayersConfig,path);

    if(!
KvJumpToKey(roundPlayersConfig,userString)){
        
CloseHandle(roundPlayersConfig);
        return;
    }

    
KvSetNum(roundPlayersConfig,"IsInfected",0);
    
KeyValuesToFile(roundPlayersConfig,path); 
    
CloseHandle(roundPlayersConfig);

2. I HIGHLY recommend you not doing that, just use a normal array instead:
PHP Code:
new bool:IsInfected[MAXPLAYERS 1];

public 
Action:HookPlayerActivate(Handle:event, const String:name[], bool:dontBroadcast){
    new 
userID GetEventInt(event,"userid");
    
    
IsInfecteduserID ] = true;

__________________
http://www.nican132.com
I require reputation!
Nican is offline
Send a message via ICQ to Nican Send a message via MSN to Nican
dalto
Veteran Member
Join Date: Jul 2007
Old 09-22-2007 , 17:29   Re: [Question] Keyvalues GRRR!
Reply With Quote #10

They can't easily be stored in array because they are userid's NOT client indexes so they grow over time and are not limited to MAXPLAYERS
dalto is offline
Reply



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 20:34.


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