The RPG Maker Resource Kit

RMRK RPG Maker Creation => MV => MV Event Systems => Topic started by: yuyu! on June 01, 2016, 05:01:44 AM

Title: [MV] Randomized Shop Inventory
Post by: yuyu! on June 01, 2016, 05:01:44 AM
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.

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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:
Spoiler for Screenshot:
(https://rmrk.net/proxy.php?request=http%3A%2F%2Fi.imgur.com%2F8rrS0MY.png&hash=b5a84134ad74b082ebd6dac424a37be4c042c35a)
(The second script call has a different order of variables. I changed them and forgot to update that one. Oops!)

Resetting the variable
On 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! ^-^
Title: Re: [MV] Randomized Shop Inventory
Post by: doug on June 28, 2016, 02:46:24 AM
I have been searching the internet for something like this for days, and was so excited to use it, but I'm getting "Type Error: Cannot read property 'setupGoods' as null"

I'm not sure what that means or what I'm doing wrong. Is there anything you can do to help?
Title: Re: [MV] Randomized Shop Inventory
Post by: yuyu! on June 29, 2016, 11:14:45 PM
Sure, I can take a look! :)

Can you provide a screenshot of your script calls? My guess is that some crucial variable is not set-up properly and is defaulting to "null".