View Single Post
Old 29/05/2020, 12:48 PM   #3
Nexotronix
Huge Clucker
 
Nexotronix's Avatar
 
Join Date: Jan 2010
Location: Donetsk/Ukraine
Posts: 275
Reputation: 41
Default Re: What's wrong with the loop?

Quote:
Originally Posted by Variable View Post
If I get it correctly, you want items to be added up to the same slot in case they are the same type, but instead they are added to other slots instead of being added to the same slot?

If so, you need to loop through inventory slots to check if the slot is used and the type of item you're adding is matching the one in the slot.

Don't return zero if the condition is not met because it will just stop the loop and the rest of code from working. And just a tip, whenever your condition is met, you can break the loop using 'break' instead of returning a value.

Back to the topic, if the item wasn't added to an existing slot, you can continue to make another loop so that it is added to a free slot, then you can break the loop if it is added to one slot, like use "break" after you add the item to a slot, so that it doesn't continue through all the other slots.

And finally, you can update the inventory independently without being added to the loop(s) at all.
This code works! Thank you! REP+ to ya!

pawn Code:
stock AddItemToInventory(playerid,itemid)
{
    new SlotID = 0;
    while(SlotID != MAX_ITEM_SLOTS)
    {
        new str[64];
        if(SlotInfo[playerid][SlotID][slotUsed] == 1)
        {
            format(str,sizeof(str),"SlotID: %i is Unavaible for ItemID: %i", SlotID, itemid);
            ITM(playerid,str);
        }
        else if(SlotInfo[playerid][SlotID][slotUsed] == 0)
        {
            PutItemToInv(playerid,itemid,SlotID);
            break;
        }
        SlotID++;
    }
    return 1;
}
Attached Images
File Type: jpg sa-mp-001.jpg (192.7 KB, 12 views)
File Type: jpg 2.JPG (67.4 KB, 9 views)
File Type: jpg sa-mp-002.jpg (194.2 KB, 10 views)
__________________
Work In Progress:
[GameMode]Stroberia RolePlay (20%) [Join My Channel to take part in Testing http://discord.gg/sBpRC5E]
Development Video: Watch Here
Serverside Map/GPS System: Watch Here
Discord: Tom_Strobe#2300
Nexotronix is offline   Reply With Quote