How do I assign a script variable to a game variable?

I have an item that when clicked can give the player random loot. I want the quality of the loot to be weighted so that a common item is more likely to be awarded than a rare item, for example. I also want to use variables to control how the loot is weighted so if (for example) it's raining out the rare item might be more likely to be awarded, and if the player is wearing their lucky ring the rare item is even more likely to be awarded.

So I cobbled this code together from some examples I found on Stack Overflow (forgive the messy formatting; I'm running this as a script so I'm staying within the 12 line script limit):

Code:
var spawnitems = {
    common:$gameVariables.value(90), junk:$gameVariables.value(91), uncommon:$gameVariables.value(92), rare:$gameVariables.value(93), legendary:$gameVariables.value(94) // Weighted Probability
};
function get(input) {
    var array = [];
    for(var spawnitems in input) {
        if ( input.hasOwnProperty(spawnitems) ) {
            for( var i=0; i<input[spawnitems]; i++ ) {
                array.push(spawnitems);}        }    }
    return array[Math.floor(Math.random() * array.length)];
} console.log(get(spawnitems)); // See Console.

BTW: spawnitems is just my name for the script variable, and Variable 90 = 33, Variable 91 = 5, Variable 92 = 3, Variable 93 = 2, Variable 94 = 1

My code runs great and does exactly what I want it to do. Every time I run it it returns to the console either "common", "junk", "uncommon", "rare", and "legendary" based on how they're weighted (so common is by far the most common at 33 as defined by game VAR 90).

But what I can't figure out is how to get a variable in the game to be equal to my spawnitems variable in my script. The console returns the correct value, but I don't know how to get, say, Variable 95 to equal var spawnitems. And the reason why I'd want a variable = to my script's result is so I can assign which quality of loot to give to the player. Thus if the loot is rare, it gives the player a rare item, etc.

Any help would be appreciated. Bear in mind I'm running this as a script so I'm limited by the 12 lines a script can be in MV; I haven't learned how to write plugins yet - one step at a time for me lol.

Also, I'm SURE my code is probably not the best way to do what I'm doing and I'm open to suggestions on improving it, but I would like to solve the main issue first, if that is possible. Though if what I'm trying to do does require a rewrite then I totally understand. I'm still learning so I have no ego at all to crush lol.

Thanks, everyone!
Just a couple of suggestions for looking up quick stuff like this:
- The pinned script call list has stuff organized by category, so it's pretty quick to look things up.

- Basically everything has been asked about the code at this point, so if you Googled "rpg maker mv script variable" the first several hits are threads with the appropriate code in them.

The function is $gameVariables.setValue(ID, value)

So $gameVariables.setValue(95, spawnItems)
After the Code ran, the Variable spawnItems will be deleted.
So putting it into a Game Variable with the Code @ATT_Turan wrote is maybe the solution.
also on a side note on small function.

the amount of lines = 11 or 12 which is a limit, but you can also make it a 1 liner.
saving the code on your desktop or elsewhere if you need to change it.

1 line dont have a limit at all, so you can make a huge plugin and set it as a 1 liner ingame 🙂
(dont do that though).
When I first wrote this code I did use

Code:
$gameVariables.setValue(95, spawnItems);

but I never could get it to return anything. I either get a "0" or an "object Object" being returned. So I assumed this wasn't the correct code to use since I couldn't get it to work (though the rest of the script worked and the console does return the values I expect).

I'm confused because my original idea (and @ATT_Turan reply) is the right code, but no matter where I put it in the script I can't get it to return the value of the script's variable (spawnItems), I either get a "0" or "object Object". I even tried setting it as it's own script, but like @Bex said I can't call that script variable (spawnItems) outside of the script. And I did check my syntax and changed my code so that it's now spawnItems (not spawnitems which I had before).

My question now is where do I place

Code:
$gameVariables.setValue(95, spawnItems);

in my original script? At the beginning? At the end? I've tried everywhere that makes sense to me so far but I'm not doing something corect to get VAR 95 to return the expected values that the console is returning.

Sorry if this is incredibly obvious, I really am trying (and googling), I'm just still learning javascript and I'm at the point where I sometimes "know" the answer, I just don't know how to really "do" the answer (at least to simple java stuff like my script).

Thanks, everyone! Ya'll are awesome as always and I always appreciate the help!!
Sorry i have trouble understanding all the context, sorry language Barrier.
But my big guess is:
You did not learn the difference between both lines yet. If not, iam sorry for bothering.

$gameVariables.value(90) reads the stored information in that Variable90, often used in combination with other code that needs the stored value.
$gameVariables.setValue(95, spawnItems); writes the value of spawnItems into variable 95


Iam no Scripter so iam not sure if your Variable is allowed to have upper case letters.
Make sure you dont use wrong Variable Ids, previously you used 90, now its 95, i was searching for Errors myself until i realised i assigned the wrong Variable XD.

Edit:
Try this as test in a Script Eventcommand:

Code:
bex = 10
$gameVariables.setValue(95, bex)

Edit:
Do you know the Scriptcall List allready? Not for this Problem, but its maybe helpful later on.
My question now is where do I place

Code:
$gameVariables.setValue(95, spawnItems);
in my original script? At the beginning? At the end? I've tried everywhere that makes sense to me so far but I'm not doing something corect to get VAR 95 to return the expected values that the console is returning.
This function sets the game variable to the provided value - so anywhere where this value exists. For example you could replace your console.log with it like so:

JavaScript:
var spawnitems = {
    common:$gameVariables.value(90), junk:$gameVariables.value(91), uncommon:$gameVariables.value(92), rare:$gameVariables.value(93), legendary:$gameVariables.value(94) // Weighted Probability
};
function get(input) {
    var array = [];
    for(var spawnitems in input) {
        if ( input.hasOwnProperty(spawnitems) ) {
            for( var i=0; i<input[spawnitems]; i++ ) {
                array.push(spawnitems);}        }    }
    return array[Math.floor(Math.random() * array.length)];
} $gameVariables.setValue(95, (get(spawnitems)));
Code:
$gameVariables.setValue(95, (get(spawnitems)));

Adding get() to the variable worked perfectly! Thank you!
@Bex I have a copy of the script library downloaded onto my Google drive that I refer to all the time. And it is a fantastic resource that I've learned a ton from. I also highly recommened anyone who uses RPG Maker have a copy of it to refer to.
What does the get() do? Iam just curious.
What does the get() do? Iam just curious.
It's a custom function defined in this script - it picks a random item from spawnitems, with chance weighted by the value of each field.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-assign-a-script-variable-to-a-game-variable.158431/