Help with a script/plugin for multi-party and/or multi-character inventories/gold

What I'd like to do is set up something that:

1) Takes the current inventory of the party (a variable or table? I'm not super advanced with programming, so multiple objects of varying quantities is something I'm not sure how to deal with).
2) Resets inventory to none/empty and/or 'loads' a saved 'party_inventory' variable.
3) Loads/sets the inventory from 1-2 into a pre-specified chest/event/etc.

I'd like to -try- to do this on my own, but I'm not entirely sure where to start.

I basically need to know how to:
A) Save a full inventory
B ) Load it for later use
C) Delete current inventory
D) Possibly have the ability to add/subtract items from an existing inventory, or combine multiple saved inventories.

This is -sort of- for multiple parties to avoid 'bag of holding' type play, which is why being able to combine (if two parties merge into one) would be cool.

I'm sure there's already a plugin similar to this and I'd be cool with looking at that if someone could link me, but I'm trying to get into coding so I want to write my own, I'm just not clear on where to start.

Any help or any recommended tutorials for working with RPG Maker MV/Javascripts would be really helpful.

I'm also instead toying with abandoning global items/gold and instead having an individual inventory/gold(the gold possibly represented as an item: Gold Piece x(#)). I'm not quite sure how to do that. Possibly where you select Item->You're prompted to pick a character in your party, and it loads the inventory/gold assigned to that character.

If someone can tell me how to add that step of 'selecting a character', and I had the previous info added, I could possibly do that.

Item->Locke->Load Locke_Inventory-> Display Locke_Inventory; Upon exit-> Save Locke_Inventory; Clear Current_Inventory, etc.

It's a little late, hopefully this all makes sense. Perhaps this should be two threads but they are related so perhaps not.
Javascript/Plugin Support is normally for queries concerning existing plugins, whereas you want to write your own. I think this is probably better dealt with in 'Learning Javascript'.

Moderator note: Moving to Learning Javascript
There are so many things you require, but i guess you can find some of them here: http://himeworks.com/mv-plugins/
Just take a look at "Actors and Parties" section.
But programming your own shouldn't be too hard either.
If you want to save an inventory, create a global array.
Inside the array simply push an item ID and the amount you're carrying.
Then simply delete all items in your inventory, since it is an array, you can clear it easilý.
Poryg - Could you possibly show me an example of each of those things? Creating an arrray, then adding item ID 1 of the amount currently in the inventory, then how to delete all items in the old inventory?

And/or if anyone can point me to which /js files deal with going into the inventory, changing items (as in the event), etc. that would also be good.
Hmm, looking through the code I realized there is an easier method. You can just save the inventory inside an array of inventories.
So saving the inventory into an array is simple like this.
invArray = []
invArray[0] = $gameParty._items
$gameParty._items = {}

Creates an array and the first member of it will be your inventory, then the inventory gets emptied. The code is completely healthy. I'm not going to explain any further, since arrays are elementary javascript. If you want to learn more about javascript, go to sololearn.com or some other site where you can learn it.
Just note that this isn't the whole function, so you can't just copy paste it. But with a little bit of practice you should be able to finish it yourself.
That's perfect, thanks a lot. I do want to learn how to do this myself, I haven't quite reached arrays yet but this is a good, tangible goal for me to work toward to expand my learning. Thanks again.
I was wondering, do you know if there's a way for me to replace the # for the array element with a variable ID?

Array[v1] or something like that?
v1 doesn't display variable ID, but value of a certain variable. The javascript equivalent to v1 would be $gameVariables.value (1). Which in other words means the value of variable 1. Not sure if I understood you correctly, my head is hurting a bit now.
Okay I'm having trouble with this but I'm not sure why. I'm thinking maybe it's because the array isn't being saved outside of the event but I'm not sure.

I have two events. The first event is supposed to capture the inventory and delete it. The second event is supposed to add the contents from the previously captured inventory back into the current inventory and empty the element in the array.

The first event does clear out the items, however when I go to the second event I get an error: Reference Error: invArray not defined.

Event one I have some text, then 'Script':
var invArray = [];

invArray[0] = $gameParty._items;

$gameParty._items = {};

Event 2 I have some text, then 'Script':

$gameParty._items += invArray[0];

invArray[0] = {};

Also - I have an event to give a few random items just to have something in the inventory.

I'll try out the variable, it's more for keeping track of 'party A', 'party B', 'Party C', etc. with the variables/array index but I may have to work around that (if V1, then invArray[0], etc.)
That is because invArray[0] is an object. And you cannot do math operations with an object.
You have to do it like this.
$gameParty._items = invArray[0].
invArray[0] = {}. Or if you want, you can just do invArray = [] instead, which will empty the invArray.
Hmm. I replaced the curly braces with brackets and removed the += and changed it to =, but I'm getting the same error: invArray is not defined.

Is there anything I would need to do to make sure the array saves outside of event 1 so it can be used elsewhere, like a global save? I'm wondering if this could be an issue of me defining the array within one event and since it isn't elsewhere it's outside the 'scope', I think? I'm not sure of the terminology. Like when you initialize a variable inside a loop and try to use it elsewhere.
Oh, I see!!! I didn't read the whole topic.

You cannot define var invArray in one event and use it in another one. Variables defined in functions mean they are only valid for the functions, unless they are object properties. So no var invArray = [], but invArray = []. A global variable instead of local is necessary.
For the record, you can't use var invArray

and then call invArray

even in two script calls inside one event. It would need to be inside one script to work properly.
Ahh I didn't realize I could make an array without the 'var'. Removing that fixed that and I was able to get the inventory back.

Now I have a new issue. I don't know how to combine the current inventory with the saved inventory.

My test was: 1) Got items from chest. 2) Saved inventory to array, checked to make sure inventory was empty. 3) Went back to the chest to get more items into my current inventory. 4) Went to the 2nd event (to restore items) and rather than having the combined items, I only have what I saved to the array.

Now I'm trying this in event 2 (the restore-inventory event), which I know is wrong because you just said I can't use math operators on an object:
$gameParty._items = invArray[0] + $gameParty.items;

invArray[0] = [];

And it replaces my inventory with vertical letters. :/

Do you know how I'd basically merge $gameParty._items with the contents of invArray[0]?
As I said, you can't just add an object. Adding an object to an object will result in the javascript converting the Object into strings, which will result in "[Object Object] [Object Object]" string.

for (itemVar in invArray[0]) {
if ($gameParty._items[itemVar]){
$gameParty._items[itemVar] += invArray[0][itemVar]
}else {
$gameParty._items[itemVar] = invArray[0][itemVar]
}
}

I have to say, I did not know how to merge them together... But I managed to devise it on the spot 😀
Hmm... this is what I have in my inventory-restore event:
--
(text)
script:
for (i in invArray[0]) {

if ($gameParty._items){

$gameParty._items += invArray[0]

}else {

$gameParty._items = invArray[0]

}

}

invArray[0] = [];
--

But it's still just inserting letters into the inventory.
Yeah, because there was [ i ], but the post took it as a code to make italic letters, so I had to edit the post, check it now.

EDIT: ONCE MORE. I forgot to add it everywhere. But now, after four edits, it is finally complete.
Okay that works great for items, but I noticed weapons/armors aren't transferring. I tried to correct this.

event1 (inventory capture):
(text)
script:
invArray = [];

invArray[0] = $gameParty._items;

invArray[1] = $gameParty._weapons;

invArray[2] = $gameParty._armors;

$gameParty._items = {};

$gameParty._weapons = {};

$gameParty._armors = {};

event2(inventory restore):
(text)
script:
for (itemVar in invArray[0]) {

if ($gameParty._items[itemVar]){

$gameParty._items[itemVar] += invArray[0][itemVar]

}else {

$gameParty._items[itemVar] = invArray[0][itemVar]

}

}
script:
for (weaponVar in invArray[1]) {

if ($gameParty._weapons[weaponVar]){

$gameParty._weapons[weaponVar] += invArray[1][weaponVar]

}else {

$gameParty._weapons[weaponVar] = invArray[1][weaponVar]

}

}
script:
for (armorVar in invArray[2]) {

if ($gameParty._armors[armorVar]){

$gameParty._armors[armorVar] += invArray[2][armorVar]

}else {

$gameParty._armors[armorVar] = invArray[2][armorVar]

}

}


script:
invArray = [];

And this seemed to work! It restored everything, so thanks a lot, I think this one is solved.
Good to know 😀 I forgot that the inventory is separated to three parts to be honest. But well, people learn every day 🙂
No it's great.

I was wondering though, if I wanted to make a function, something like 'inventoryCapture' and 'inventoryRestore', perhaps with an argument to differentiate between parties, where would I store the code? Just to make it more neat since with the inventoryRestore, it's 4 script calls. I'd like to instead just do script: inventoryRestore(1); or something, but I'm not sure in RPG Maker where I would put the function itself. Would I need to make a plugin perhaps?

I guess I could also possibly do a common event but then I'm not sure how to pass an argument to it.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-a-script-plugin-for-multi-party-and-or-multi-character-inventories-gold.82797/