26.02.2010, 22:39
(
Последний раз редактировалось Joe Staff; 04.04.2013 в 18:16.
)
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
Non-Required
AddItem(playerid,ItemName[],Amount)
This gives the player the specified item and amount.
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.
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
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
returns 1 unless &idx reaches a number larger than MAX_ITEMS
ResetPlayerInventory(playerid)
Removes all of a player's inventory
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.
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
Doesn't return a number
LoadInventory(playerid)
Loads the player's inventory if it exists
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.
Video
Here's a video made by Torran (JoeDaDude) using my Inventory include on his server.
![](http://video.xfire.com/23134e-4.jpg)
Example Script
Robbing a random item from a nearby player
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.
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;
}
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);
}
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!");
}
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!");
}
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;
}
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;
}
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;
}
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;
}
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;
}
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
}
Here's a video made by Torran (JoeDaDude) using my Inventory include on his server.
![](http://video.xfire.com/23134e-4.jpg)
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;
}
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.