Taking away Inventory, adding items, then adding old Inv+New Inv.

Hey all, I'm not too keen on script calls, but I can figure things out pretty well. However, since I'm
not the best at trying to get things to work, I am trying simply to do this scenario:

You enter a tournament, in which your current item inventory is saved, then cleared using this code.


$gameVariables.setValue(169, $gameParty._items);

$gameParty._items = {};

Then, you enter the tournament, get thrown in jail for a crime you didn't commit. You break out of jail, and then find other items along your way. Later, you meet up with another party member who happens to have the stuff
you had on you prior to entering the tournament. I want to be able to add variable "169" and new inventory
variable "170" together to form your new, current inventory.

This is what I tried
RPGMV_uHDoJU9uOs.png

As you can see, it saves the 2nd inventory as a different variable
and then attempts to add the two together later.
And it led to getting this crash:
game_hFyEGx6kg1.png

Obviously, my skills at syntax are not the best, and i was wondering what the proper script is to input to do the desired result. Any help is appreciated, and thank you in advance.
perhaps many variables saving how much of each item the player had before them getting thrown into jail, (for example if they had 2 sodas a variable that is something like soda amount (before jail) set to 2.
Then when they can combine their inventories go through each item variable and add how many they have. . .hopefully not too many different items by the described point. . .
perhaps many variables saving how much of each item the player had before them getting thrown into jail, (for example if they had 2 sodas a variable that is something like soda amount (before jail) set to 2.
Then when they can combine their inventories go through each item variable and add how many they have. . .hopefully not too many different items by the described point. . .
This route would be far too arduous to put in. When you get put into jail, you are really far into the game and have many key items as well. That part is easy to save. When you break out, you add other items, going through a sewer and fighting a boss, then you're reunited with your old stash + new one. to do all of that would just fundamentally be like adding 100+ different variables instead of cleanly doing it in one line like I'm attempting to do.
Well, this endeavour is already full of holes.

First of all, are you aware what this code really does?

JavaScript:
$gameVariables.setValue(169, $gameParty._items);
$gameParty._items = {};

That doesn't empty the player's inventory. Any weapons or armour they had will still be in their inventory. It only removes items, key items, and hidden items. If this was your intent, fine, but I'm mentioning it because I bet it wasn't the intent.

Next, I think there's no real point in using variable 170. Putting the new items into a variable doesn't really help with the matter of fusing the two together.

Anyway, to get down to the actual question, I believe this code in a script call should work to add all the removed items back:

JavaScript:
let items = $gameVariables.value(169);
for(let itemId in items) {
    $gameParty.gainItem($dataItems[itemId], items[itemId]);
}
Well, this endeavour is already full of holes.

First of all, are you aware what this code really does?

JavaScript:
$gameVariables.setValue(169, $gameParty._items);
$gameParty._items = {};

That doesn't empty the player's inventory. Any weapons or armour they had will still be in their inventory. It only removes items, key items, and hidden items. If this was your intent, fine, but I'm mentioning it because I bet it wasn't the intent.

Next, I think there's no real point in using variable 170. Putting the new items into a variable doesn't really help with the matter of fusing the two together.

Anyway, to get down to the actual question, I believe this code in a script call should work to add all the removed items back:

JavaScript:
let items = $gameVariables.value(169);
for(let itemId in items) {
    $gameParty.gainItem($dataItems[itemId], items[itemId]);
}
For the first code, yes. I understand that it only removes items/hidden items/key items, as was my intent. weapons/armors remain as the character is allowed to use those for the tournament. The first line states to set a variable to the current inventory. Then the second line sets the inventory items to null. I just wanted to restrict the player from using items in their inventory during battle to heal, or to cheese certain fights with consumable weapons like bombs. It does what I intended to do, saves the items you have at the time, and then clears them. That part works.

What I am trying to do, is capture a SEPARATE inventory instance in a variable, one you gather after the tournament, and then adds that variable to the new one to create a third, entirely separate inventory. So if I have 30 soda/10 berries. Save them, lose them, inventory is now null. Then get 40 rocks, 10 gemstones and 10 sodas. Save those. Add those to the original inventory, getting 40 rocks, 40 sodas, 10 berries, 10 gemstones. This is a basic example, but the inventory is large, I'm talking over 60 different items taken in the initial variable save. Then adding 10 or so different items, then adding those cleanly to the inventory previous to create a new one.
Ah, so you're saying the inventory will be saved and cleared. Then, after you get more items, it will be saved and cleared again. Then, after you get more items, the two saved inventories will be merged into the final inventory.

Still, if that's the case, the code I posted should work. You'll just need to do it twice instead of once (change the variable ID for the second one of course).
Ah, so you're saying the inventory will be saved and cleared. Then, after you get more items, it will be saved and cleared again. Then, after you get more items, the two saved inventories will be merged into the final inventory.

Still, if that's the case, the code I posted should work. You'll just need to do it twice instead of once (change the variable ID for the second one of course).
It worked, and here is the solution for anyone in future who needs to do this exact scenario, with explanation. Event 1:
RPGMV_QAHFDxLvNe.png

Script saves the value of "your current ITEM inventory" in a variable. In this case, "169" is the ID I used. Second line of the script sets your ITEM inventory to nothing.
The change items function is used as a test to indicate "other items gathered after inventory has been cleared".
Event 2:
RPGMV_KmEewu0tSZ.png

Third script defines "items" as equal to variable "169", then adds current item inventory to previously saved inventory "169.

Thank you for your help and this forum helps me a lot with learning javascript despite not knowing anything from the beginning and I appreciate anyone who leaves explanations for future question askers like myself!
Like I said in my original post, that's overly complicated. If that's what you're doing, you don't need to use variable 170 at all. You can just use the code as I originally posted with variable 169, and you'll get the exact same effect.
Like I said in my original post, that's overly complicated. If that's what you're doing, you don't need to use variable 170 at all. You can just use the code as I originally posted with variable 169, and you'll get the exact same effect.
You're correct and I edited my post to reflect that.

Original thread: https://forums.rpgmakerweb.com/threads/taking-away-inventory-adding-items-then-adding-old-inv-new-inv.181751/