Character Stats in Unity #1 - Base Implementation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up this is Grizz and in this video we're gonna be making a system to manage these stats or also known as attributes of your characters in RPG games but you might be wondering why would we need a system for that we can just slap on an int or a float variable and it's done well depends in some cases that might be just good enough but when you're having your stats modified by armor talents your skills or spells that you cast and the plethora of other things well it's a pretty good idea to have a system that keeps it all organized and without any further introduction let's get right into it we'll start by creating two c-sharp scripts the first I'm going to call character stat and the other one I'm going to call step modifier let's open them up in Visual Studio and we can start by deleting all of the Unity default stuff including the monobehaviour part we don't need these classes to extend from monobehaviour since we are not going to be attaching them to any game objects directly let's do the same for the other script too we can also delete all of the using statements at the top of the step modifier script and in the character stat script we can delete everything except for system collections generic there's two main things that are going to define our stet the first is the base value which I'm going to declare as a public float and the second is a list of set modifiers that's going to hold all of the step modifiers that have been applied to our set this list is going to be private and read-only the read-only modifier on a variable declaration means that we can't alter that variable except when we are in the constructor of the class or in the declaration itself this means that we won't be able to accidentally modify our step modifiers list by assigning it null or assigning it a new list and breaking stuff that was dependent on the original list we still can however add remove and modify elements inside the list itself let's also declare a constructor which is going to receive only the base value as an input parameter and it's going to initialize the list keep in mind that since this is a constructor we can assign values to the step modifiers even though it's read-only as for the step modifier let's declare a public read-only float that's going to have the value of the step modifier and let's also declare a constructor to initialize it now that we have the base structure of our modifiers in our stats in place we need a way to actually add modifiers to our set we need a way to calculate the final value of our stats taking into account the base value and all of the set modifiers that have been applied and we also need a way to retrieve that value so let's start with having and removing modifiers we'll create a method that's going to receive as an input parameter the modifier that we want to add and it's going to simply add it to the step modifiers list for the remove modifier method we will do something very very similar to calculate the final value of the stat we are going to start with the base value and then we are going to go through the entire list of step modifiers and we are just going to add all of them together lastly we run the value to avoid float calculation errors later on when we start adding percentage modifiers to our stat we will inevitably end up with values that are not whole numbers and besides that we will inevitably have to deal with float calculation errors by rounding to four significant digits it's usually precise enough and it gets around those errors pretty well however feel free to change this to hit whatever your needs are to use the run method we need to include the system namespace at the top of the script and lastly we need a way to retrieve the calculated final value if you're like me the fact that we're calling this method every time we need to return the value is probably bugging you so let's avoid that by making the following changes add a private pool called is dirty this will serve to indicate if we need to recalculate the value or not this variable will have a default value of true and they float called underscore value and this will hold the most recent calculation that we did now we can change the value property to only calculate the value if changes have been made [Music] we must also change our air and remove modifier methods to set is dirty to true so that we know that modifiers were added or removed great we can add flat modifiers to our stats but what about percentages okay so that means there's two types of stat modifiers let's declare an enum to define those I'm gonna do that at the top of the step modifier script you can put this pretty much wherever you want in its own script even but I'm gonna do it here now we need to change the stat modifier class to take these types into account and we also need to change our calculate final value method to deal with each type in a different way if it's a flat modifier we do what we were already doing we just add it to our current value but if it's a percentage modifier we actually need to calculate it in a different way we're assuming that if we have a modifier that's increasing our stat by 10% it's going to have a value of 0.1 which means that if we do one plus the value of our modifier we will get one point 1 which means 110 percent so by multiplying that with our stat value we are transforming it from 100 percent into 110 percent which is exactly what we wanted okay we can now deal with percentages but our modifiers are always applying in the order that we add them to the list let's say there's an item that gives us 20 strength and the talent that increases our strength by 10 percent in our system if we equip the item after gaining the talent the 10 percent from the talent won't apply to the bonus from the item that's probably not what we want so we need to tell this calculation a specific order in which to apply each modifier let's do that by declaring a new variable in the stat modifier class it's going to be an int and they'll call it order we'll also declare an extra constructor that allows the user to define his own custom order when creating a stat modifier for the current constructor we will assign a default order value that makes sense according to the stat modifier type if you've never seen this you're probably asking what the heck is up with this line so we've defined the constructor that requires the user to input all three variables down here we have defined another constructor that requires the user to only input the value and the type however this constructor will automatically call the other constructor passing it in the value supplied the type supplied and the inch representation of the type that's going to serve as the default value for the order another question you might be asking is how the heck does casting type - int even work well in c-sharp every in am value is automatically assigned an int value then index if you will if you hover your mouse over the enum value you can see in the tooltip but it's index is by default the first in uhm value is going to have an index of 0 the second is going to have an index of 1 the third would have an index of 2 and so on and so forth and so by casting an enum - int we are just getting its into value aka the index so what this means for us right now is that flat modifiers will have an order of 0 and % modifiers will have an order of 1 and so flat modifiers will apply before % modifiers yay but we're not doing anything with the other variable yet so let's head back to our character stat script and tell it to organize our modifiers according to their order to do that we can call the c-sharp list method called sort but by default this doesn't know how we want our modifiers sorted we need to have a way to tell it to use the order variable in order to sort our modifiers according to each other for that we must implement a comparison function and we must tell our sort method to use the comparison function we just made if you don't know what a comparison function is as the name implies it compares two objects and for each pair of objects there's three possible situations the first object a should come before the second object be in the list and so we return a negative value the first object should come after the second object and we return a positive value or the two objects have an equal quote/unquote priority and so we return zero and now that we're sorting our modifiers when we calculate the final value the modifiers will already be in the correct order that we want them to and so we don't need to do anything else here and with this we are already at a pretty good basis for a stat system so stay tuned for the next episode oh and by the way I will be reading and replying to every single comment so feel free to leave your suggestions and questions down below see you next time [Music]
Info
Channel: Kryzarel
Views: 88,262
Rating: undefined out of 5
Keywords: c#, unity, unity3d, unity2d, unity game enging, unity engine, stats, attributes, rpg stats, rpg attributes, dnd, dungeons and dragons, stat sheet, character stats, diablo 3, diablo, stat modifiers, stat modifier, stat bonus, stat bonuses, programming
Id: SH25f3cXBVc
Channel Id: undefined
Length: 10min 34sec (634 seconds)
Published: Thu Nov 23 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.