Godot Dictionary basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so today i wanted to give you a quick overview over what are dictionaries and why would you want to use them so first of all i am starting out with basically just an empty scene that just closes the game again when i open it because there won't be any visual elements to this what i'm doing is i'll just be giving it some print statements down here if we need any feedback on what's happening so if i just play this it opens up and closes again just like that now what is a dictionary most of you are probably going to be aware of how a list or an array works in godot so if we have an array it's defined using square brackets and essentially we just put a bunch of things in there we can put any kinds of objects in there really and these are going to be accessible via the spot they are in so this is accessible by zero this is accessible by one this is accessible by two we have these numbers so if we type print array position one it should give us hello and so it does so that is very simple and we can see why it would be pretty fast because we are just accessing a predefined position so the computer already knows where this value is it can access it basically instantly without having to search for it as long as we have this number dictionaries are a bit more complex here so if we say var dict equals curly brackets now dictionaries are defined using curly brackets which is similar enough to the square brackets of an array just to tell them apart the difference here is that our key isn't a number that's predefined 0 1 2 as in a list but we specify a key so we can make our key key 1 and give it the value 12. a second key give it the value hello and let's see numbers are hashable too so let's go with that give it the value factor 2 0 comma 1. so now if we print dictionary and put a 3 in there it prints our vector 2 with 0 comma 1. so technically if we just put numbers in here 0 1 2 this here is identical to how the list functions we have these predefined numbers and can access it with this however why does it still work fine when we put something like a string or some other hashable object in here the key in that lies for people who don't yet know what hashable actually means in a hash function what a hash function essentially does is it takes the information that this string contains so whatever you would put into a variable with that string and calculates a number based on that which can be used to identify where this value is stored efficiently so essentially the reason we care about this is because without a hash function to figure out where this thing actually here is we would have to search through the entire dictionary to find the correct value let's say this dictionary was really really long it had like hundreds of values in there then to find the 99th value we would have to iterate through the entire thing to figure out where the corresponding key is since we don't have a specific number but the hash function simplifies all of that by just calculating where the number would be using a hash table to get there almost immediately that makes dictionaries really fast even though we use complicated things like strings to access our values now why do we actually care why are we actually going to want to use something like this the reason is simple we can represent much more interesting data structures using a dictionary while still having them human readable if we have something like this in the array this 12 and this hello and this vector 0 1 could essentially mean anything there's really no way for a user to know without checking the documentation what these values are representing there's some number there's some string but these could all be important pieces of information that i used for example to store the location of a character when we are leaving a scene and want to be able to reload it later so while for a computer it might be fine to just use lists and keep them in order and not really care about what it is oftentimes it's a lot more convenient for actually programming and for maintenance and stuff like that to be able to say our character's height is 12 our character's name is hello and our character's position is vector 2 0 comma 1. so like this we would have information that is actually readable we can actually tell what these things are and we can efficiently access them by just using this string here and waiting for the program to hash it automatically and get us the correct value that makes dictionaries really great for saving data later on and let's see if we just print the entire dick there it's quite easily human readable height is 12 name is hello position is that we can easily print it out and see what is inside of a dictionary and we can easily access it in code like if we want to say print dictionary of name then we already are going to know yeah this is going to output us the name so that's fine now one issue that is still gonna happen if we access it like this is we don't know what's gonna happen if this value doesn't actually exist so let's say we take the key name two this isn't a thing up here so let's take a look and there we go it freezes invalid get index name two on base dictionary this doesn't exist and it doesn't know what to do with that now in some cases you might want values that don't always exist so if you haven't included it yet you don't necessarily want to add it to the dictionary in that case there's a neat thing we can use which is the function dict dot get now here it already tells us we can put a key in there and set a default so we can say get name two and if that doesn't exist we use the default steven so now if we print that since name two doesn't exist it's going to just give us steven and not complain at all this works fine and this can be used to do things like this let's say we want to count something inside of our dictionary then we can say dict at position or string counter equals dict dot get counter comma zero plus one so if this already exists if counter is already up in here then it's fine it's just going to use whatever value is currently in there and increase it by one if that isn't a thing yet it's just going to create it because it will just start out with zero add one there we go we have one now so we can just say 4i in range of 10 we do this and then afterwards we can print dict at position counter let's run that and it gives us 10 because that's how often it counted up simply enough if we were to just try accessing predict at position counter of course that's not going to work because let's see similar to before invalid get index counter doesn't exist as for the actual value here in the back you can put anything you want in here even another dictionary say you want an inventory you can technically put an entire inventory in here and say this contains other stuff it doesn't really matter but i think this about gives you a brief overview of the most important features for a dictionary this will be all for today bye you
Info
Channel: iaknihs
Views: 603
Rating: undefined out of 5
Keywords:
Id: K6mTDnHX6UE
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Tue Mar 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.