MV [MV beginner] Question about assigning array elements to variables

Hi everyone! Sorry i this isn't the correct place for this, I'm pretty new to this site. I was wondering if anyone could give me some input on why my script isn't working properly. My goal is to write a script that assigns an array to a variable (#20), and then randomly selects an item from that array to assign to a second variable (#21). I tried writing something based on what I read on this spreadsheet, but it doesn't seem to be working properly.

I can successfully assign the array to variable 20 (the text output is 1,2,3), but the second half doesn't seem to be working, as variable 21 always returns a value of 0.

For context: I'm trying to design a loot pool that removes items from the pool when the player obtains them. I'm working on a roguelike, so the idea is that you obtain random items from chests, but you can only obtain an item once. My plan was to assign arrays containing my loot pools to specific variables, and then have a script that modifies the array each time a player collects an item.

Additional information: I posted this same question on Reddit, and someone suggested using “Math.floor(Math.random() * list.length)" instead of “Math.randomInt(list.length)”, but this was unable to solve my problem. Variable 21 still ended returning a value of 0.
1765653930980.png

I've moved this thread to Javascript/Plugin Support. Thank you.


I posted this same question on Reddit, and someone suggested using “Math.floor(Math.random() * list.length)" instead of “Math.randomInt(list.length)”
there wouldnt be a real impact other than randomInt being easier to type and look at

Variable 21 still ended returning a value of 0.
have you tried other variables? have you expanded your maximum variables to include 21?

your code is correct, so my guess would be that you either have a plugin or parallel event updating the value during use, or you never expanded the variable maximum past 20 as variables past the maximum will always return 0 with $gameVariables.value.

1765668620664.png

1765668623210.png
Moved to: Javascript/Plugin Support

there wouldnt be a real impact other than randomInt being easier to type and look at


have you tried other variables? have you expanded your maximum variables to include 21?

your code is correct, so my guess would be that you either have a plugin or parallel event updating the value during use, or you never expanded the variable maximum past 20 as variables past the maximum will always return 0 with $gameVariables.value.

View attachment 354975
View attachment 354976
I see. I went back and redid everything exactly as I did it the first time, and it ended up working. I'm still not sure what exactly I did to prevent it from working, but I can only assume it had something to do with a parallel event updating the value. Thank you for your response!

Original thread: https://forums.rpgmakerweb.com/threads/mv-beginner-question-about-assigning-array-elements-to-variables.181425/