[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
#2

Good work Joe. I'll test this. And, I have one question: Recently, I saw a video his "", and it seemed really interesting. You plans to launch that script?
Reply
#3

This is great! Really nice work any bugs?
Reply
#4

Quote:
Originally Posted by © Tђэ LυxυяiσN™
Good work Joe. I'll test this. And, I have one question: Recently, I saw a video his "", and it seemed really interesting. You plans to launch that script?
Ehhhh, it was part of a new RP GM I was working on. I may or may not release that portion.

Quote:
Originally Posted by aspire5630
This is great! Really nice work any bugs?
No bugs that I'm aware of. Perhaps that when an item is removed from a player's inventory and the player has the window opened, it won't update. But you can fix that yourself by adding
pawn Код:
if(!PlayerHasItem(playerid,ItemName))return SendClientMessage(playerid,0xFFFFFFFF,"You no longer have that item!");
to OnPlayerUseItem


Quote:
Originally Posted by © Tђэ LυxυяiσN™
Good work Joe. I'll test this.
How would you plan on doing that? I didn't add the download link until just now. :P
Reply
#5

Quote:
Originally Posted by Joe Staff
Quote:
Originally Posted by © Tђэ LυxυяiσN™
Good work Joe. I'll test this. And, I have one question: Recently, I saw a video his "", and it seemed really interesting. You plans to launch that script?
Ehhhh, it was part of a new RP GM I was working on. I may or may not release that portion.

Quote:
Originally Posted by aspire5630
This is great! Really nice work any bugs?
No bugs that I'm aware of. Perhaps that when an item is removed from a player's inventory and the player has the window opened, it won't update. But you can fix that yourself by adding
pawn Код:
if(!PlayerHasItem(playerid,ItemName))return SendClientMessage(playerid,0xFFFFFFFF,"You no longer have that item!");
to OnPlayerUseItem


Quote:
Originally Posted by © Tђэ LυxυяiσN™
Good work Joe. I'll test this.
How would you plan on doing that? I didn't add the download link until just now. :P
Thankz for making this for me and then releasing it,
I used it on my server as soon as you sent me it over xfire,
Using it for my shop system and weapon system
Also thanks for using my video
Reply
#6

NotT bad I will use it
Reply
#7

Nice I think I'm gonna use it
________
Gm Foods
Reply
#8

Quote:
Originally Posted by ViruZZzZ_ChiLLL
Nice I think I'm gonna use it

ViruZZzZ_ChiLLL's Post History
Reply
#9

you're bad driver because sytem is excellent
Reply
#10

It would be cool if you make this system saving!
Reply
#11

nice one Joe , and btw i really like that : "http://www.xfire.com/video/17d1ea/" its amazing , will u release it one day??
________
EXTREME VAPORIZER
Reply
#12

I was just about to convert a menu based inventory over to dialogs, Thanks you saved me the trouble.
Reply
#13

Quote:
Originally Posted by Seif_
System looks similar to mine. Good job though.
It's influenced by my old system here: http://forum.sa-mp.com/index.php?topic=116917.0

Quote:
Originally Posted by Seif_
I'm using this pic >
Go ahead =p

Quote:
Originally Posted by [AC
Etch ]
nice one Joe , and btw i really like that : "http://www.xfire.com/video/17d1ea/" its amazing , will u release it one day??
I had began making a MySQL version of the email system so it could be given to a friend who wants to convert it to read and write PMs for his forum. But I guess I never got around to finishing it.
Reply
#14

Well for some reason even on a blank gm, when I add


pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
  InventoryOnDialogResponse(playerid, dialogid, response, inputtext[]); //you forgot the semi colon..in the example :P
  return 1;
}
I get error 029: invalid expression, assumed zero for the line InventoryOnDialogResponse(playerid, dialogid, response, inputtext[]);

[EDIT] Rofl never mind figured it out you might want to fix your example for InventoryOnDialogResponse too....

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
  InventoryOnDialogResponse(playerid, dialogid, response, inputtext); //you forgot the semi colon..in the example :P also should have removed the brackets xD
  return 1;
}
Reply
#15

Heh, sure did. I just copied and pasted it real quick, thanks for noticing.
Reply
#16

i didnt understand what to do with the
Код:
InventoryOnDialogResponse
what EXACTLY to write under the OnDialogResponse
EXACTLY this:
Код:
InventoryOnDialogResponse(playerid, dialogid, response, inputtext);
or any thing else i dont understand
Reply
#17

Can someone give me a good idea about how I would go about saving with DUDB/DINI and loading back up?

I asked Joe in a private PM but am still a bit confused.
Reply
#18

Good Work Man !
Reply
#19

nice~
Reply
#20

Why am I getting j_inventory.inc(66) : error 001: expected token: ")", but found "continue"

when I use ifplayerhasitem
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)