 In episode 2.4, I'm going to be teaching you guys how to create lists, I'm going to be showing you guys how to use them, I'm going to be showing you guys where to use them, and I'm going to show you some other safety precautions as well. Hello guys, it is Crypto Grounds here. I hope everyone's doing well today. Welcome back to another Unity idle game tutorial video. This is episode 2.4 and today we're going to be discussing lists. If you enjoyed the video, make sure you leave a like below, subscribe to my channel if you're new, and turn on the bell for future notifications of videos and live streams. Anyways, let's discuss lists. So once again, just like the previous arrays video, this is purely for demonstration. None of the lists here that I'm going to make are going to be included in the actual game. This is purely for demonstration. So if you want to follow along, do the same code as me, go ahead so you can understand as well. But just know that none of it is going to be part of the real game. Just thought I would though that disclaimer out. But as I explained in the last video, lists are very similar to arrays. But however, their use case is very different. So lists can be easily expanded, they can be easily added, they can be removed easily. The list is just an expandable array with different types of features. There's a lot of things you can do with lists as well. But basically as you as a beginner, you just need to know that they are an object and it's just an array, but you could do more things with them. So to create a list here, I'm going to be in my controller script, just to be doing this, we need to import something at the top here. So right now we have these four here. We need to import a namespace called system.collections.generic. So this will give us access to lists because this is exactly where they come from. So now in order to make our list, we just got to type in the keyword list like here. And you can see that now this is not gray anymore, because let's come from the generic namespace inside collections, which is inside system. So now you see that it's laid out like this. So what goes inside here? Well, this is the type of the list that we're going to make just like we did for arrays. I'm going to do the same thing, but for big doubles, just for example, so we put our big double in here, you could put string and you heck, you can even put TMP text in here, you could put any form of object or variable type in here, but I'm going to be doing big doubles just for this example. So now we're going to put a variable name here and we're going to be doing something like this later on in the series. So I'm going to call this upgrade levels just for an example. So we have our list here. This is all we need to do to declare the variable, which is pretty easy. So now in the start method, what we can do here is do upgrade levels equals new. So we're using the new term here, just like we did for data here. So new list. And we're basically doing the exact same thing here. So now what is this doing right here? Well, this is creating a brand new big double list. So where do we initialize the size just like we did for arrays? Well, we actually put this in here. This is the capacity slot and you can leave it empty. This will be just an empty list. So I'm just going to make this a size two list. So right now it's just two. So now we're going to iterate through this entire list here. So I'm going to use a normal for loop. So we start at zero, we create a variable called I to temporary one inside this for loop here. And basically we want it to not past upgrade levels dot count. So instead of length, it's count. So semi colon, and then we're going to add I by one, not really sure what happened there. So now what I'm going to print here is I'm going to print out I plus space plus and then whatever is in here upgrade levels at index I so accessing and changing variables inside this list is exactly the same. So just be aware of that. So now when we run this, we should see zero and one and this is empty. So this should just be zero space zero one space zero. I'll show you in the console. Okay. So we actually ran into an issue here. So I added this line here, upgrade levels dot counts, and it's printing zero. Hmm. This is a bit strange. It's a bit awkward. It should be creating a size two list, which is completely empty. Well, that's not the case. It's just creating a completely empty list with no values inside of it, even though it's supposed to be two. So I am not completely sure why this happens yet. I've this is just a common issue. I guess I'm not sure if it's just something related to how lists are created, or if it's something related to building your game or comp or for when it compiles or something like that. But all I know is that this does not work. Okay. So we're actually have to create our own method to replace this. But before we do that, I want to show that we can simply just add two slots in here by ourself. We can add two values. So we can just do is do upgrade levels dot add, and then just put in a number here. So I'm just going to add 10 and then upgrade levels dot add. And then for example, 42, just for an example. So now this should be size two, and it should print 10 and 42. Cool. So now this is two, we have 0 10 142. Perfect. So now we know that we can add to the list, and we can access them, and we can even change them too. So I'm actually going to change this 42 here. So it index one, this is zero right here. This is one. And I'm going to change that to actually 41. So now when we print, there you go. Now says 41. So another thing about lists is that you can actually remove the last value here. So all you got to do is upgrade levels dot remove. So now we have a red thing right here, remove works like this. So we have an item here. So we basically want to remove a specific item. So here I'm actually going to add back this 42. And we're going to remove the 42 here. So this is the item here, we're going to look for 42, and we're going to remove it. So now what we should see here is just 10, because we removed this 42 after we created it. See, there you go. Okay, so we learned how to add and remove. Now, another thing we can do with this is that we can actually initialize this list with some values here. So let's get rid of this two capacity here, and add some curly braces in here. And we can actually delete these parentheses here. So let's add some values. So 400, 292, just for some random numbers here. Now this is the exact same thing as creating an array here. So now we should see five numbers, actually four because we removed one of them here. So we should see 400, 292, and then 10, sweet. So yeah, we see 400, 292 and 10. So let's actually go back a step. And let's fix this issue here, because this is going to be kind of crucial when it comes to creating new lists. And again, I have absolutely no clue on why this happens. It's just like it's stuck at a zero capacity or something like that. I have no idea why this occurs. But for now, in this video, we're just going to create a method, you guys can create this now if you want. But I'm going to recreate it in a future script in a future episode, because not everyone is probably going to be copying all this down. Probably not everyone's going to be watching the list video maybe because they already know what lists are. So again, we will add it back later on. So just a heads up. Okay, so now to create this method, it's going to be a public list T. So T is a generic here. So T basically just represents any object we throw in. So for example, big double T and B text data int string, example list can go on T basically represents all of those. So now we're going to do create list. You can call it whatever you want, but I'm just going to call it create list since we already know what it is. And now we need to throw in our capacity. So it's kind of like this here. We're doing that pretty much the same thing, except we're just renaming it to create list kind of. So now we're just going to create a variable called into capacity. So this will be our parameter. And in here, we are going to create a special kind of list. We're going to be creating an i innumerable, which is basically a special type of list. Basically, it's read only and it doesn't have add or remove support. So it's good for sorting or reading big chunks of data. Now the thing is that list actually implements i innumerable. So basically that means that lists come from i innumerable. So some of list functionalities come from i innumerable. So basically you can say that list is a modified i innumerable. I don't expect you guys to know i innumerables are that really isn't that important. And if you want to do any more research on the one line I'm about to write here, you can go ahead. I'm sure you can find some of this stuff on forums or you can find on the C sharp docs. Anyways, what we're going to do here is innumerable. Okay. And then we're going to do dot repeat. And then in here, we're going to use the key term default T. So basically in this repeat here, we are creating a default new object of T. So basically we're creating the default version of this T here, which would be big double. And the default of big double is just zero. Now we don't know that because T can be anything. It could be a text again. It could be an end. It could be a string. So like the default of a string is just an empty string. So now in here, we put capacity. So now this repeat, it basically does this every time and it just adds to an empty i innumerable. And now we're going to eventually convert this to list. Okay, cool. So now we have a list and it should be the same size as what we've given it. And we also need to return it. And this can be converted into an expression body. So it looks very clean like that. Cool. I know some of this stuff may not make sense. It's perfectly okay if you don't understand it. In all honesty, I'm not very clear with it as well. But all we need to know is that this works. This solves our issue here. So now anyways, we can simply just replace this with create list in here, we can get rid of this new as well because we're not creating anything new. We're just simply calling a method that returns a list. So we do create list type big double and a capacity of two. So now if we get rid of these here, we should now see 0010. It should just be a size two list. And it works. Okay, so now we have finally created our list. They work. They're beautiful. And we're all good to go. When do we actually use these? Why don't we use arrays? Well, the thing is that for our data, we're going to be storing our levels, our just anything where we have multiple of something. Again, levels is a great example for this, or maybe you have 10 XP bars and you want to store every single one of those XP values or each of their levels and stuff like that in different lists. So eventually we're going to create methods that will be able to expand these lists safely and most efficiently. And now this is very important because when we have our save data, we can't just simply modify this number here in our data constructor. Let me pull that up. We can't just modify in here because the objects already created when we load our data, it knows that whatever list we have here again, let me just copy and paste this into our data for a good example here. So for example, let's just say we have our upgrade level, it is a size two list. Okay, so now we have our save and load system going. Okay, one update later. Okay, you know what, I decide I just want to add another upgrade. Let's just change this to three. It doesn't work that way, because when we load our data, and I'm going to be showing you guys this in the future, I know I'm getting a little ahead of myself here, but I just want to know you guys, this is really important. But when we load our data, it's going to load upgrade levels as a size two list. It is not going to check for this. So initially it's going to be a size three list. But once we load our data in, upgrade levels already exist, just as a size two list, not three. So then this causes some problems. So what we're just going to have to do in a future episode is check to see if our important list is less than a given size we want it to be. If it is, then we're just simply just going to add onto it like we did earlier here, we just added to our upgrade levels here. It's just going to add an empty value. So now this is all really cool stuff. And I promise we will get to this and I'm going to clarify more of this once we get there. So anyways, guys, I know this was a really complex episode. I really am sorry if you guys were a little stuck here. If you have any questions, please throw them in the comments below. I'll be sure to answer them as soon as I can. If I need to make another video on this topic, clarifying some things, I will be sure to do that. Otherwise, if you learned something new, and if you enjoyed this video, make sure you smash the like button as it supports my channel, and it makes my videos show up to more people. Subscribe to my channel if you enjoyed this content. And if you're new around here, and turn on the bell for future notifications of videos and live streams. Want to support crypto grounds? Well, click that join button below or check out the Patreon in the link in the description below. If you want to come talk to me in the community, make sure you check out the discord. It's also in the description below. Anyways, I hope you guys all have a fabulous day or night. I will catch you guys in the next one, which will be episode 2.5. And yes, finally, we're going to be messing with lists and arrays by adding more upgrades. Thank you guys very much for being here today. Thank you guys for watching this video. I'll catch you guys in the next one. Peace.