slot 1|itemid 10
slot 2|itemid 20
slot 3|itemid 30
slot 1|itemid 10
slot 2|itemid 30// the old slot :slot 3|itemid 30
slot 1|itemid 10
slot 3|itemid 30//slot 3 should be 2
//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;
}
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;
}
If you have foreach include, read the section about custom iterators. Otherwise:
pawn Код:
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. |
slot 1|item id 10 slot 3|item id 30
slot 1|item id 10 slot 2|item id 30
slot 1|item id 20 slot 2|item id 20 slot 3|item id 30
slot 1|item id 20 slot 2|item id 30
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
|
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)
|
slot 0|item id 10 slot 1|item id 20 slot 2|item id 30
slot 0|item id 10 slot 1|item id 30
stock IsSlotUsed(playerid,slotid) { if(ClothesItems[playerid][slotid]==0) { return 0; } else return 1; }
//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");
}