What is the Script call to get the current inventory count?

Discussion in 'RPG Maker MV' started by Uzuki, Sep 2, 2019.

  1. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    Hey everyone, due to some conflicting issues with some limit inventory plugins I've had to forgo using them in my current project, but due to the nature of how the game is supposed to be played, I would still like to limit how many items the player can carry with them. This can be achieved easily enough with variables and conditional branches but there doesn't seem to be an easy way to retrieve the player's current inventory count and store it into a variable. Does anyone know if there a Script Call or even plugin that I can use to achieve this effect?
     
    #1
  2. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    $gameParty._items[id]
     
    #2
  3. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    I'm using it in a script call, but the variable isn't changing.
     
    #3
  4. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Do you have the items of that particular ID in your inventory?
     
    #4
  5. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    Sorry I must not have explained it well. I want to get the current amount of items in the player inventory and add it to a variable. For example, say you have 3 potions, 2 elixirs, and 1 phoenix down, then the script call would add 6 to the assigned variable.
     
    #5
  6. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Set a variable to 0, then:

    Code:
    for (var i in $gameParty._items) $gameVariables.setValue(varId, $gameParty._items[i])
    replace the varID with id of the variable where you want to save the item count.
     
    #6
  7. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    I tried that using "$gameVariables.setValue(17, $gameParty._items)" in a script call and I got this error:
    upload_2019-9-1_13-1-11.png
     
    #7
  8. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    In vanilla MV the code works.
    Hit f8 in the game to open the dev console, hopefully it shows the real readon why the bug triggered.

    Btw. it wasn't the fully desired code, I forgot to include the variable as an additive.
    Code:
    for (var i in $gameParty._items) $gameVariables.setValue(varId, $gameVariables.value(varId) + $gameParty._items[i])
     
    #8
  9. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg Ok it was on my end why the code wasn't working, but it still doesn't solve my problem. The code will just keep adding to the variable and I would still need to check every item in the game when I want an easier way to manage how many items are in the inventory.
     
    #9
  10. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Well, if you want to prevent having to reset the variable to 0 everytime, this script would do it.

    Code:
    var itemCount = 0;
    for (var i in $gameParty._items) itemCount += $gameParty._items[i];
    $gameVariables.setValue(varId, itemCount);
    where you just replace the varId with desired item id.

    As for the checking every item in the game, well, you've asked for a way to check the item count with a script, I delivered what you asked for.
    If it didn't solve your problem, then that means a small adaptation might be needed and it saved as a plugin to prevent you from having to check for every item if that's what you want.
     
    #10
    Uzuki likes this.
  11. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg Perfect! Works like a charm! Thank you so much with the help and being patient with me. I super appreciate it.
     
    #11
  12. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    It's okay. I usually opt in for the simplest solution, although the simplest one is not always the most convenient one, so it was kind of my bad too.
    Going a step further wouldn't be too difficult either though, for example this saved as a plugin
    Code:
    Game_Interpreter.prototype.command126 = function() {
        var value = this.operateValue(this._params[1], this._params[2], this._params[3]);
        var itemCount = 0;
        if (value > 0) {
            for (var i in $gameParty._items) itemCount += i;
            if (itemCount + value > $gameVariables.value(maxcapId) ) value = $gameVariables.value(maxcapId) - itemCount;
        }
        $gameParty.gainItem($dataItems[this._params[0]], value);
        if (value + itemCount == $gameVariables.value(maxcapId)) $gameMessage.add("Picked up only " + $dataItems[this._params[0]].name + " x" + value + " - Inventory full!");
        return true;
    };
    would do all checks on the backend and it would warn the player if the inventory got full.

    EDIT: Edited out one extra line of code
     
    Last edited: Sep 2, 2019
    #12
    Uzuki likes this.
  13. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg That's really neat! i tried it out, but I get an error of maxCap is undefined even though I put the variable number for the Max Inventory Limit.
     
    #13
    Poryg likes this.
  14. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Fixed, sorry :D
     
    #14
  15. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg Hmm, now when I add items it lumps them all into one.
     
    #15
  16. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Typing from mobile is prone to errors :( Debugged on PC, so hopefully it's all good now. Added in an extra spice too!

    Code:
    Game_Interpreter.prototype.command126 = function() {
    var value = this.operateValue(this._params[1], this._params[2], this._params[3]);
    var itemCount = 0;
    if (value > 0) {
    for (var i in $gameParty._items) itemCount += $gameParty._items[i];
    if (itemCount + value > $gameVariables.value(1) ) value = $gameVariables.value(1) - itemCount;
    }
    $gameParty.gainItem($dataItems[this._params[0]], value);
    var gained = this._params[1] ? " lost!" : " gained!";
    var itemName = $dataItems[this._params[0]].name;
    var iconID = $dataItems[this._params[0]].iconIndex;
    if (value + itemCount == $gameVariables.value(1)) $gameMessage.add("Picked up only " + "\\i[" + iconID + "] " + itemName + " x" + value + " - Inventory full!");
    else $gameMessage.add("\\i[" + iconID + "] " + itemName + " x " + value + " " + gained);
    return true;
    };
     
    #16
    Uzuki likes this.
  17. Uzuki

    Uzuki Kawaii on the streets, Senpai in the sheets Veteran

    Messages:
    1,841
    Likes Received:
    1,257
    Location:
    Georgia
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg This is fantastic! Thank you so much for your help! Is it ok if I use this for a commercial project? I'll be sure to credit you and send you a copy of the finished project.
     
    #17
  18. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,992
    Likes Received:
    10,107
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Sure, you can use it in a commercial project without a problem. Half of the code was taken from my scripting/plugin making tutorial series anyway, so I can't really hold it hostage even if I wanted :D
     
    #18
    Uzuki likes this.

Share This Page