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:menu, MenuAction:action, param1, param2)
{
// 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(menu, param2, info, sizeof(info))
if (!found)
return;
// user selected an item
// advance kv to this item (so we can check price)
KvJumpToKey(carbuykv, info)
KvGetString(carbuykv, "type", type, sizeof(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(param1, MoneyOffset);
if(money > cost2)
{
GetClientAuthString(param1, authid[param1], sizeof(authid[]));
KvJumpToKey(carinvkv, authid[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(param1, MoneyOffset, money - cost, 4, true);
PrintToChat(param1, "\x03[Car] You bought a %s.", info);
KvGotoFirstSubKey(carinvkv, true);
decl String:buffer[16];
KvGetSectionName(carinvkv, buffer, sizeof(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 i = 0; i < cars; i++ )
{
index = KvGetNum(carinvkv, "index", -1);
KvGotoNextKey(carinvkv);
}
index += 1;
KvJumpToKey(carinvkv, info, true);
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);
}
}