Data Structures For Game Devs: Dictionaries | Unity Tutorial (Part 3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to my multi-part guide on how to make your game dev live easier by using the right c-sharp data structures in unity this video is about dictionaries or hash maps as they are sometimes called and it builds on my previous video on hash sets so if you don't know what hash sets are and how they work i strongly recommend you watch the previous video which teaches you all that so like the name dictionary suggests you can use this data structure to look things up and unlike a hash set which also lets you look things up a dictionary stores not just a single value but key value pairs and lets you access the values by their keys so it's essentially a hash set but each element in the set has an associated value so when defining a dictionary we need to provide two different data types one for the keys and one for the values the full definition of an empty dictionary would look like this if we want to immediately add items so in this case key value pairs we can do so like this note the extra braces around each key value pair so the dictionary data structure works pretty much how a real world dictionary would you want to look up a word the key and since dictionary entries are usually sorted alphabetically it's easy to find that key in order to then check the value associated with it for example a definition for a given word time for a first game dev example let's imagine your game character can collect and equip clothes and maybe hold an item in each hand you could now keep a dictionary where each key represents an equipment slot and the value represents which item is equipped in that slot like we did in the video on hash sets let's use an enum to represent different equipment slots then you can use the equipment slot type for your dictionary keys and define your dictionary like this just like the elements in a hash set the keys in a dictionary are unique and you cannot add the same key multiple times you could however add multiple different keys that all have the same associated value for example our pantry could contain five of each key items so all dictionary keys would point to an integer value of five and again just like element lookups for a hash set finding or modifying a specific key in a dictionary takes a short and constant amount of processing time no matter how many elements a dictionary contains that's because it uses a so-called hash function to look up keys which we covered in the video on hash sets so going back to our inventory when the player equips a new item you don't need to first find and remove the currently equipped item for that slot you can simply overwrite the existing value for the equipment slot if the slot is currently empty you will instead add a new key value pair to the dictionary as you can see accessing an item bytes key looks similar to accessing a list or an array by an index you could also use the add method to add key value pairs but this would throw an exception if the dictionary already contained the key you're trying to add definitely check out the documentation for any data structures you use and especially remember to skim over the properties and method sections to get an idea of what else it offers i also find that the remarks section often has some bits and pieces that might be interesting keep in mind that unity scripting runtime typically lags behind the latest c-sharp standards so you may need to select the correct.net version at the top of the reference page which is currently framework version 471 for me anyway as you can see using the right data structures and making them do the work for you can potentially make your life a lot easier and save you a lot of code and potential bugs because let's face it more code always means more bugs by the way another potential use case for a dictionary is staring at us right now i'll let you know what i'm thinking of in a moment but maybe you'll figure it out in the meantime anyway if you're completely new to dictionaries you may not realize that your values can be all kinds of data types including not only game objects and classes you've defined but also for example lists of anything you like for example you could store a list of enemies for each enemy type in a dictionary with keys indicating the enemy type and values listing the enemy game objects and just like you could store lists as your dictionary values you could also use any other container for example a stack or eq which we will cover in the following video you could also put hash sets in your dictionary imagine a city builder game where you want to indicate whether the player is allowed to place structures on the terrain we could create a dictionary that lets us look up the allowed terrain types for each structure type for example a house could be allowed on sand concrete and grass but an offshore platform makes more sense in water or maybe on a cliff your dictionary could then look like this in code again we assume that structure type and terrain are enums if you're really evil you could also make them strings then to check if a house can be placed on grass you can just check the dictionary we can now shorten this a bit by skipping the intermediate hash set variable and in reality this code would probably not be specific to houses and graphs but a general purpose function pro tip for functions that return a boolean value and primarily exist to give me a yes or no answer to a question i like to prepare is to the function name note that we could also call this function structure allowed on terrain but that could also mean that it returns a structure you know the structure allowed on the terrain and not a boolean and finally a bit of a funky example of storing dictionaries in a dictionary imagine a world that has shops the player can visit to purchase items where each shop offers a subset of items you could now again keep a dictionary where the keys are stores and the values are hash sets containing all the items the store sells but what if you want to also store the price of each item along with it we can do so by instead using dictionaries as dictionary values the blacksmith and carpenter are each shops so there are the dictionary keys the values in this dictionary are dictionaries of the items that the store sells and how much they cost note that the shops may sell the same items at different prices like colon this example if that wasn't the case we would probably want to just store hash sets of the sold items and keep a completely separate single dictionary of all the items in the game and their respective global prices so in code that would look like this if we now want to check the price for an item at a specific shop we could do so like this you can also use the safer variant try get value when accessing dictionaries which will not throw an exception if the query key doesn't exist but the code for this will be a bit longer so let me know if you have any questions on how you plan on using dictionaries now that you're an expert on them and make sure to check out the next video on queues and stacks oh and for the example earlier you could use a dictionary to keep track of the item bonuses the keys are the categories like logic or authority and the values are simple integers that you can adjust whenever the player equips or removes an item let me know what you came up with thanks for watching
Info
Channel: Anni
Views: 1,553
Rating: undefined out of 5
Keywords: game development, unity tutorial, data structures, c# tutorial, game dev tutorial
Id: vditeSiJdSY
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Wed May 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.