Arrays/Lists in Godot - The Non-Coder's Guide to GDScript 11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on you guys today we are talking about a really important group of variables called container variables and as the name suggests they are just variables that contain other variables and they are really important in game dev typically behind the scenes but important nonetheless today we're going to be talking about one called arrays or lists but i'll quickly show you both that we are going to be talking about there's really only two types in godot and other programming languages they have more but you have the array which is denoted denoted notated by these square brackets and then you also have a dictionary which is denotated denoted by these curly brackets right here the difference we'll get into later except that basically a dictionary is unordered and you look things up in it like a dictionary like if you want to get some information in there we would ask like what the key or the word is and we'd get the definition whereas an array just kind of keeps things in some order now i'm going to change this to list because i like to call these lists because they call them that in python and i think it really suggests more so what they are lists really just contain other variables in some order it keeps that order just like you would a shopping list uh and both of these together are really important in building things like for instance if you're making a multiplayer lobby you know what are the players in your game you have them all in a list or an array uh if you have an inventory system like for instance like a pokemon game you've got the bag um you know you've got like the item slot this slot blah blah blah and items in those slots that's going to be using a combination of dictionaries analysts i mean they're just very very helpful anyway it's very very important we're going to go ahead and crack into it so to write a list you essentially have the option of one just writing it on online or hitting enter and you write like one thing another thing another thing like that um but we're going to be doing it all in one line today and i typically do that with lists that aren't too long like today so you can make these literally anything so i can put one here i can put a string say hello i can put a boolean value right so i can say true okay and now we have a list that contains these three variables obviously these are not assigned anywhere or i'm sorry three data types right we've got strings and we've got this and you can really put anything here you can put a node like for instance if we make an instance of something or for instance we could put bullet scene in here just like that and that still works and now if i go ahead and i say print list sorry if you can hear my pug in the background i'm going to run this you can see that it goes ahead and prints what's in the list one hello as you expect and then this again as we talked about is the kind of identifier of this scene right and so the point here is really just to say like this is a list here is the kind of syntax right we've got the um one item in the list then we have a comma then we have the next item and then you don't put a comma at the end of the list that's a no-no although that doesn't break your game it's just kind of like a very taboo thing to do if you will anyway again as you can see as i said you can go ahead and separate this into separate lines if you wish some people like to do this because it looks a little more organized i do that with my dictionaries but not usually with my list unless they're really long and yeah so we'll go ahead and get into all the things you can do with a list i am first going to go ahead and add a few values to this list so for this example i'm going to be pretending we're basically dealing with the item slot of an old school pokemon game so we've got a pokeball in here okay and then we've got a great ball okay then we've got a potion okay uh then we have a tm 50 and then i'll do one more team uh or we'll do we'll do hm 69 all right okay so again if we run this you can see that now we've just got this list of strings here and here they are and this is our i this is our bag in pokemon now there's a few things you can do with lists that are really important in game right you can pick up items and just in a list you can add items from this list you can remove items from this list and there are several different ways that you can do that now if we go to this list right let's say we're picking up some item what you typically do is append actually let me change this to items let's just delete this spell items correctly okay there we go all right so um all right here we go so let's say that uh in the ready function i'm simulating that my character is picking up a hyper potion right so what i would do is i say list dot append why is it not like this oh i'm sorry items dot append right because items is the name of our list we're calling the function append on it and in here we're going to say what are we adding to this list and in here again we could put anything we could put a 1 we could put true but i'm going to put that we're picking up a hyper potion and again in the real game right this might be a literal item right like an actual node like the scene here that represents a high proportion but right here we're just using a string okay now if we go ahead and print items okay you can see that we have our list at with high proportion at the the end there so i'm actually going to go and do this print items so then you can see that it has changed right we've added the hyper potion and you can see we've added it to the end if i run this as many times you're going to see that these stay in the exact same order and that it goes right there to the end now if you want to stick something at the front of array of the array you can say push underscore front and then you put whatever item you want and this will stick it at the front of the array just like that you can see the hyper portions down the front now before we get into anything else it's important to know something about uh these arrays or these lists and that is that they are ordered and uh each uh item in this list has an index or a kind of position right in that list so we would call this position or index zero right then we have position one two three and four aka negative one i'll get into that in a second but essentially you start with zero and then you just count on so this is something that gets confusing even for experienced programmers because sometimes you forget that the list starts with zero and you you'll mean to like grab item number one from that list and or the first item off that list you'll grab the second item so how you would do that is to simply do you would use this syntax right here if i can type okay so we'd say print items brackets and then we'd say the position or index of whichever one we want so if we want to do the first one again i put 0 here and you can see in the middle it should just say pokeball because that's at position 0. now little quiz if i want to do potion which index should i put in here well the index of potion is going to be 1. i'm sorry 2 because 0 1 2. so even even i will get confused here and we run this and you can see that potion is printed there because we've selected that from the list now the other thing to note here is if we put a negative 1 right this is going to grab us the end of the list right so hm 69 but we can also in this case put a 4 and we'll get the end of the list and that again is just because this list has a size of one two three four five elements right so we know that zero one two three four so four is the end of the list okay but if you just want what's ever at the end you can just say negative one because that's just kind of a shorthand that's helpful so you can grab that and obviously this is useful for things other than printing right if you have a physical potion at the end of the list you can go ahead and grab that now before we keep going into accessing items i do want to say about adding items in the list we can go ahead and add items at a specific point now that we've gotten that established right and that is done by items dot insert and then you can go ahead and stick something at some point so you see first is the position or the index right if i want to put something between pokeball and great ball i would put that at position one and then we'd say whatever it is okay and then we run that and you can see that it has inserted in between these the other thing you can do is if you want to replace one of these items you can do this right we can say items and we'll say zero and then you say equals and then whatever you want right okay and if we go ahead and do that you can see that pokeball has been replaced with ola so yeah you can do that that's pretty important to know and those are those basically how you add things or modify the list now i want to keep going to accessing things in the list again you can do items this and then the index right other ways that you can access items in the list you can check if the list has an item right you can say for instance if um pokeball right and then you can say in items and we'll say print hello okay now of course it's going to print hello because pokeball is in items right so if the list items contains whatever is in this if statement then this evaluates the true and thus this is run now there's another thing that you can do this another syntactic syntactic syntax way you can do this which should go if we can say if items dot has pokeball and that's exactly the same thing dot has pokeball um and that's the exact same thing as what we just did except it's a built-in function and for some people that's just easier to remember all right okay you can also grab things by their value but this is a little bit more difficult and that's not really the point of items if you want to grab something by its value you should be using a dictionary most likely but i'll quickly show you the way you do that and that would be that you use items dot find okay and then you put the value of what you want to find so we'll say for instance great ball okay and then what i'll do is just print this and you'll see what it actually returns is the index so it returns one so if we replace print with items make this brackets here okay and then we do that okay then you'll see that then it returns great ball because we found it and then did that now that might seem like it doesn't make sense because that's not really how you use lists but that's just an example of how you might do that now we'll go about how you remove things from lists right so let's say i use an item let's say i'm using a potion here then i will go ahead and say items dot remove okay and this rep depends on the position so we'll remove something at some position so we'll remove for instance if we want to remove potion remove something at position two and you can see now potion is missing down here you can also do something called erase okay and this erases by the value so here again i would put potion and we could get the same result but instead we're moving it by the value here all right so that is just some of the things you can do with lists there's tons of other ones i highly recommend that you go up to your search help here and search array again i call them lists but they're technically called arrays and godot but a lot of people will call them by both names and that's because that's you know a lot of different programming language it goes between both names there and you can see all the cool functions that it has most of them make a lot of sense once you understand lists for instance you have the count count will tell you how many times one of those so for instance if you do count pokeballs it'll say how many pokeballs are in this list uh if you do uh where is a good one oh shuffle shuffle will just randomly place all them in some order in the list remember that the array is organized such that this is always going to be position zero where dictionary it has no position in the list but if you click shuffle i'm sorry or run the the function shuffle it's going to reach or reorganize all these into different positions alright now what i like to do to kind of go over like how you would actually do this is to make a little kind of simulation of us in our pokemon game in the bag cycling through our list and how that might work so what i'll do here in i'm sorry not in ready but in physics process it'll say if input dot is underscore action just pressed and i'll do ui down we'll say so or i guess we'll do yeah we'll do down okay okay i'm going to pass because i need to make another thing so we say var index we say equals zero okay so we're going to start with an index of zero then what we're going to do here is we're going to say index plus equals 1 okay so we're incrementing our index by 1 every time we hit the down button and then we're going to go ahead and make sure that the index does not go or not become greater than whatever the size of this list is so we'll say if index is greater than the size of the list minus one right so items dot size okay minus one then index equals zero so i didn't actually go over this function which is my bad it's pretty helpful essentially this counts the number of items in the array and that's all it is it tells you the size of the array so the size here is one two three four five right so the size is five here and so thus we can make sure that the index never goes over 4 meaning the last item in here by saying if our index is greater than the item size minus 1 or the list size minus 1 then do that because again the size of the list is 5 but the index only goes up to 4 so size minus one will give you the the maximum index right all right now if i go ahead and print index you can see what we've just accomplished haven't done anything really with the list quite yet but i can go one two three four and then it goes back to zero one two three four zero okay cool now we can do is actually use that to cycle through our list right so we can say print okay and we'll say that we want to print the items dot get or not get i'm sorry just the brackets and we'll say index okay now if we do this you can see that every time we press the down arrow it's cycling through a different thing in our list so we're cycling through our great ball our potion tm50 secret 69 and the next one will be the pokeball because it starts from the beginning so we can see we're going through our bag and it's just kind of cycling through and then so obviously in the real game you would you know show some kind of graphical or visual representation of this on the screen in this case we're just putting it down here in the output but that's pretty convoluted and really just kind of monotonous work this is the programming and difficult concept behind that and we've kind of went over all of that right here in describing items anyway i really hope you stay tuned for the next one we go over dictionaries dictionaries are a lot like lists except again they are unordered uh but anyway i'll get into that in the next episode thank you guys so much for watching and have a great day you
Info
Channel: ACB_Gamez
Views: 373
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: FDqzt2P4EAk
Channel Id: undefined
Length: 16min 48sec (1008 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.