Randomized Shop Inventory
Want your shopkeepers to sell something different every now and then? This snippet script call will let you randomize your shopkeeper's inventory!
I made this little code snippet for my game because I'm working with a time system and wanted to have the shopkeepers randomize their goods daily. It's short and sweet, so all you have to do is copy paste it into a script command and then change the values on the first line to fit your needs.
Step 1 - Randomization Code:Paste the following code as a script call on the event that will randomize the shop's inventory.
var id = 0, type = 0, min = 0, max = 0, inv = 0;
var array = [], temp = [], rand;
for(i = 0; i < inv; i++) {
do { rand = Math.floor(Math.random()*(max-min+1)+min);
} while (temp.indexOf(rand) > -1);
temp[i] = rand;
array[i] = [type, rand, 0];
}
if(Array.isArray($gameVariables.value(id)))
var array = $gameVariables.value(id).concat(array);
$gameVariables.setValue(id, array);
ONLY change the first line of code. Replace the "0"s with the following information:
id = the variable id that you want to store your randomized shop data in
type = the type of shop items you want to randomize (0 = items, 1 = weapons, 2 = armor)
min & max = the minimum and maximum item ids to work with (i.e. min = 4, max = 20 will pick any items between 4 and 20)
inv = the number of shop inventory items you want to be randomized and added
Example:
var id = 61, type = 0, min = 16, max = 127, inv = 10;
The above example randomizes 10 items from between ID 16 and ID 127 and stores the info in variable 61.
Step 2 - Calling the Shop Processing:After you have ran the code to randomize your shop's inventory, you will need to use a script call to summon the shop processing using the variable you stored the information in. This is how it's done:
goods = $gameVariables.value(0);
SceneManager.push(Scene_Shop);
SceneManager.prepareNextScene(goods, true);
All you have to do is change the first line and change the "0" to whatever variable you have used.
Examples & Information:(The second script call has a different order of variables. I changed them and forgot to update that one. Oops!)
Resetting the variableOn the very first line in the screenshot, I manually reset the variable to = 0 to erase any previous data in it. This is because the randomized shop items WILL stack when you call the code again unless you remember to reset it.
Mixed Item Shops:The reason I set up randomized items to stack was for the purpose of having mixed types of shops. Basically, having a shop where the vendor can sell items, weapons, and armor at the same time. For mixed items in a shop, simply copy and paste the code another time and edit it. For example (as seen in the screenshot), my first script call randomizes 10 items, whereas my second script call randomizes 2 weapon types.
And after that, all that's left to do is use the shop processing script call! I don't recommend doing this in the same event (like I did in the example), because you don't want the shopkeeper to randomize his goods every time you talk to him. :p
And, that's all!