Bag system help
#1

Hello guys. It has been a while since my last post here, but i finally found time for scripting again!

What I need help with, is a bag system. A command called "/bag" or something like that, that shows all slots in your bag.
The bag must have 5 slots, and therefore players can only carry 5 items.

An item could for example be a cheque.
Lets say that the cheque is in bag slot 1, and when the player types "/use 1" he will get some money.
So here is what i want it to look like when "/bag" command is typed:
"-----------------------
Slot 1: Checque +20
Slot 2: Empty
Slot 3: Empty
Slot 4: Empty
Slot 5: Empty
-----------------------"

(Only the "Cheque +20" if the player has a cheque in his bag slot 1, of course )

And when "/use 1" command is typed, the player will get $20 because its a "+20" cheque.
- And the bagslot 1 will become empty.

Hope you understand what i want to make, and hope someone will help me.
Thank you.
Reply
#2

Just to clear things up:
I have a data saving system, using DjSon.
I have en enum which contains BagSlot1, BagSlot2 and so on.
Lets say that i want to add an item to bag slot 1, i use the variable "PlayerInfo[playerid][pBagSlot1]"

My question: What is the easiest way to add an item? Is it by making item IDs, like if a cheque is ID 3, and then do "PlayerInfo[playerid][pBagSlot1] = 3"
Or is there an easier way?

Thank you.
Reply
#3

I found this threat https://sampforum.blast.hk/showthread.php?tid=32310
It is something like that I want, but this is very old and its also a plugin, which is not nessecary.
If someone could help me making something like this, I would be very happy.
Thank you
Reply
#4

there is a include "pInvertory" just look in the Filterscripts and includes area its on the 5-9 page
Reply
#5

Quote:
Originally Posted by Zonoya
Посмотреть сообщение
there is a include "pInvertory" just look in the Filterscripts and includes area its on the 5-9 page
Thanks. Ill take a look at it.
It looks like its using textdraws, but i think I can modify it to use client messages
Reply
#6

..........
Reply
#7

Hi there. I got it working, but it has been a while. I still have the script. The code I made looks like this:
Well first off I made this function. This function adds an "item" to the player's bag, adding it to any free slot. I made a function to check if the bag is full or not, which has to be used before this function, otherwise it will be buggy. Just insert this code into your script above all callbacks.
pawn Код:
AddPlayerBagItem(playerid, itemid)
{
    if(PlayerInfo[playerid][pBagSlot1] == 0) PlayerInfo[playerid][pBagSlot1] = itemid;
    else if(PlayerInfo[playerid][pBagSlot2] == 0) PlayerInfo[playerid][pBagSlot2] = itemid;
    else if(PlayerInfo[playerid][pBagSlot3] == 0) PlayerInfo[playerid][pBagSlot3] = itemid;
    else if(PlayerInfo[playerid][pBagSlot4] == 0) PlayerInfo[playerid][pBagSlot4] = itemid;
    else if(PlayerInfo[playerid][pBagSlot5] == 0) PlayerInfo[playerid][pBagSlot5] = itemid;
}
(Remember that the "PlayerInfo[playerid][pBagSlot1-5]" is just my variable. You can make your variable just as you want to.)

This is the function to check if the player's bag is full. Important to use this function and check if the player's bag is full BEFORE adding an item to it. Here is the code:
pawn Код:
IsPlayerBagFull(playerid)
{
    if(PlayerInfo[playerid][pBagSlot1] == 0 || PlayerInfo[playerid][pBagSlot2] == 0 || PlayerInfo[playerid][pBagSlot3] == 0 || PlayerInfo[playerid][pBagSlot4] == 0 || PlayerInfo[playerid][pBagSlot5] == 0)
    {
        return false;
    }
    else return true;
}
This function is used to remove an item from the a player's bag. It is important to check if the item is in the bag first! (Function for checking player's bag is below). Here is the code:
pawn Код:
RemoveItemFromPlayerBag(playerid, itemid)
{
    if(PlayerInfo[playerid][pBagSlot1] == itemid) PlayerInfo[playerid][pBagSlot1] = 0;
    else if(PlayerInfo[playerid][pBagSlot2] == itemid) PlayerInfo[playerid][pBagSlot2] = 0;
    else if(PlayerInfo[playerid][pBagSlot3] == itemid) PlayerInfo[playerid][pBagSlot3] = 0;
    else if(PlayerInfo[playerid][pBagSlot4] == itemid) PlayerInfo[playerid][pBagSlot4] = 0;
    else if(PlayerInfo[playerid][pBagSlot5] == itemid) PlayerInfo[playerid][pBagSlot5] = 0;
}
This function is used to check whether a certain item is in the player's bag or not. It is importand to use this function BEFORE removing a certain item from a player's bag! Here is the code:
pawn Код:
IsItemInPlayerBag(playerid, itemid)
{
    if(PlayerInfo[playerid][pBagSlot1] == itemid || PlayerInfo[playerid][pBagSlot2] == itemid || PlayerInfo[playerid][pBagSlot3] == itemid || PlayerInfo[playerid][pBagSlot4] == itemid || PlayerInfo[playerid][pBagSlot5] == itemid)
    {
        return true;
    }
    else return false;
}
So actually, when you add an item to a player's bag, you are actually adding a number. For example could an AK-47 be number 2. You will have to make your code read that number, and labeel them with item names for the players.

Hope this helps you. Feel free to ask if you have any other questions.
Reply
#8

This would be more easier if you would have an array instead of a simple variable. For example:

pawn Код:
new pBagSlot[5];
In this case, it is possible to loop through all of the bag slots, this would probably improve some (or maybe all of your current) functions and would open more abbilities in future coding. Not to mention that a dynamic system like this should be coded following these basics. (Just pointing out if you are going to create something in near future)

pawn Код:
stock addItemToBag(playerid, itemid) {
   for (new i = 0; i < 5; i++) {
       if (pBagSlot[i] != 0) continue;

       pBagSlot[i] = itemid;
   }
}
Isnt this easier?

After all, its just a free advice.
Reply
#9

...........
Reply
#10

......
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)