[Include] [INC] Player Inventory by Joe Staff
#1

A relatively basic include I've made for an upcoming script, I've made this for releasing so it's easy to figure out, even easier to make an item with.

How To Install
1st. Place the a_playerinventory.inc into the "pawno/includes" folder found in your server's directory.
2nd. Open your gamemode's .pwn and add to the top the following line
pawn Code:
#include <a_playerinventory>
3rd. Place the following functions in their respective callbacks
pawn Code:
//Inside of OnGameModeInit
PlayerInventoryOnGameModeInit();
//Inside of OnPlayerUpdate
PlayerInventoryOnPlayerUpdate(playerid);
//Inside of OnPlayerKeyStateChange
PlayerInventoryOnPlayerKeyState(playerid,newkeys);
//Outside of any callbacks
public PlayerInventoryUseItem(playerid,ItemName[])
{
  return 0;
}
Functions
PlayerInventoryToggleWindow(playerid,toggle);
-Use this to bring up the menu so players can use their items. This menu is updated if the other functions are used so they player can see immediate changes if colors are changed or items added. A player uses the arrow keys to control it hit's the sprint button to use an item and the enter/exit vehicle button to close it.
pawn Code:
if(!strcmp(cmdtext[1],"Inventory",true))
{
  if(IsPlayerInAnyVehicle(playerid))return SendClientMessage(playerid,0xFF0000FF,"You cannot be in a vehicle when searching your inventory.");
  PlayerInventoryToggleWindow(playerid,1); //setting to 0 closes the window
  return 1;
}
PlayerInventoryAddItem(playerid,ItemName[],amount=1);
-Use this to add a specific item to a player's inventory, It's case sensitive. No more remembering item IDs! Returns 0 if Inventory is full, returns -1 if invalid item name or amount (EX: "Empty" and -1)
pawn Code:
public OnPlayerPickUpPickup(playerid,pickupid)
{
  if(pickupid==paperclip)
  {
    PlayerInventoryAddItem(playerid,"Paper Clip");
  }
}
PlayerInventoryRemoveItem(playerid,ItemName[],amount=1);
-Use this to remove items from a player's inventory. Once a player's item runs to '0', it turns to "Empty" (or otherwise a useless non-usable item). Returns 0 if item not found, -1 if invalid Item Name or Amount
pawn Code:
public OnPlayerEnterCheckPoint(playerid)
{
  PlayerInventoryRemoveItem(playerid,"Paper Clip",PlayerInventoryPlayerHasItem(playerid,"Paper Clip"));
  SendClientMessage(playerid,0xFF0000FF,"You've turned in your paper clips!");
}
PlayerInventoryPlayerHasItem(playerid,ItemName[]);
-This is perfect for 'if' statements and can be used for missions to collect things like garbage or paper clips. Returns the amount of the item the player owns
pawn Code:
if(!strcmp(cmdtext[1],"ThrowRock",true))
{
  if(PlayerInventoryPlayerHasItem(playerid,"Rock"))
  {
    return SendClientMessage(playerid,0xFF0000FF,"You threw a rock. Take THAT society!");
  }
}
PlayerInventoryPlayerItemData(playerid,slot,ItemNa me[],&Amount,len=sizeof(ItemName));
-Much like GetPlayerWeaponData, this can be used to record down all the items that the player has for saving or for removing
pawn Code:
if(!strcmp(cmdtext[1],"CheckInventory",true,14))
{
  new string[MAX_ITEM_NAME+20];
  new amount = strval(cmdtext[16]);
  GetPlayerName(amount,string,sizeof(string));
  format(string,sizeof(string),"%s has the following items:",string);
  SendClientMessage(playerid,0xFF0000FF,string);
  for(new index;index<MAX_ITEMS;index++)
  {
    PlayerInventoryPlayerItemData(playerid,index,string,amount);
    format(string,sizeof(string),"%s -- %d",string,amount);
    SendClientMessage(playerid,0xFF0000FF,string);
  }
  SendClientMessage(playerid,0xFF0000FF,"--------------------");
}
PlayerInventorySetLetterColor(playerid,code);
-This function is used to change the colors of the non-selected lines, the codes go as follows:
Code:
0  White
1  Green
2  Blue
3  Yellow
4  Purple
5  Black -- Needs a bright box color
6  Light Green
7  Light Blue
8  Light Yellow
9  Light Purple
10  Very Light Green
11  Very Light Blue
12  Very Light Purple
returns 0 if invalid code
PlayerInventorySetWindowColor(playerid,colorhex);
-This changes the window's box color, pretty simple. Uses Hexadecimal, but won't stop you from putting in a large decimal number
pawn Code:
if(!strcmp(cmdtext[1],"setmyboxcolor",true,13))
{
  PlayerInventorySetWindowColor(playerid,strval(cmdtext[15]));
  return 1;
}
PlayerInventorySetFont(playerid,font);
-Let's you choose between the fonts found on this page https://sampwiki.blast.hk/wiki/TextDrawFont
pawn Code:
if(!strcmp(cmdtext[1],"setmyfont",true,9))
{
  PlayerInventorySetFont(playerid,strval(cmdtext[11]));
  return 1;
}
Callback
PlayerInventoryUseItem(playerid,ItemName[]);
-This callback is called whenever a player 'uses' a valid (not "Empty") item in their inventory window with the sprint key. This is where you'll be placing your 'definitions' for your items, just like making commands
pawn Code:
public PlayerInventoryUseItem(playerid,ItemName[])
{
  if(!strcmp(ItemName,"Cheese Burger",false))
  {
    new Float:tmp;
    GetPlayerHealth(playerid,tmp);
    SetPlayerHealth(playerid,tmp+10);
    PlayerInventoryRemoveItem(playerid,"Cheese Burger");
    return 1;
  }
  if(!strcmp(ItemName,"M4 Assault Rifle",false))
  {
    GivePlayerWeapon(playerid,31,100);
    PlayerInventoryRemoveItem(playerid,"M4 Assault Rifle");
    return 1;
  }
  if(!strcmp(ItemName,"Iron Ore",false))
  {
    new tmp = PlayerInventoryPlayerHasItem(playerid,"Iron Ore");
    if(tmp<5)return SendClientMessage(playerid,0xFF0000FF,"You need 5 Iron Ores to make steel");
    PlayerInventoryRemoveItem(playerid,"Iron Ore",5);
    PlayerInventoryAddItem(playerid,"Steel Plate",1);
    return 1;
  }
  return SendClientMessage(playerid,0xFF0000FF,"You can't use that item here.");
}
Download
http://************/d3893lu/files/scr...rinventory.inc -Right Click + Save As...

Pictures
[Image: PIP.JPG]

Resources
This can potentially use 400 TextDraws (2 per player on a fuller server)
Also item names can only be 19 characters long, to prevent the textdraw from cutting off, but this will be fixed in the next SA-MP version.
Reply
#2

Yay First Comment!

Nice .inc im going to use it for my Server!

edit: Where do i download a_playerinventory.inc
Reply
#3

Yup, forgot it, there you go. Lol.
Reply
#4

not everyone uses IE , you should add an .amx file to another hosting site for people that use fire fox
Reply
#5

I'm using FireFox and I can just Right Click + Save Link As... Also, there's no .pwn, just an include.
Reply
#6

Very nice, want a mirror?
Reply
#7

Nice work
Reply
#8

Everyone note that you have to use the callback 'PlayerInventoryUseItem', Otherwise you'll get an error coming out of the include.
Reply
#9

omg... very nice!!!
Reply
#10

I will use this! ThankYou!!
Reply
#11

Amazing
tanks a lot!!!!!!
Reply
#12

I Made You A Mirror if You Dont Mind?

http://solidfiles.com/d/GcZc
Reply
#13

nice work this with GF?
Reply
#14

I suppose I could make an example gamemode later, if anyone's interested. Or perhaps a list of pre-made items.

Just realized that in SA-MP 3.0, you'll be able to carry 40 items.
Reply
#15

Good Job Joe
Reply
#16

I love it
But if you can make a version without menu, that will be great

Good work
Reply
#17

You don't need to use the menu. Just make commands using 'PlayerInventoryUseItem' to use the item.
Example:
pawn Код:
if(!strcmp(cmdtext[1],"UseItem",true,7))
{
  for(new i; i<strlen(cmdtext[9]);i++)if((cmdtext[9+i]<'9') || (cmdtext[9+i]>'0'))
  {
    if(strval(cmdtext[9])>MAX_ITEMS)return SendClientMessage(playerid,0xFF0000FF,"USAGE: /UseItem [Item Slot or Name]");
    PlayerInventoryUseItem(playerid,PIPIN[playerid][strval(cmdtext[9])]); //new PIPIN[MAX_ITEMS][MAX_ITEM_NAME] is located in the include
    return 1;
  }
  if(!strlen(cmdtext[9]))return SendClientMessage(playerid,0xFF0000FF,"USAGE: /UseItem [Item Slot or Name]");
  PlayerInventoryUseItem(playerid,cmdtext[9]);
  return 1;
}
Reply
#18

Oops
Reply
#19

Btw, the items save on gmx, leaving server ?
Reply
#20

These are just the functions, the saving method (either it be file based or MySQL) is up to you to script. If I were to make it save, it forces the server host to either have a MySQL database to even use, or use files he may already be using.
Reply


Forum Jump:


Users browsing this thread: 8 Guest(s)