[FilterScript] Textbased Inventory Base
#1

I made this in attempt to learn arrays. It mainly consists of 2D-arrays, and it's my first release with arrays that I actually made myself so please be gentle with feedback as it was my first attempt. I'm eager for feedback and constructive criticism though, so please do reply if you see anything void! This was the first version of my first inventory system (ever), which was originally meant to be used for a server but as we've decided to resort to textdraws and dialogs, I am now releasing this version.

Some things you should know before using this script:
- Item ID 0 isn't used. Reasons for this are that the inventory determines empty slots if the slot ID is 0, so adding item ID 0 will make the inventory believe the slot is empty. (Since I store item IDs inside the slots)
- This script uses ALOT of arrays, variables and loops. Make sure you know atleast the basics of them before trying to use it.
- The command /useitem is empty. It only shows a message and removes the item incase the value exceeds the value the item has.

Quote:
Commands:

/inventory
  • Uses a loop to go through a player's inventory slots
  • If a slot is 0, it'll show them an "EMPTY" message.
  • Item IDs are stored inside the slot variable for ^
/useitem [slot] [value]
  • Removes specified value from the item's value
  • If the item's value is zero after change, it'll get removed
  • Displays a SCM w/: the item ID, name, value and slot ID.
/dropitem [slot]
  • Removes the item and item value from a player's inventory (if slot != empty)
  • Creates an ammo box object (3014) at a player's position
  • Creates a label saying: the item ID, name, value and object ID.
  • Using an array and a loop for both the label and the object (see script)
/pickupitem [pickupid]
  • Removes the object and label and clears the pickup ID, thanks to mentioned array
  • Adds the item ID and item value to a player's inventory if they have empty slots
  • A player needs to be in range of the object before it can be picked up (determined by pickup ID)
/giveitem [player] [slot] [itemid] [value]
  • Gives a player a selected item ID and value (ID range 1-15)
  • This will overwrite whatever the player had in their slot
  • The message it shows is incomplete. It doesn't show the target a message, nor does it show what player it was given to.
  • Currently only usable by logged in RCON users
/itemlist
  • Uses a loop to go through the array and show a scm w/ all items
  • Displays item ID and name (from 1 to 15).
  • Currently only usable by logged in RCON users
Video of usage (which was made yesterday, some things have been modifed (i.e. text colors, some params, etc) for the script to be actually plug and play worthy):
[ame]http://www.youtube.com/watch?v=2OLPB3f1_BM[/ame]

pawn Код:
#include <a_samp>
#include <sscanf2>
#include <zcmd>

#define MAX_INV_SLOTS   5
#define MAX_INV_ITEMS   15
#define MAX_DROP_OBJ    50

// Store item ID inside of this
new pInv_Item[MAX_PLAYERS][MAX_INV_SLOTS];
new pInv_Value[MAX_PLAYERS][MAX_INV_SLOTS];

new DroppedItemsObj[MAX_DROP_OBJ];
new Text3D: DroppedItemsLab[MAX_DROP_OBJ];
new DroppedItemsVal[MAX_DROP_OBJ];
new DroppedItemsItem[MAX_DROP_OBJ];
new DroppedItemsTime[MAX_DROP_OBJ];

new InventoryItems[MAX_INV_ITEMS][] = {
//  ID,     "Name of Item"

    {0,     "Untitled"}, // Can't be given
    {1,     "Untitled"},
    {2,     "Untitled"},
    {3,     "Untitled"},
    {4,     "Untitled"},
    {5,     "Untitled"},
    {6,     "Untitled"},
    {7,     "Untitled"},
    {8,     "Untitled"},
    {9,     "Untitled"},
    {10,    "Untitled"},
    {11,    "Untitled"},
    {12,    "Untitled"},
    {13,    "Untitled"},
    {14,    "Untitled"}
};

public OnFilterScriptExit()
{
    for(new i = 0; i < MAX_DROP_OBJ ; i ++)
    {
        DestroyObject(DroppedItemsObj[i]);
        Delete3DTextLabel(DroppedItemsLab[i]);
    }

    return 1;
}

CMD:inventory(playerid, params[])
{
    new string[128];

    for(new i = 0; i < MAX_INV_SLOTS; i ++)
    {
        if(pInv_Item[playerid][i] > 0)
        {
            format(string, sizeof(string), "Item Slot %d: {549E63}%s{FFFFFF} (V: %d)", i, InventoryItems[pInv_Item[playerid][i]][1], pInv_Value[playerid][i]);
            SendClientMessage(playerid, -1, string);
        }

        else if(pInv_Item[playerid][i] == 0)
        {
            format(string, sizeof(string), "Item Slot %d: {BF5050}EMPTY{FFFFFF}", i);
            SendClientMessage(playerid, -1, string);
        }

    }

    return 1;
}

CMD:useitem(playerid, params[])
{
    new slot, value, string[128];

    if(sscanf(params, "i", slot, value)) return SendClientMessage(playerid, -1, "/useitem [slot]");
    if(slot > 4) return SendClientMessage(playerid, -1, "There are only {50A0BF}five{FFFFFF} slots you can use! (0-4)");
    if(pInv_Item[playerid][slot] == 0) return SendClientMessage(playerid, -1, "There's nothing in that inventory slot!");

    if(pInv_Value[playerid][slot] < value) return SendClientMessage(playerid, -1, "The value you specified is incorrect! You don't have that much of the item you selected!");

    format(string, sizeof(string), "You used item {50A0BF}ID %d{FFFFFF} ({50A0BF}%s{FFFFFF}) {50A0BF}(%d){FFFFFF} from {50A0BF}slot %d{FFFFFF}.", InventoryItems[pInv_Item[playerid][slot]][0], InventoryItems[pInv_Item[playerid][slot]][1], pInv_Value[playerid][slot], slot);
    SendClientMessage(playerid, -1, string);

    pInv_Value[playerid][slot] -= value;

    if(pInv_Value[playerid][slot] == 0) pInv_Item[playerid][slot] = 0;

    return 1;
}

CMD:pickupitem(playerid, params[])
{
    new Float: X, Float: Y, Float: Z, string[128], item;

    if(sscanf(params, "i", item)) return SendClientMessage(playerid, -1, "/pickupitem [id]");

    GetObjectPos(DroppedItemsObj[item], X, Y, Z);
   
    if(IsPlayerInRangeOfPoint(playerid, 3.0, X, Y, Z))
    {
        for(new i = 0; i < MAX_INV_SLOTS; i ++)
        {
            if(pInv_Item[playerid][i] == 0)
            {
                pInv_Item[playerid][i] = DroppedItemsItem[item];
                pInv_Value[playerid][i] = DroppedItemsVal[item];

                format(string, sizeof(string), "Picked up item {50A0BF}ID (%d){FFFFFF} ({50A0BF}%s{FFFFFF}) {50A0BF}(%d){FFFFFF}. Stored in {50A0BF}slot %d{FFFFFF}.", item, InventoryItems[DroppedItemsItem[item]][1], DroppedItemsVal[item], i);
                SendClientMessage(playerid, -1, string);

                break;
            }
        }
       
        DestroyObject(DroppedItemsObj[item]);
        Delete3DTextLabel(DroppedItemsLab[item]);

        DroppedItemsObj[item] = 0;

        DroppedItemsVal[item] = 0;
        DroppedItemsItem[item] = 0;
    }
   
    else SendClientMessage(playerid, -1, "You're not near that object!");

    return 1;
}

CMD:dropitem(playerid, params[])
{
    new slot, string[128], labstring[128], Float: X, Float: Y, Float: Z;
    GetPlayerPos(playerid, X, Y, Z);

    if(sscanf(params, "i", slot)) return SendClientMessage(playerid, -1, "/dropitem [slot]");

    if(slot > 4) return SendClientMessage(playerid, -1, "There are only {50A0BF}five{FFFFFF} slots you can use! (0-4)");
    if(pInv_Item[playerid][slot] == 0) return SendClientMessage(playerid, -1, "There's nothing in that inventory slot!");

    for(new i = 0; i < MAX_DROP_OBJ; i ++)
    {
        if(DroppedItemsObj[i] == 0)
        {
            format(labstring, sizeof(labstring), "[{B2E66E}LOOTABLE ITEM{FFFFFF}]\n %s (%d)\n[{B2E66E}/pickupitem{FFFFFF} ID: {B2E66E}%d{FFFFFF}]", InventoryItems[pInv_Item[playerid][slot]][1], pInv_Value[playerid][slot], i);

            DroppedItemsLab[i] = Create3DTextLabel(labstring, 0xFFFFFFFF, X +0.1, Y, Z -0.4, 15.0, GetPlayerVirtualWorld(playerid), 1);
            DroppedItemsObj[i] = CreateObject(3014, X, Y, Z -0.8, 0.00, 0.00, 0.00);
            SetPlayerPos(playerid, X, Y, Z +0.9);

            DroppedItemsVal[i] = pInv_Value[playerid][slot];
            DroppedItemsItem[i] = pInv_Item[playerid][slot];

            DroppedItemsTime[i] = 300;
           
            format(string, sizeof(string), "You dropped item {50A0BF}ID %d{FFFFFF} ({50A0BF}%s{FFFFFF}) {50A0BF}(%d){FFFFFF} from {50A0BF}slot %d{FFFFFF}.", InventoryItems[pInv_Item[playerid][slot]][0], InventoryItems[pInv_Item[playerid][slot]][1], pInv_Value[playerid][slot], slot);
            SendClientMessage(playerid, -1, string);

            break;
        }
    }

    pInv_Item[playerid][slot] = 0;
    pInv_Value[playerid][slot] = 0;

    return 1;
}

// ADMIN COMMANDS

CMD:giveitem(playerid, params[])
{
    new targetid, slot, item, value, string[128];

    if(sscanf(params, "uiii", targetid, slot, item, value)) return SendClientMessage(playerid, -1, "/additem [targetid] [slot] [item] [value]");
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "You need to be an RCON admin in order to use this command!");

    if(item > 15 || item < 1) return SendClientMessage(playerid, -1, "The item ID you entered does not exist. For a list of items, type: {50A0BF}/itemlist{FFFFFF}.");
    if(slot > 4) return SendClientMessage(playerid, -1, "There are only {50A0BF}five{FFFFFF} slots you can use! (0-4)");

    pInv_Item[playerid][slot] = item;
    pInv_Value[playerid][slot] = value;

    format(string, sizeof(string), "Item {50A0BF}ID %d{FFFFFF} ({50A0BF}%s{FFFFFF}) {50A0BF}(%d){FFFFFF} added to {50A0BF}slot %d{FFFFFF}.", item, InventoryItems[item][1], value, slot);
    SendClientMessage(playerid, -1, string);

    return 1;
}

CMD:itemlist(playerid, params[])
{
    new string[128];

    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "You need to be an RCON admin in order to use this command!");

    for(new i = 1; i < MAX_INV_ITEMS; i ++)
    {
        format(string, sizeof(string), "Item ID: {50A0BF}''%d''{FFFFFF} - Name: {50A0BF}''%s''{FFFFFF}", InventoryItems[i][0], InventoryItems[i][1]);
        SendClientMessage(playerid, -1, string);
    }

    return 1;
}
Please don't rerelease this without my permission. You're not obiliged to give credits and you can edit this however you want. If there are any bugs, please comment here and I'll fix them.
Reply
#2

Excellent! Simple, but effective - I really like it. :P
Reply
#3

This is very interesting for me too as im working on an inventory system right now.
i took a look at your 'CMD:inventory' wich checks out if a slot is empty or not.
How would you do this within a dialog ? i actually want to show the word EMPTY in the dialog instead of leaving it blanc.

and BTW: you did very well its good to see simple basics. Many people can learn from it.
Maybe you could put the itemvalues in an enum instead.
Reply
#4

Really Nice!
Good Job!
Reply
#5

nice i will use this
Reply
#6

Quote:
Originally Posted by AIped
Посмотреть сообщение
This is very interesting for me too as im working on an inventory system right now.
i took a look at your 'CMD:inventory' wich checks out if a slot is empty or not.
How would you do this within a dialog ? i actually want to show the word EMPTY in the dialog instead of leaving it blanc.

and BTW: you did very well its good to see simple basics. Many people can learn from it.
Maybe you could put the itemvalues in an enum instead.
I haven't actually done it in a dialog but they're very repetitive, and since 99% of this script uses formatted strings you can simply show a DIALOG_STYLE_MSGBOX with said strings, I assume you'll be using formatted strings aswell so you can just use them. (:

Konstantinos gave an example on how to store them in an enum here
Reply
#7

Not bad, good job!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)