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):
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!
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!