Dictionaries in Godot - The Non-Coder's Guide to GDScript 12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody today we are talking about dictionaries dictionaries are the second type of container variable uh the first one being list which we talked about in the last episode remember that a list is an ordered container meaning that every item in the list is in the same order no matter how many times you ask to retrieve that list right list so if we ask for the first item in items here or the first whatever is at the first index which is index 0 we will always get pokeball with dictionaries however they are not ordered by their position in the dictionary but instead by a key value pair just like an actual dictionary where you have the word with a corresponding definition and the benefit of that is kind of behind the scenes dictionaries are a little bit faster to use but you can't really iterate through them which is something we'll get in the next episode but you can't really go through and look at every single part of a dictionary where the list you can look through the entirety of the list right we can look through every single point of this and see like oh uh you know you know what is at this position what is it this position this position and do all that type of stuff a dictionary is really when you know what you're looking for and you want to find that right you wouldn't look up a dictionary and look through the entire dictionary and then find what you want you know what you want you skip to that point and then so it helps you in that regard so that made no sense that's okay we're going to go over dictionaries right now and i'll show you exactly how i would use one with the example of a bag so the bag we're going to try and make it somewhat like pokemon again with the dictionary we use the curly brackets instead of these straight brackets here and then just for formatting purposes and totally optionally i'm going to go ahead and hit enter and that's going to put this on a two lines down and give us a separate line here now instead of writing a single item at some position we write a key which can be read like this key okay then we write a colon and then we write a value and again these can be whatever you want these don't have to be strings i'm just going to be using strings right now but these could be integers these could be boolean values these could be vectors these could really be anything you want but you cannot have two of any of the same key if you if i go here again you can put a comma then next line i can't write key again because you can't have two of these same key right you can have two the same value but you can't have two the same key so this should yell at me in a second yeah duplicate key found in dictionary okay anyway so that's basically how you write a dictionary and we could have tons and tons of different keys with different values and i'll go ahead and make this a little better of an example here well i guess first what we'll do is we'll show you how to retrieve values from the bag so what we'll do is we'll say like you know print bag and then again we can do this but instead of putting a number for the position we'll just put the value of whatever we want to get print bag dot key then we can hit that and we can see that it prints value whatever is at key now the problem with this is if you put like for instance ky you misspell it maybe you check maybe you're modifying this bag as you go and you run this it'll it'll break if that does not exist so dictionaries have a function called get which is what you should always use when you're just trying to get the value of some key rather than actually modify the dictionary and this does the same thing but if the key does not exist it just returns null which prevents your game from crashing which is really nice so that's really important in order to get information but the idea here is that you give it a key and you get the value so for instance in our case um in the newer pokemon games right we have for instance like uh pokeballs okay and then we'd have like uh for the value here this might be a list right and so here we're putting a list within a dictionary because we have all the times the pokeballs we have pokeballs pokeball then we have a great ball right okay and then we'll come back down here and now here we've got our next key right so our first key was pokeballs our second key will be tms tms and then again only get on the list and we'll say tm 50 and tm 69 nice okay and then finally we'll have potions now i gotta capitalize this okay and then we'll just have regular potion uh and then we'll have a super potion okay perfecto all right so now we've got that should stop yelling at me in a second ah that's why this needs to be wrapped in brackets like that okay cool now again so these right now the value is a whole list that contains values right and that might be confusing this potion could literally be 1 right and that would be okay but for like making it an actual example what we want is to actually have those items exist so again we can just print bag okay if we run this you can see that it prints the entire dictionary like that we can also do print bag.get right potions remember that capitalization matters and then as you can see right there it prints what potions we have and then if we want to modify any of these we do something very similar to what we would like in the lists right we would go this and then we'd input the key so let's say i pick up a potion potions right and so essentially let's get rid of print here and then here we'll do print bag.get potions okay so we have potions so bag potions this is returning to us this list and then what we want to do with this list well here i guess i'll do it like this we'll say var potions equals that and then we'll say print potions okay and then what we'll say here this will say potions dot append remember because we have a list here and we'll say hyper potion okay now if we run this oopses that's why as you can see here we've added now the high proportion to that list and so that's because we've grabbed this and done it like that and there you have that right there so as you can see what we did here is we grabbed the potions key whatever the value was the value is this list of potions and what we did is we went ahead and added the hyper potion to that using the append function which we learned in the last video on lists because we are operating on a list now see if we print potions here this is going to print that list okay so now i want to quickly go over a few important functions that dictionaries have in them again just like the items has a has function right if x in we can do the same thing with dictionaries we can say if bag dot dot has and then we can put in any kind of key right so for instance tm right print uh hi okay so do that example it's high and then if we change this to be like ts or something that's not in the list and it then doesn't work it's that simple that is really helpful and that only searches the keys now you can also do something pretty cool which is something we'll definitely use later on which is we can do print we'll do prints and we'll do bag dot keys okay and this returns a list with all of the keys in the dictionary right so pokeballs tms potions then we have bag dot values and what this does is this returns all the values in the dictionary as a list so if we print this we can see that we've got well here you know what i should do is do we'll do that okay okay so you can see we've got one list which is pokeballs tms potions and that is our keys and then we have a second list which is our values and so this is a list of three lists one two three and that's how that works okay coco last one i want to talk to you about you have the same function again you have the size function and the size function again will tell you the number of keys in the dictionary so if we write print bag.size right it will go ahead and say three because there's three keys in the dictionary so now what i'd like to do is kind of put everything we've just done into practice using what we know about dictionaries and lists to make a little program that allows us to kind of sort through the bag and have it print out what pocket we're in and what is in that pocket so i'll quickly make an on-ready variable right remember this is going to happen on the ready and we're going to say actually this could probably just be a regular variable say var and we'll say keys equals bag dot keys okay again this is returning to us a list of all the keys so i should say pokeballs tms potions let's do this print keys let's make sure that works there we go okay so we're going to make another variable i'm going to say var key and then we're going to say equals 0. okay now we're going to do is go down to our physics process and we are going to say that if input dot is underscore action just pressed ui we'll make it down so we can go down right if if input is action just press ui down we're going to say key plus equals 1 okay so we're going to increase our key and then we're also going to say that if key is greater than we'll say e3 well actually we'll do is uh if key is greater than keys dot size okay then key equals zero so essentially what i've just done here is that when we press down we're going to increase our keys we'll go 0 1 2 3. yeah and then we'll say if our key is greater than the size of all our keys this should actually be size minus one right because we start at zero then key equals zero and then what i'll also do right here is say print key and this is just to show you what's happening so i'll go like this one two zero one two zero so that way i've kind of basically clamped this at a value between one and zero and it can't go i'm sorry zero and two and it can't go above two right so then what we'll do is we'll say print key i'm sorry keys dot i'm sorry not dot say print keys dot or keys key right so again the key is going to be a number like that and then what we can do here is do this so we're in tms we're in potions we're in pokeballs tms potions pokeballs and now we're running through that right so we've got that and then what we can do here is basically say print so we'll keep that we'll say print keys key and then we'll say print bag dot get we'll even do this we'll say var pocket equals keys key right just like that we'll say print pocket okay and then just to show you again this is going to do the same thing potions blah blah blah now we'll do this we'll say print bag dot get pocket okay and so now what's happening is we're shifting we're going through our bag when i hit down right here and so we're just looking at the output down here again and so i'll hit down i'll go into our pokeball slot you can see i have a pokeball and a great ball if i go over to our tm slot i've got tm50 1069 and i can shovel through the bags and of course i can go ahead and do the same thing by saying if the input action just press ui up then we would go minus one right and so that is the end of the example that just shows how we've incorporated learning a little bit about our both the lists here as well as our dictionaries and we are able to make a basically functioning bag now in an actual game what you'd of course do is make a visual representation of what you're doing here right so we'd have like the pocket that says tms and then we'd have a list of all the tms we have each one of those would be selectable and stuff like that but the idea is still the same we've actually made a functional inventory system and all it is is just this simple uh variable here that is a dictionary container and that's that uh in the next episode we are going to be talking about looping and looping is really important because looping allows us to sift through every single item of some list or even a dictionary and check it we can do things with each and every one in that list and that's really really important and that is one of the reasons we actually learned these things so thank you guys so much for watching i hope you learned something and have a great day [Music] you
Info
Channel: ACB_Gamez
Views: 302
Rating: undefined out of 5
Keywords: Godot, Video, game, dev, development, beginner, tutorial, script, programming, coding, non coder, noncoder, Tutorial, Indie Game, Game Development, Learn gamedev, Gamedev
Id: oyAMRIC-Twc
Channel Id: undefined
Length: 15min 35sec (935 seconds)
Published: Sat Aug 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.