Raised This Month: $ Target: $400
 0% 

Question about creating subkeys...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
NatalyaAF
Senior Member
Join Date: Dec 2008
Old 03-25-2010 , 22:26   Question about creating subkeys...
Reply With Quote #1

I have a plugin where players can buy cars. It saves their car's information and stuff to a key value thing so that when they come back later it remembers that they bought a car. However the cars have a few values associated with them such as what skin they should use and stuff, so each car needs a number of sub keys. Here's an example what it looks like:

PHP Code:
"Cars"
{
    
"STEAM_0:1:XXXXXXXX"
    
{
        
"Cars"        "0"
    
}
    
"STEAM_0:1:XXXXXXXX"
    
{
        
"Cars"        "2"
        "Lada 1300"
        
{
            
"skin"        "6"
            "stowed"        "0"
            "index"        "0"
        
}
        
"Chevrolet Impala"
        
{
            
"skin"        "1"
            "stowed"        "1"
            "index"        "1"
        
}
    }

Okay, this all works and stuff for most things, but I don't know how to let players buy a new car because the car needs sub-keys as u can see. So like what happened was this:

PHP Code:
"Cars"
{
    
"STEAM_0:1:XXXXXXXX"
    
{
        
"Cars"        "0"
    
}
    
"STEAM_0:1:XXXXXXXX"
    
{
        
"Cars"        "3"
        "Lada 1300"
        
{
            
"skin"        "6"
            "stowed"        "0"
            "index"        "0"
        
}
        
"Chevrolet Impala"
        
{
            
"skin"        "4"
            "stowed"        "1"
            "index"        "1"
            "Lada 1300"
            
{
                
"skin"        "4"
                "stowed"        "1"
                "index"        "2"
            
}
        }
    }

Here is my code:

PHP Code:
public Menu_Buy(Handle:menuMenuAction:actionparam1param2)
{
    
// user has selected to buy something

    
if (action == MenuAction_Select)
    {
        new 
String:info[30];
        new 
String:type[30];

        
/* Get item info */
        
new bool:found GetMenuItem(menuparam2infosizeof(info))

        if (!
found)
            return;

        
// user selected an item
        // advance kv to this item (so we can check price)
        
KvJumpToKey(carbuykvinfo)
        
KvGetString(carbuykv"type"typesizeof(type), "0");

        new 
cost KvGetNum(carbuykv,"Price",2000);
        new 
cost2 = (cost 1);


        
// Why is it like this?  I have no idea.
        
new MoneyOffset FindSendPropOffs("CCSPlayer""m_iAccount");
        new 
money GetEntData(param1MoneyOffset);

        if(
money cost2)
        {
            
GetClientAuthString(param1authid[param1], sizeof(authid[]));
            
KvJumpToKey(carinvkvauthid[param1], true);

            new 
cars = (KvGetNum(carinvkv"cars"0));
            if(
cars 10)
            {

                new 
val_2 KvGetNum(carbuykv,"val_2",1);
                
cars += val_2;
                
KvSetNum(carinvkv"cars"cars);
                
cars -= val_2;

                
SetEntData(param1MoneyOffsetmoney cost4true);
                
PrintToChat(param1"\x03[Car] You bought a %s."info);

                
KvGotoFirstSubKey(carinvkvtrue);
                
decl String:buffer[16];
                
KvGetSectionName(carinvkvbuffersizeof(buffer));
                
PrintToChat(param1"\x04[Car DEBUG 0] Section Name = %s"buffer);

                new 
index KvGetNum(carinvkv"index", -1);

                
// Skip to last car in the player's car list.  We make a new key afterwards with the new car.
                
for ( new 0;  carsi++ )
                {
                    
index KvGetNum(carinvkv"index", -1);
                    
KvGotoNextKey(carinvkv);
                }
                
index += 1;

                
KvJumpToKey(carinvkvinfotrue);
                
KvSetNum(carinvkv"skin"0);
                
KvSetNum(carinvkv"stowed"1);
                
KvSetNum(carinvkv"index"index);

                
KvRewind(carinvkv);
                
KvRewind(carbuykv);
            }
            else 
PrintToChat(param1"\x03[Car] You can only own a maximum of 10 cars."cost);
            
KvRewind(carinvkv);
            
KvRewind(carbuykv);
        }
        else 
PrintToChat(param1"\x03[Car] You can't afford this vehicle.  You need $%i to buy it."cost);
        
KvRewind(carbuykv);
        
KvRewind(carinvkv);
    }


Last edited by NatalyaAF; 03-25-2010 at 23:02. Reason: Post complete now.
NatalyaAF is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 03-25-2010 , 23:57   Re: Question about creating subkeys...
Reply With Quote #2

What are you using the "index" value for? Regardless, your key names have to be unique (or they are just overwritten). So if a player has an Impala with skin "1" and they want to buy an Impala with skin "2" your going to run into trouble.

As for your current problem, I think KvGotoFirstSubKey() is adding to the traversal stack so once you have your index value (which I think is what your looking for), you need to use KvGoBack() to return to the correct position.

Maybe...
PHP Code:
new index 0;

if (
KvGotoFirstSubKey(carinvkvtrue)) // if player has any cars
{
    
index++;

    
// Skip to last car in the player's car list.  We make a new key afterwards with the new car.
    
while (KvGotoNextKey(carinvkv))
    {
        
index++;
    }

    
// Set position back to authid key.
    
KvGoBack(carinvkv);
}

KvJumpToKey(carinvkvinfotrue);
KvSetNum(carinvkv"skin"0);
KvSetNum(carinvkv"stowed"1);
KvSetNum(carinvkv"index"index); 
__________________
.
[ 1 Dumerils Boa | 1 Cali King ]...
.
I'm a lil' spirituous.

Last edited by meng; 03-26-2010 at 21:03. Reason: fixed little mistake.
meng is offline
Send a message via Yahoo to meng
NatalyaAF
Senior Member
Join Date: Dec 2008
Old 03-26-2010 , 17:06   Re: Question about creating subkeys...
Reply With Quote #3

The index value is there to detect what car it's doing an operation on. For example, if I had two of the Lada 1300's, and I wanted to change the skin, it wouldn't just use KvJumpToKey to the Lada 1300, rather it cycles through all of the authid's sub keys and searches for the sub-key with index number whatever (they are assigned sequentially starting with 0) so it operates on the right car. I guess what I was trying to do here was have the traversal stack skip to be in position after the last car, then write a new one there, so it wouldn't overwrite any of the cars before it. But I guess I could live with having it limit so people can only own one of each kind of car, because that would make sense anyway given that they can change the colour and stuff even after they buy it.

I'll try the bit you wrote there. But if it doesn't work I can just settle for people only owning one of each kind of car.

*EDIT*

Wow just thought of this... I could have it create a new key with a bogus name, then just re-name it two seconds later. That way I could get it to make keys with the same name. But like is that bad programming?

Last edited by NatalyaAF; 03-26-2010 at 17:23.
NatalyaAF is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 03-26-2010 , 21:02   Re: Question about creating subkeys...
Reply With Quote #4

Quote:
Originally Posted by NatalyaAF View Post
I'll try the bit you wrote there. But if it doesn't work I can just settle for people only owning one of each kind of car.

*EDIT*

Wow just thought of this... I could have it create a new key with a bogus name, then just re-name it two seconds later. That way I could get it to make keys with the same name. But like is that bad programming?
I made a little mistake, edited above. It doesn't fix the issue of naming the keys. Just fixes the structure problem and counts the cars (providing your index).

I wouldn't rename your keys. It would just make it harder for you to keep track of whats what.
__________________
.
[ 1 Dumerils Boa | 1 Cali King ]...
.
I'm a lil' spirituous.
meng is offline
Send a message via Yahoo to meng
NatalyaAF
Senior Member
Join Date: Dec 2008
Old 03-26-2010 , 23:57   Re: Question about creating subkeys...
Reply With Quote #5

Thx for the help meng. In the end I found a simpler way to do it though. I realized that the "cars" value before all the individual car keys was the same as the index of the next car you bought. With that in mind I ended up with this code:

PHP Code:
            new cars = (KvGetNum(carinvkv"cars"0));
            if(
cars 10)
            {
                
SetEntData(param1MoneyOffsetmoney cost4true);
                
PrintToChat(param1"\x03[Car] You bought a %s."info);

                
KvJumpToKey(carinvkv"default"true);

                
KvSetNum(carinvkv"skin"0);
                
KvSetNum(carinvkv"stowed"1);
                
KvSetNum(carinvkv"index"cars);
                
KvSetSectionName(carinvkvinfo);

                
KvGoBack(carinvkv);
                
cars += 1;
                
KvSetNum(carinvkv"cars"cars);

                
KvRewind(carinvkv);
                
KvRewind(carbuykv);
            }
            else 
PrintToChat(param1"\x03[Car] You can only own a maximum of 10 cars."cost); 
Thanks for pointing out the KvGoBack though, that was helpful.



*EDIT*
Okay, so I've run into another issue with this... When the player spawns their car (when they buy it the car is only recorded in their inventory and is not spawned in game) I want there to be a way for the server to know who owns the car. Right now I do this by setting the car's target name to the player's Steam ID. With that in mind, I want there to be a way for the player to delete the car from the game and put it back in their inventory. So, how it works is that when the player uses a command ( !car_stow ) if they're inside the car it sees if the car has their Steam ID attached to it. If the car matches the player, the game knows that it belongs to them. But it doesn't know which car it is. Like if I had 2 Impalas spawned in game and I wanted to stow one of them and they both had the same skin then how can it tell them apart? I have the index value that I could attach to the car as well somehow, but I don't know how I'd do that. If I could attach both the car's index and the player's Steam ID to the car then this would work out. I know I could format a string to include both of those and use that as the car's name, but I don't know how to then later decode the string so that it can match Steam ID's and the car's index.

Last edited by NatalyaAF; 03-27-2010 at 02:01.
NatalyaAF 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 03:04.


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