How to set arraylist and add/remove element from it?

Last time, I had the following script to give the party a random item from an item pool when opening a chest.

pool = [40, 55, 122, 345, 531];


loot = pool[Math.floor(Math.random() * pool.length)];


$gameParty.gainItem($dataItems[loot], 1);



Then, I tried to make the item that the party receive to be removed from the pool and replaced with something else. So I add the following line.

pool = [40, 55, 122, 345, 531];


loot = pool[Math.floor(Math.random() * pool.length)];


$gameParty.gainItem($dataItems[loot], 1);


pool.remove(loot);


next = loot + 1;


pool.add(next);

Apparently, array is static and can't be changed. Stack overflow told me to use arraylist since it is dynamic, except that I don't know how to implement


it inside rmmv. Please teach me!
It's not really static... You can add new item into the array via array_object.push(item) (puts that item into the end of the array). And you can add/remove via the .splice method


your code should be


pool = [40, 55, 122, 345, 531];
loot = pool[Math.floor(Math.random() * pool.length)];
$gameParty.gainItem($dataItems[loot], 1);
pool.splice(pool.indexOf(loot),1); //Tells the engine to remove 1 element of the array from the position of loot.
next = loot + 1;
pool.push(next);





Take note though that your code basically removes the taken item and simply adds the next item in the pool (essentially duplicating that item into the pool)


So let's say you get 40, that means after this code is ran, your pool will be [55,122,345,531,55];


now if 55 is picked, it will only remove one instance of 55 since there are now 2 55s in there. Meaning item 55 can still be picked after


Now we can also make it delete all instances of 55 and make it add the next item in the same amount as the removed 55s. If you want that
So let's say you get 40, that means after this code is ran, your pool will be [55,122,345,531,55];

Eh? But I tried to make the pool [55,122,345,531,41] if 40 got picked. (since the var next = loot+ 1).


Also I run your code..... and there's no changes to the item pool.


But the main point is I want to set up the pool as arraylist instead of array.
Ah yes its 41 lols.. I thought loot was an index at first. XD


I just tested my code on MV, and it worked...


Here is my setup I made it to print on the console the value of loot, the contents of pool after I removed loot, and the contents of pool after I added the new item.


Pool01.PNG


Basing on the console log, the code worked


Pool02.PNG


So there's no need to use arraylist for it. Actually ArrayList is a Java thing which allows dynamic arrays (Java arrays cannot change maximum size once created). On JavaScript, arrays are already dynamic so no need for ArrayList.


Remember, MV uses JavaScript not Java, and those two are very different languages
Remember, MV uses JavaScript not Java, and those two are very different languages

Oh... no wonder I've been struggling like an idiot all this time. Thanks for clearing that up.


Also my mistake:


When I run your script and run it again a second time, I forgot to remove this line:


pool = [40, 55, 122, 345, 531];


No wonder the item pool got reset every time the code runs. ( making me think there's no change in the item pool ). 


Thanks for your tutelage!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-set-arraylist-and-add-remove-element-from-it.73369/