How to...Use Arrays in Unreal Engine 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to how to use arrays so I wanted to do a generalized concept video about arrays so you understand what they are and when you should be using them so an array is created by creating a new variable and choosing it from the drop down next to your variable type so your single array set and map array here is flexible as a shortcut now you can also down here in the bottom left you can right click on the variable type and it'll convert it back and from um to and from sorry the single and array type so what is an array an array is a list essentially it's an ordered list ordered is a key part of this with an array when I add elements to my array I can type in cheese for example I can type in ham oh I still hang on and do lettuce but array can also have duplicates so I can actually have another one in there called ham and it'd be totally okay with that this is very different from a set or map which can't have duplicates they have to have unique ones so an array you can have duplicates as many as you like and the array is very clever because you can keep adding to it and you can increase the size of it now in memory terms the computer science terms an array is kind of stored together so um when we resize it that's important to note in that we can have empty array elements so for example ham here could be just deleted left blank and what that'll mean is that our array length is still four we've still got four elements just one of them is blank so there are lots of different things you can manage with an array if I just drag it out and from here and if you go down to the utilities options and array you can see the different options you have now I won't go through every single one of them but I'll give you a few key ones you should know about so the first one you've got add and add unique these two nodes look similar but operate very uniquely in that ad will allow duplicates into the array whereas adrenic will first of all check to see if there's a duplicate and if there is it won't add it it will only make sure there's only one unique entry so this is very useful when you're doing references to objects uh because you may not want to have duplicate references um so yeah be aware of those two differences and the output you get for here is the index that they're in in the array as well you've got append array so you can combine two arrays together you can clear the array so you can empty it completely you do contains items so you can check whether or not it contains a certain item this is useful for things like strings and you want to see if it contains the array in a certain string you can output true or false beautiful flashes you're making and we've got find item this finds the first instance of the item so this will just go through in order and output the first one again quite useful especially with the duplicates you can keep going so if I'm putting ham in here for example and I search for ham this would output the value of one because ham is first found in the index one so it goes in order other key ones we have got a for each Loop which you're probably familiar with this allows you to go through each element in the array and doing some code on it using it for various things very useful when you've got a varieties of all sorts of types and sizes and you've also got get a copy and get a reference so this one usually confuses people about what the difference is between these two and you can tell what a difference is by looking at with looking at the pin at the end the pin here is a circle where it just pins a diamond so get a copy we'll get you a um a copy of that element of that array so when this outputs this here this is going to be a separate different string than the one that's in the array it's just a copy of it it's just you can do it so if I was to do something to this and say like set um no do um append like do this for example if I do append um blue for example that return value is different from what's inside the array it's completely separate yeah so getting a copy is useful if you want to do non-destructive stuff so you like doing a pens and things like that and you may be doing for calculations and it's probably the most common one you're using uh get a reference though this is going to be a string that is directly referencing one in here so if I was to change something in this I can it directly influence what I'm doing with it so this is not a big example of strings but if we were saying this is a reference to an actor I can then set a value on that actor without sending it back to the array and it would update the array so that's where that comes in useful is mostly when you want to change active properties that are inside of an array and there's I say there's a bunch of them we're not gonna go for every single one of them but other useful ones are random array item so if you want to pick one at random can do and we've also got a shuffle so we're going to randomize them up you can do um but the other one I want to show you is set array elements Now set a right element and you pass through an index and you pass through the item this will directly change the in the item in there so if I would have put in index 0 and item ham that would change the first index here index 0 to have now if I were to put in no index 6 and ham this won't work because there's no sixth index okay but if I were to take size to fit this will expand the array up until I got the sixth index and then that one we set ham here important to note though is that four and five will still be generated it'll be just that they're left blank okay so bear that in mind when you do tick size to fit it will expand open but leave everything blank that doesn't currently have a value so when should you use an array an array is useful when you want to do a list that is ordered so when you care about the order or something you want to use an array so for example if you're doing an inventory system if you want the character to have an inventory slot where they can manage it and decide which item's going what's lots and and what order they should go in an array is best because we can order it and make sure it's consistently the same order every single time they load it up whereas if we don't care about the order we just want to display it as is um we can use something like a map for example instead because we don't care about the order the player can't change the order we can't sort them um so we don't prefer to use a map Maybe also we want to use an array if we are expecting duplicates so for example if your inventory system is going to allow uh slots that can have different size Stacks so let's say we've got an example of like Minecraft where you have a stack of apples in one slot and another stack of apples in another slot that would make sense to be an array because you've got two slots that are set to Apples yeah there's two separate things regardless of whether or not you can manage it and move it around because they are too the same we can't use sets or Maps we have to use an array and lastly one key thing about an array is if you don't remove index say like I say moves in zero that would get rid of index zero okay but much like over here if I were to delete index 0 everything else gets pushed up so now index 0 becomes ham it's not the same as it was before and that's important to notice that when you remove items from here it all gets squashed together you can't have like an index that's no you can't have nothing in uh you can empty ones but you can't have you can't just remove it and expect like a null value or anything like that in there so it will squash it all down and move it along so it's great for doing that queue systems where you want um first in first out type of cue it's very good for that sort of thing um and likewise also good for first uh first in last out of two if you want to do it the other way around remove the last index every single time you can totally do that too uh by subscribing from the array and getting the last index from there and that will remove the last one I entered it so if you're doing like a stack where you get first in last out they this will handle that otherwise you can just leave it at zero and it always removes the first item in the queue so again very useful for doing situations like that when you want to do a q system so when you're sending instructions to something you could send it as an array and it would execute those instructions in that particular order but there you go there's a brief overview of arrays and how they work I hope you found this useful if you didn't know too much about arrays and you know when to use them if you like is there if there are any other Concepts sorry that you want to talk about please let us know in the comments below and we'll do another how-to video about various different concepts thanks for watching hope you enjoyed this video you want to support the channel hello the patreon.com forward slash Ryan daily we can find all my videos earlier before everyone else thank you for watching make sure you subscribed and I'll see you next time bye everyone [Music] foreign
Info
Channel: Ryan Laley
Views: 4,601
Rating: undefined out of 5
Keywords:
Id: XBMYmYeXRZg
Channel Id: undefined
Length: 10min 19sec (619 seconds)
Published: Thu Nov 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.