How to Create a RANDOM LOOT TABLE in Unity C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey game dev HQ today we're going to explore the logic and implementation of a random loop table based on weights let's check it out hey guys john here and i'm so thrilled to finally get this video out random loot drops are insanely popular amongst games like rpgs adventure games and anything that has a selection of drops i hope you enjoy the video and if you're new to this channel we create amazing content every week focused on game development for unity we partner with unity to create some of the best courses in the world and thanks to our members here at gaming of HQ we are able to provide weekly free content on our youtube channel special thanks to the lost o3 steamed poet sizzle stank 6pi code and pixel pop oj zack miss drama castro miss Theo's Dilla doof holger jack b game dev jay jackson 12 172 Johnny Flynn Laurie DK lucky lucky 10 and Mac gamer you guys are professional members of game dev HQ and you all rock let's get to the video here we are in unity and I've downloaded three weapons off of game dev HP's file base so you can see here we have three swords that we're gonna be working with so the first thing we need to do here is we have three items in our game and we want to create a random loot well the way we have to do this is we need to create that table so we're gonna create basically an array which is going to hold the weighted values so here we're gonna say public int and it's going to be an array and we're gonna call this table and we're gonna assign it equal to the array so what we want to do here is item one let's say we want to give it a 60% chance that would look like this we put a 60 for item 1 a 30% for item 2 and a 10% for item 3 so basically these are mapped to each sort so this is going to be sword a sword B and sort C now this weighted table here is out of a hundred if you wanted to make it out of a thousand I'll show you exactly how we can that in the same process will work what's important here is that you determine how frequently in the order of the items that you want them to occur so I want item aid to be most frequently followed by this one than this one if you wanted item C for example to be 60% you can't do this you can't say 60 there and then 10 here it's not gonna work well it's gonna give you it's gonna really just screw up the weights so what you want to do here is associate the item index from I'd say you know most frequent down to rarity so here you have your 60 your 30 and your 10 another great way to represent this is if we actually drop this down and you have each one on a line so here this is going to be sword a which is most frequent followed by sword B and then sword C so here we have our table and the next thing that we want to do is we need to look at the logic for calculating a weight in which one we give you so what is the logic to create a weighted system well the logic of it is pretty simple basically we need to tally the total weight so what is the total weight here right now it's a thousand but it could be an odd number it doesn't have to be an even number it works best if it is a hundred or a thousand even numbers tend to work better but it could be anything so what we need to do is tally the total weight and then we're going to draw a random number between 0 and the total weight now let's run through a scenario by drawing a random number between 0 and the total weight which is 100 for this example let's say you get 49 right so let's say here that random number equals 49 we're on that table do we land well it comes down to understanding how to check this and what we're doing here is we are checking we are checking if the random value is less than the current index of the table so here if random number is 49 we're going to create an if statement here going through our tape we need to basically check is 49 less than 60 which is a wet sword a now is that a true statement is 49 less than 60 yes well you get sword a sword a let's look at another example here let's say you were to get random number 61 so here let's say we got random number is 61 right so random number we run it again random number equals 61 now where do we land which one should we get should we get sword a or sword B well let's check the logic here is is random number which is 61 less than sword a which is 60 is 61 less than 60 or less than or equal the answer is no so if it's no what do we do well we need to actually subtract the value from a so we take 61 which is a random number minus 60 equals 1 and then we check the next one is 1 less than or equal to 30 if it's yes that's your weapon that is how a weighted system works so how do we get 10 well if we wanted sword see you only get a 10% chance so let's say that you had let's say you rolled a 92 right so here we'll say random number equals 92 and what we're gonna check here is is 92 less than or equal to 60 no so we're gonna say 92 minus 60 now we're left with 32 is 32 less than or equal to 30 which is our next weight up here so 32 less 74 to 30 the answer is no so what do we do we subtract 32 minus 30 and now we're at 2 and now we check the next one is 2 less than or equal to 10 the answer is yes you get sword and that's only gonna happen 10% of the time so that's how a weighted system works you have to go through the table so now let's take a look at turning this into actual code the way we're gonna do this here is basically when I hit the space key we're gonna just draw a random number so here at void start what we should do first is tally the total so let's go ahead and create a variable to store the total and then in void start we're gonna calculate it so we're gonna use a for each loop to do that here we're gonna say for each item in table then what I'm gonna do is I'm gonna grab the total and I'm gonna add the item so this is gonna tally up the total and if I run this in unity and now we have 100 and the same concept applies that same logic we just covered if I wanted this to be say 600 300 and then I wanted this to be 100 the same logic applies we tally it up we calculate a random number now between 0 and the total which is gonna be a thousand and then we can do the same process so let's check that out let's keep it out of thousand here so the next thing we're gonna do is once we do that is when we now need to create if statements going through this table so before we do that there let's draw a random number between 0 and our total so here let's create another variable so we can see it in the inspector called random number and the way we assign this is we're gonna say random number equals random dot range we're gonna generate a random number between 0 and the total of all of those weights so if we save this and run it you'll see here it's out of a thousand and let's take a look at where we land which item should we return to the user so random number was 3:17 so the way we check this is we go through our list here or we can do this through if statements or a for loop and we're gonna basically say hey is the random number less than or equal to element 0 its 3:17 less than or equal to 600 then yes you get item element 0 what happens if say 552 was returned which item are you gonna get is 552 less than 6-hundred yes so you still get item zero what if I were to make this 816 is 816 less than or equal to 600 no so you need to subtract 600 so if we subtract 600 we're at 216 is 216 less than 300 yes so you get element 1 and that's how easy it is to create a weighted table so let's look at that in action we know what our random number is now what we need to do is create a for loop or for each loop and iterate through our table and compare the random number to each index so let's do that using another for loop here for each weight in the table I now need to compare is my random number less than or equal to the current weight and that's as simple as that so here I'm going to compare it is my random number less than equal to the current weight and here I'm checking if random number is less than or equal to weight and if it is what does that mean if it is a ward item and let's go ahead here we're gonna award item will create logic to highlight one in a moment and then what we're gonna do if it's false is we need to subtract it so here we're gonna say else random number minus equals the week subtract the weight and let's check the next one so the next time it goes around random number less than the current weight and random number has been subtracted so let's take a look at here at rewarding an item so let's know which item we're gonna award so the way we do this is we're gonna say here debug log and we're going to award plus the weight this is now going to actually print out which weight we should award so it's going to print out either 600 300 or 100 you'll see here that random number was 61 the total is 1,000 and it's saying here award is 100 so let's go ahead and actually tech check that out make sure that's actually correct so here award is a hundred and actually it actually went through so the first initial random value must have been really high because it subtracted a ward pretty lengthly we can actually read calculate what the original value was so here it got to 100 so let's add 300 what that puts us at 361 and let's add 600 to this which puts us at 961 was the initial random number and if that's the case is 961 less than 600 no so we subtracted we're at 361 is 361 less than 300 no so we subtract it now we are at 61 is 61 less than 100 the answer is yes you get element 2 and it says here award is a hundred so we would associate that with an item in an actual game so last but not least I'm just gonna save this I am gonna create something to kind of visualize and highlight which item was selected so here I'm just gonna create a list of game objects to map to my table which is just gonna be some simple lights so here my game object I was gonna say lights and then from here when I award the weight what I want to do is basically I need to know the index here so I'm actually gonna have to convert this to a for loop and here I'm gonna say for int I equals 0 well I is less than the table dot length we're gonna increment I and then here we're just gonna check the current weight so here if random number is less than or equal to the table current index then we're gonna award them else what are we gonna do else we're basically gonna say random number - equals the current table weight there we go okay we can save this and test it out and you'll see here on the main camera we have lights game objects I'm going to associate the lights with the weights here so sort a is going to be the first one so here you'll see that both a and B lit up so what's the issue the issue is that we continued running the program after we selected a table element we don't want to do that so as soon as we set one active we need to say here stop the program so we're gonna return and here you'll see here that green was highlighted so we got sword a and let's rerun it and you'll see again we got sword a so let me go ahead here I'm gonna go ahead and drop this down to say let's go ahead and make this maybe like let's make this 400 you always want to go rarest at the bottom so this one's gonna be like 399 and then let's make this like 5 so we should have a pretty 50/50 chance between zero and one here and there yeah the middle is now called and you'll see here that our random number generated here is 46 and the total weight is 804 so this one because it's not an even number it's a little harder to track back up but you guys get the picture so if we actually were to increase this let's set this one to maybe 200 and let's set the step 300 and let's set that the 400 and let's go ahead and take this one and make that 350 and make this one 400 as well and there we go that time we got soared to C and that's how you create a random link table don't think I forgot about you amazing plus members a thank you for all of your support and we're so excited to have the opportunity to serve you [Music] [Music] [Music] [Music] [Music]
Info
Channel: GameDevHQ
Views: 61,084
Rating: undefined out of 5
Keywords: unity, unity3d, random loot table, how to, how to create a random loot table in unity, rarity system unity, loot drop system in unity, c# loot, loot system in unity, random loot drop system in unity, random loot unity, how to make a weighted table in unity c#, c# unity random table, unity c# tutorial, unity c# random drop table, unity random drop table, create a random drop table in unity, rare item system in unity, c#, video game, gamedevhq
Id: OUlxP4rZap0
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Wed Mar 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.