- Home /
Add random amount of random items from one list to another?
I have a list that contains every item in my game and a list list for chest contents. I want to randomly choose some items from the main items list to put in the chest list, but based on a rarity enum in the item class. Some ideas maybe?
Sorry I don't have any code, I'm at school right now, my stuff is at home. I don't think it is that necessary in this case anyways.
I dont undestand what you mean when you say based on an enum, the enum deter$$anonymous$$es the chance of the object to be added or you just want to add objects of certain type deter$$anonymous$$ed by that enum?
a simple way is, all items would have multipliers, very rares would have one, some 2, some 50 maybe, as if they are listed x times. you can find the total number, and generate a random number out of it. then you can project this number to the list item number. simply basic version of weighted random numbers.
Answer by robertbu · Apr 04, 2014 at 05:58 PM
The "hard" part of your question is handling the rarity. I can think of two approaches. Both solutions involve defining a value for rarity rather than an enum. In the first one you need an integer value, and I'd do it in terms of abundance. So say your rarest item is given a value of 2. All other items are relative to the rarest. So if next rarest is twice abundant, it would get a value of 4. And if you had one that was 3 1/2 as abundant, it would get a value of 7.
Once defined, you construct the 'store' array. You populate it based on the abundance value. So you for the item with a value of 7, it will have 7 entries in the list. You populate the chest by simply selecting one from the length of the list.
The second approach is similar to the first in that you assign abundance values, but they don't have to be integers. So say the rarest has a value of 1.0, the next rarest might have a value of 1.5 for example. To get a random entry, you first sum all the abundance values. Then you generate a random value based on all the abuncance:
val = Random.Range(0.0,totalAbuncance);
Then you walk your array adding up the abundance values as you go. When the sum is greater than your 'val', that is the entry that goes in the chest.
Answer by Xavier78 · Apr 04, 2014 at 05:42 PM
well i can push you in the right direction.
first I would make the rarity of the items so
var rarity = #;
and make it into a static var. Then in your chest script make it call your array of items. the chest should have a built in array of GameObjects var ItemsInChest = GameObject[size];
var size = 0; // make that how many items you want in the chest or/also
var min = 10; var max = 15;
function start() { size = Random.Range(min, max) }
// so it chooses random amount of items
now this is just a basic over vew, if you have the array already you can just make it static and then do
var ItemsToGet = 0;
function start() { // make how ever many randum itomes you want to be in chest // get those items from your items array //add those items to this chest array //set amount for those items }
now with out knowing the type of array you are using i have no more i really can say. hope it helped if not i will delete it to save space.
Your answer
Follow this Question
Related Questions
Making a camera list 1 Answer
Using enums to design an inventory system? 1 Answer
How can I remove an item ffrom a list. 1 Answer
Count object list duplicates 1 Answer
my random task generator not working 1 Answer