[Include] [INC]Player Inventory[v2] -- Now with Saving/Loading functions
#1

What Is It?
It's an easy to use function-adding include to implement inventory for players.

Functions

Required
InventoryOnDialogResponse(playerid, dialogid, response, inputtext[])
This goes under OnDialogResponse for it to work properly
pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
  InventoryOnDialogResponse(playerid, dialogid, response, inputtext);
  return 1;
}
Non-Required
AddItem(playerid,ItemName[],Amount)
This gives the player the specified item and amount.
pawn Код:
if(!strcmp(cmdtext[1],"additem",true,7))
{
  if(!IsPlayerAdmin(playerid))return 0;
  AddItem(playerid,cmdtext[9],1);
}
Returns 1 if successfull, returns 0 if player inventory is full. -- You can increase the MAX_ITEMS in the include.

RemoveItem(playerid,ItemName[],Amount)
This removes the specified item and amount.
pawn Код:
if(!strcmp(cmdtext[1],"usemedkit",true,7))
{
  if(RemoveItem(playerid,"Medkit",1))SetPlayerHealth(playerid,100.0);
  else SendClientMessage(playerid,0xFFFFFFFF,"You don't have a medkit!");
  return SendClientMessage(playerid,0xFFFFFFFF,"You used a medkit!");
}
Returns 1 if successful, returns 0 if player doesn't have the item.

PlayerHasItem(playerid,ItemName[])
This tells you if a player has an item
pawn Код:
if(!strcmp(cmdtext[1],"usemedkit",true,7))
{
  if(PlayerHasItem(playerid,"Medkit"))
  {
    SetPlayerHealth(playerid,100.0);
    RemoveItem(playerid,"Medkit",1);
  }
  else SendClientMessage(playerid,0xFFFFFFFF,"You don't have a medkit!");
  return SendClientMessage(playerid,0xFFFFFFFF,"You used a medkit!");
}
returns the amount of the item that the player has

GetPlayerItemInfo(playerid,&idx,const ItemName[],len=sizeof(ItemName),&Amount)
Gives you the information pertaining to a single players inventory, much like GetPlayerWeaponData
pawn Код:
if(!strcmp(cmdtext[1],"myitems",true))
{
  new idx,amount,itemname[MAX_ITEM_NAME];
  new tmp[128];
  while(GetPlayerItemInfo(playerid,idx,itemname,_,amount))
  {
    if(strlen(itemname))
    {
      format(tmp,128,"%s -- %d",itemname,amount);
      SendClientMessage(playerid,0xFFFFFFFF,tmp);
    }
  }
  return 1;
}
returns 1 unless &idx reaches a number larger than MAX_ITEMS

ResetPlayerInventory(playerid)
Removes all of a player's inventory
pawn Код:
public OnPlayerConnected(playerid)
{
  ResetPlayerInventory(playerid); //This would be considered redundant for Version 2 because it uses PVars which are reset anyway
  return 1;
}
Doesn't return a number.

ShowInventory(playerid)
Shows the inventory dialog for that player. -- This uses my dialog method so it only uses 1 dialog id throughout the entire script, which is modifiable inside the include.
pawn Код:
if(!strcmp(cmdtext[1],"inventory",true)||!strcmp(cmdtext[1],"inv"))
{
  ShowInventory(playerid);
  return 1;
}
Doesn't return a number

Version 2
SaveInventory(playerid)
Saves the players inventory to "../scriptfiles/Inventory/NAME.inv" so don't forget to create the folder before using
pawn Код:
public OnPlayerDisconnect(playerid)
{
  SaveInventory(playerid);
  return 1;
}
Doesn't return a number

LoadInventory(playerid)
Loads the player's inventory if it exists
pawn Код:
public OnPlayerConnect(playerid) //Probably best placed in an OnPlayerLogin like callback
{
  LoadInventory(playerid);
  return 1;
}
Returns 1 if successful, 0 if the file doesn't exist.

Callbacks

OnPlayerUseItem(playerid,ItemName[])
This is called once a player uses an item in the inventory window.
pawn Код:
public OnPlayerUseItem(playerid,ItemName[])
{
  if(!strcmp(ItemName,"Medkit",true)) //The item's name
  {
    new Float:hp;
    GetPlayerHealth(playerid,hp);
    if(hp>=100)return SendClientMessage(playerid,0xFFFFFFFF,"You have full health.");
    SetPlayerHealth(playerid,(hp>75)?(100):(hp+25));
    RemoveItem(playerid,ItemName,1); //Remove the item once used
    return SendClientMessage(playerid,0xFFFFFFFF,"You used a medkit."); //Returning 1 keeps the Inventory Window open
  }
  return 0; //Returning 0 causes the Inventory Window to close
}
Video

Here's a video made by Torran (JoeDaDude) using my Inventory include on his server.


Example Script

Robbing a random item from a nearby player
pawn Код:
new pPPDelay[MAX_PLAYERS];
public OnPlayerCommandText(playerid,cmdtext[])
{
  if(!cmdtext[1])return 0;
  if(!strcmp(cmdtext[1],"pickpocket",true))
  {
    if(pPPDelay[playerid]>GetTickCount())return SendClientMessage(playerid,0xFF0000FF,"You must wait longer before pickpocketing again.");
    new Float:dist,Float:x,Float:y,Float:z,player=INVALID_PLAYER_ID,Float:px,Float:py,Float:pz,Float:closest=3;
    GetPlayerPos(playerid,x,y,z);
    for(new i;i<MAX_PLAYERS;i++)
    {
      if(!IsPlayerConnected(i)||(i==playerid))continue;
      GetPlayerPos(i,px,py,pz);
      dist=floatsqroot( ((px-x)*(px-x)) + ((py-y)*(py-y)) + ((pz-z)*(pz-z)) );
      if(dist<closest)
      {
        player=i;
        closest=dist;
      }
    }
    if(player==INVALID_PLAYER_ID)return SendClientMessage(playerid,0xFFFFFFFF,"No one is nearby");
    new item,itemname[MAX_ITEM_NAME],amount,tries,tmp[128];
    item=random(100); //Success rate
    if(item>80)return SendClientMessage(playerid,0xFF0000FF,"You fail to take something of interest");
    while(!strlen(itemname))
    {
      GetPlayerItemInfo(player,item,itemname,sizeof(itemname),amount);
      if(!strlen(itemname))item=random(MAX_ITEMS);
      tries++;
      if(tries>10)itemname="_&_&_&_Money_&_&_&_";
    }
    if(!strcmp(itemname,"_&_&_&_Money_&_&_&_",false))
    {
      if(GetPlayerMoney(player)<=0)return SendClientMessage(playerid,0xFF0000FF,"You fail to take something of interest");
      item=random((GetPlayerMoney(player)>5000)?(5000):(GetPlayerMoney(player)));
      GivePlayerMoney(playerid,item);
      pPPDelay[playerid]=GetTickCount()+10000;
      GivePlayerMoney(player,0-item);
      format(tmp,128,"Someone stole $%d from you!",item);
      SendClientMessage(player,0xFF0000FF,tmp);
      format(tmp,128,"You stole $%d",item);
      return SendClientMessage(playerid,0xFF0000FF,tmp);
    }
    RemoveItem(player,itemname,1);
    AddItem(playerid,itemname,1);
    format(tmp,128,"Someone stole your %s!",itemname);
    SendClientMessage(player,0xFF0000FF,tmp);
    format(tmp,128,"You stole a %s!",itemname);
    SendClientMessage(playerid,0xFF0000FF,tmp);
    pPPDelay[playerid]=GetTickCount()+10000;
    return 1;
  }
    return 0;
}
Download
j_inventory_v2.inv<--Direct link (Right Click+Save As)
j_inventory.inc

Version Notes
Version 2
-Added SaveInventory(playerid) and LoadInventory(playerid)
-Fixed all reported errors
-Converted to use Pvars

If you find any bugs please report them here.
Reply


Messages In This Thread
[INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Joe Staff - 26.02.2010, 22:39
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by LuxurioN™ - 26.02.2010, 22:48
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by aspire5630 - 26.02.2010, 22:49
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 26.02.2010, 23:03
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Torran - 26.02.2010, 23:21
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MaykoX - 27.02.2010, 03:26
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by ViruZZzZ_ChiLLL - 27.02.2010, 06:12
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 27.02.2010, 06:56
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by bydraq10 - 27.02.2010, 07:24
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by armyoftwo - 05.03.2010, 14:32
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by aircombat - 05.03.2010, 15:17
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by cyber_punk - 07.03.2010, 04:02
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 07.03.2010, 06:54
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by cyber_punk - 08.03.2010, 12:31
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 08.03.2010, 15:45
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by king4you - 04.04.2010, 02:49
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MisterTickle - 27.04.2010, 03:04
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Peep - 27.04.2010, 13:58
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by yezizhu - 28.04.2010, 03:20
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MisterTickle - 01.05.2010, 06:37
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Aur0nX390 - 01.05.2010, 06:53
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 15.05.2010, 05:16
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 15.05.2010, 16:43
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by MisterTickle - 19.05.2010, 03:56
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Stas92 - 19.05.2010, 13:08
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by agd555 - 20.05.2010, 15:56
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Her - 22.05.2010, 15:07
Re: [INC]Player Inventory -- Uses 0.3 Dialogs - by Joe Staff - 23.05.2010, 10:38
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Her - 23.05.2010, 12:54
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Owenlishious - 25.05.2010, 12:39
AW: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by 'Pawno. - 20.09.2012, 03:42
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Nexis - 20.09.2012, 09:42
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by NoahF - 20.09.2012, 10:06
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Joe Staff - 20.09.2012, 10:18
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Nexis - 20.09.2012, 12:22
Re : [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Naruto_Emilio - 20.09.2012, 12:29
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Nexis - 20.09.2012, 12:33
Re : [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Amine_Mejrhirrou - 20.10.2012, 01:19
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by ZayanImran - 29.12.2012, 11:51
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Sramm - 12.01.2013, 10:09
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by kyoto - 28.01.2013, 16:24
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Admigo - 17.03.2013, 13:59
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by por12802 - 29.03.2013, 05:50
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Juanxz - 15.06.2013, 15:22
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by Supermaxultraswag - 28.12.2014, 17:31
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by MasonSFW - 22.07.2015, 08:20
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by SQLite - 28.12.2015, 23:56
Re: [INC]Player Inventory[v2] -- Now with Saving/Loading functions - by jojo5528 - 01.09.2018, 14:20

Forum Jump:


Users browsing this thread: 4 Guest(s)