What's wrong with the loop?
#1

Hello there!

I started to make inventory system for free use, and want to make some kind like a "AutoFreeSlotFinder"

to put Item automaticly to nearest avaible slot using loops.

So, what's wrong here?

Code:

pawn Code:
stock AddItemToInventory(playerid,itemid)
{
    for( new SlotID = 0; SlotID < MAX_ITEM_SLOTS; SlotID++)
    {
        if(SlotInfo[playerid][SlotID][slotUsed] == 0)
        {
            PutItemToInv(playerid,itemid,SlotID);
        }
        else if(SlotInfo[playerid][SlotID][slotUsed] == 1 && SlotInfo[playerid][SlotID][slotItemType] == ItemInfo[itemid][ITEM_TYPE])
        {
            if(SlotInfo[playerid][SlotID][slotItemAmount] < ItemInfo[itemid][ITEM_MAX_VALUE])
            {
                PlusItemToInv(playerid,itemid,SlotID);
            }
            else
            {
                return 0;
            }
        }
        else if(SlotInfo[playerid][SlotID][slotUsed] == 1)
        {
            UpdateInventory(playerid);
        }
        else
        {
            return 0;
        }
    }
    return 1;
}
Function Effects are can be seen at the screenshots:
Reply
#2

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.
Reply
#3

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;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)