Data Structures For Game Devs: Arrays vs. Lists | Unity Tutorial (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome in this multi-part series we'll take a look at how various c-sharp data structures can make your unity gamedev life easier you'll learn how each data structure works what their pros and cons are how and when to use them and we'll go over lots of example use cases we'll also check out at high level how they work internally this guide is suitable for beginners but should also contain a bunch of interesting bits and pieces for intermediate game devs especially if you don't have a formal computer science education or if you're new to unity and c-sharp quick note you can use data structures to optimize your code's performance in terms of runtime and memory usage so we'll definitely cover a bit of that but the focus of this video series is on using them to make your game dev life easier so data structures what are they exactly a data structure is a collection of data values the relationships among them and the functions and operations that can be applied to the data alright that sounds kind of scientific and dry let's just say you can use them to store organize and access all kinds of data in various ways for example an area is a simple and common data structure that lets you store elements of a specific type in a specific order but there are more data structures than just areas and lists and each is specialized in doing specific things so they can be super useful programming tools making your code shorter simpler safer faster more concise and more readable i also claim you can utilize them effectively even as a programming beginner one goal of this video series is to give you a first intuition on how the covered data structures can be useful so you'll hopefully spot use cases in your own code more easily almost all the data structures in this video series are so called generic collections meaning that they can hold any data type like numbers strings game objects your own classes and so on which is why they are called generic however the contained elements must all be of the same type and you need to specify that type when declaring the data structure if you're really professional you'd say they are strongly typed usually in the api reference the data structure's type is simply represented as t for type and you would replace the t with the type you want to store technically the area works a bit differently since it's more of a native c sharp feature while the other collections live in the system collection's generic namespace so whenever you want to use them you need to add a using declaration at the top of your file that said there are also non-generic equivalents for these data structures but they're usually slower less save and require you to write more code so we're not covering those so let's start out with aries here's how you can define one that can hold exactly three integers the elements are always kept in a specific order and each has an index to access it of course you can store anything you like in them for example game objects 3d positions or any of your own classes you can also define and fill in the array in one step if you like anyway now you can easily access or modify individual elements in the array in this case we loop over all high scores in our array assuming each is for a different level and we print that high score and then reset it to zero as i mentioned internally the area is a bit different from the other collections we'll cover while all the other collections are directly implemented classes that you can declare like any other class arrays have an abstract base class that means we can't actually declare such a class directly like this but any area we define like we correctly do at the top derives from the class so we can use all its properties and methods in this example we use that class's length property it provides many other methods and properties and for accessing and modifying values you could alternatively use the get value and set value methods defined by the array base class anyway there is a significant drawback to areas as opposed to lists their capacity is fixed and you cannot easily extend them to add more elements once they're declared in fact to do so you would have to copy the entire array to a new array with a larger capacity to then add your new element the fact that aries can't be extended makes them a lot less flexible than lists which we will get to in a moment but also a bit more memory efficient in many cases because they never take up more memory than they need you can safely use an array if you don't need to add elements at any point during your game's run time which could for example be the case for an array that holds all your level names but on the other hand maybe you later decide to add a level editor to your game letting players create their own levels meaning you would need an extensible data structure you can always switch to a list in such cases or if you don't have a strong reason to use an array you can just use a list right away which i tend to prefer because i'm lazy anyway if performance is a big concern for your game and you need to store a really large but fixed number of elements then an area might still be the better choice so lists are very similar to arrays they're basically a more convenient and flexible version of them one key feature being their extensibility so you can easily add elements at any time in fact if we look at the internal implementation of the list data structure we find that it uses an array under the hood whenever the internal aries capacity is reached meaning it is full of data the list will create a new area that is twice as big as the previous one and copy over the elements from that old area by the way the factor by which the array capacity increases is called growth factor and it depends on the programming language and the respective implementation of the list data structure but most implementations use factor 2 like unity c-sharp implementation or factor 1.5 looking over the list implementation in general there is a lot of helpful code that can make your own code shorter and safer like for example checking bounds you pass into any of the methods and this is another reason you might want to prefer an area in some performance critical cases the list just does a lot more things however in most cases especially for small lists this really shouldn't be a concern anyway back to utilizing lists in your code here's how you define one again as a simple example we just keep a bunch of integer numbers in the list but we can store anything like game objects or colliders note also that we don't need to specify the size of the list like we had to do for arrays as we just saw in the implementation it just automatically adjusts so let's check out a real world example i am currently building a little game called on track in short you have to operate the railway switches in order to direct trains to depot of the same color and if a train reaches a wrong depot the player loses a life to keep track of the ui elements that represent the player's remaining lives i use a list in my ui panel that holds the hot sprites in order to update the heart's colors according to the player's currently remaining lives the ui panel provides an update live's function which is triggered by an event when the number of remaining lives is updated on track is still work in progress and i think the number of lives you have may vary by level so a list is more flexible and convenient than a fixed size array i'm also totally not concerned about performance for this tiny little list that i only occasionally update so it doesn't matter anyway note in this case i definitely need a data structure that keeps my elements ordered like arrays and lists will do so that i can remove hearts from the right as the player loses lives this is a key difference compared to for example hash sets and dictionaries which we will cover in the next videos and which is where things will get more specialized and hopefully more interesting if you are already familiar with aries and lists check this video's description for links to all videos in the series or the full playlist drop a comment below if i missed anything or if you have more questions thanks for watching
Info
Channel: Anni
Views: 3,633
Rating: undefined out of 5
Keywords: game development, unity tutorial, data structures, c# tutorial, game dev tutorial
Id: uWI3JEBRMiA
Channel Id: undefined
Length: 7min 51sec (471 seconds)
Published: Sat May 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.