SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 29/05/2020, 12:08 PM   #1
Nexotronix
Huge Clucker
 
Nexotronix's Avatar
 
Join Date: Jan 2010
Location: Donetsk/Ukraine
Posts: 275
Reputation: 41
Thumbs up [Solved] What's wrong with the loop? [Solution at 3rd MSG]

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:
Attached Images
File Type: jpg sa-mp-000.jpg (193.1 KB, 15 views)
File Type: jpg Ñíè́îê.JPG (60.4 KB, 13 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

Last edited by Nexotronix; 29/05/2020 at 01:18 PM. Reason: Solved!
Nexotronix is offline   Reply With Quote
Old 29/05/2020, 12:35 PM   #2
Variable™
Gangsta
 
Variable™'s Avatar
 
Join Date: Jul 2015
Posts: 828
Reputation: 200
Default 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.
__________________
Need a game server? Contact me for information.
Variable™ is offline   Reply With Quote
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
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong loop result. Rongvaldyr Scripting Help 2 12/02/2017 02:51 PM
Wrong loop result. Rongvaldyr Scripting Help 4 19/01/2017 04:39 AM
Wrong returns from a loop TwinkiDaBoss Scripting Help 2 20/11/2016 01:21 AM
What's wrong with this loop? Makaveli93 Scripting Help 2 22/06/2012 09:00 PM
What's wrong with this loop? Knappen Scripting Help 2 07/11/2011 03:51 PM


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


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