Inventory slots.
#1

Hi all,

I have a problem with my inventory system.
I want to make if someone destroys/sold his clothes that the slot where the clothes was into get pushed back.
Example:
I have used 3 slots with 3 different items.
pawn Код:
slot 1|itemid 10
slot 2|itemid 20
slot 3|itemid 30
When i remove one item by removing slot 2 it needs to be like this:
pawn Код:
slot 1|itemid 10
slot 2|itemid 30// the old slot :slot 3|itemid 30
But it looks like:
pawn Код:
slot 1|itemid 10
slot 3|itemid 30//slot 3 should be 2
So what it should do is to decrease all slots by 1 that higher is then the slot that gets removed.

My Code:
pawn Код:
//the items are added with this code
ClothesItems[playerid][1]=10;
ClothesItems[playerid][2]=20;
ClothesItems[playerid][3]=30;

//using this test command to show the used slots with clothesids
if(!strcmp(cmdtext, "/testclothes", true))
{  
    for(new i = 1; i < MAX_SLOTS; i++)
    {
        if(ClothesItems[playerid][i]!=0)
        {
            format( string, sizeof string, "slot %d|item id %d",i,ClothesItems[playerid][i]);
            SendClientMessage( playerid, -1, string );
        }
    }
    return 1;
}
//using this command to remove slot 2
dcmd_removeclothes(playerid,params[])
{
    new slotid;
    if(sscanf(params, "d", slotid))
    {
        SendClientMessage(playerid,COLOR_RED,"USAGE: /removeclothes [slotid]");
        return 1;
    }
    if(IsSlotUsed(playerid,slotid)==0)
    {
        SendClientMessage(playerid,COLOR_RED,"You dont have any clothes in this slot");
        return 1;
    }
    format(globalstring,sizeof(globalstring),"You have removed slot:%d",slotid);
    SendClientMessage(playerid,-1,globalstring);
    RemoveClothes(playerid,slotid);
    return 1;

}

stock RemoveClothes(playerid,slotid)
{
    HasClothes[playerid]--;
    ClothesItems[playerid][slotid]=0;
    return 1;
}
Can someone correct my code?

Admigo
Reply
#2

If you have foreach include, read the section about custom iterators. Otherwise:

pawn Код:
stock RemoveClothes(playerid,slotid)
{
    ClothesItems[playerid][slotid] = 0;
    for(new i = slotid + 1; i != HasClothes[playerid]; ++i) {
        ClothesItems[playerid][i - 1] = ClothesItems[playerid][i];
    }
    HasClothes[playerid]--;
    return 1;
}
1. Remove item
2. Move all items to -1 slot
3. Decrease number of clothes

Remember to update HasClothes on addition/removal of clothes.

There is also fancy way using pointer swapping (introduced by Slice https://sampforum.blast.hk/showthread.php?tid=343172 ), but I think it won't be necessary.
Reply
#3

Quote:
Originally Posted by Misiur
Посмотреть сообщение
If you have foreach include, read the section about custom iterators. Otherwise:

pawn Код:
stock RemoveClothes(playerid,slotid)
{
    ClothesItems[playerid][slotid] = 0;
    for(new i = slotid + 1; i != HasClothes[playerid]; ++i) {
        ClothesItems[playerid][i - 1] = ClothesItems[playerid][i];
    }
    HasClothes[playerid]--;
    return 1;
}
1. Remove item
2. Move all items to -1 slot
3. Decrease number of clothes

Remember to update HasClothes on addition/removal of clothes.

There is also fancy way using pointer swapping (introduced by Slice https://sampforum.blast.hk/showthread.php?tid=343172 ), but I think it won't be necessary.
Thanks for your code but it still returns when i use /testclothes
Код:
slot 1|item id 10
slot 3|item id 30
It should return as:
Код:
slot 1|item id 10
slot 2|item id 30
Can you help me fix this?

EDIT:
I tried to remove slot 1 and it returns as:
Код:
slot 1|item id 20
slot 2|item id 20
slot 3|item id 30
It should return as:
Код:
slot 1|item id 20
slot 2|item id 30
Reply
#4

For some reason you are starting you array from 1, not 0. Don't do this, because you are wasting memory. Also this causes HasClothes to point on wrong key. If you add 4 items to clothes and delete 2, then 3 will move to 2, but 4 won't budge
Reply
#5

Quote:
Originally Posted by Misiur
Посмотреть сообщение
For some reason you are starting you array from 1, not 0. Don't do this, because you are wasting memory. Also this causes HasClothes to point on wrong key. If you add 4 items to clothes and delete 2, then 3 will move to 2, but 4 won't budge
Okay i will start the array by 0, still how can i fix this problem? I am using HasClothes only to detect how much clothes someone has. I am using MAX_SLOTS.
Reply
#6

Yup, but we don't want to iterate through all slots, only through populated slots, and conveniently you store their number in HasClothes. Additionaly now there shouldn't be any gaps between slots, so HasClothes will point to last element (when you change to index starting at 0, currently it points at second to last)
Reply
#7

Quote:
Originally Posted by Misiur
Посмотреть сообщение
Yup, but we don't want to iterate through all slots, only through populated slots, and conveniently you store their number in HasClothes. Additionaly now there shouldn't be any gaps between slots, so HasClothes will point to last element (when you change to index starting at 0, currently it points at second to last)
Even when i start the array with 0 it still didnt fixed the problem.
Reply
#8

Could you provide code for adding clothes, and if no - does it increment HasClothes properly?
Reply
#9

I runned some tests and i changed MAX_SLOTS to Hasclothes[playerid]
Now it works fine, only i noticed a new problem.

So when i have this items and slots used.
Код:
slot 0|item id 10
slot 1|item id 20
slot 2|item id 30
And when i remove slot 1 it returns as:
Code:
Код:
slot 0|item id 10
slot 1|item id 30
Код:
stock IsSlotUsed(playerid,slotid)
{
	if(ClothesItems[playerid][slotid]==0)
	{
		return 0;
	}
	else return 1;
}
Thats good but when i use the command /addclothes 2 40 (/addclothes slotid clothesid) it says the slot is already used.
So that means that ClothesItems[playerid][2] still exists.
How can i fix this?
Reply
#10

pawn Код:
//Somewhere where you add:
ClothesItems[0][0]=10;
ClothesItems[0][1]=20;
ClothesItems[0][2]=30;
ClothesItems[0][3]=40;
ClothesItems[0][4]=60;

HasClothes[0] = 5; //Important line



dcmd_testclothes(pid, params[]) {
    #pragma unused params
    for(new i = 0; i != MAX_SLOTS; i++)
    {
        format(string, sizeof string, "slot %d|item id %d|item %s", i, ClothesItems[pid][i], IsSlotUsed(pid, i) ? ("present") : ("not present"));
        SendClientMessage( playerid, -1, string);
    }
}

stock IsSlotUsed(playerid,slotid)
{
    return bool:ClothesItems[playerid][slotid];
}

stock RemoveClothes(playerid,slotid)
{
    for(new i = slotid + 1; i != HasClothes[playerid]; ++i) {
        ClothesItems[playerid][i - 1] = ClothesItems[playerid][i];
    }
    ClothesItems[playerid][--HasClothes[playerid]] = 0;
    return 1;
}

//Example adding clothes
dcmd_addclothes(pid, params[]) {
    new slotid, clothid;
    if(sscanf(params, "dD(-1)", clothid, slotid)) return SendClientMessage( playerid, -1, "USAGE: /addclothes [clothing id] [slotid]");
    if(-1 == slotid) {
        ClothesItems[pid][HasClothes[pid]++] = clothid;
    } else {
        ClothesItems[pid][slotid] = clothid;
    }
    return SendClientMessage( playerid, -1, "Done");
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)