SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   What's wrong with the loop? (https://forum.sa-mp.com/showthread.php?t=676082)

Nexotronix 29/05/2020 12:08 PM

[Solved] What's wrong with the loop? [Solution at 3rd MSG]
 
2 Attachment(s)
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:

Variable™ 29/05/2020 12:35 PM

Re: What's wrong with the loop?
 
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.

Nexotronix 29/05/2020 12:48 PM

Re: What's wrong with the loop?
 
3 Attachment(s)
Quote:

Originally Posted by Variable™ (Post 4148575)
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;
}


All times are GMT. The time now is 12:18 AM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.