SOFT Object References in Unreal Engine EXPLAINED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's talk a little bit about object references and memory management because it's important to do that if you have a decently sized game I'm going to take you through my own player character here and um before we start I want to make one thing clear uh do as I say not as I do because as everybody else here I know how easy it is to just throw in some hard references and call of a day and that's a very bad pattern to do so let's take a look look here at a couple of things first and foremost we can check out the size map of Any Given actor and that will tell us how large the total memory footprint of a specific class is so that is the size of like all the components and the code in that class as well as everything that it directly references because everything referenced by this class is also getting loaded into memory anytime this class is loaded into memory so going into the size map we can see um how it's broken down and where most of the data is coming from and it is important that for this use case uh in the size map you change this from dis size which is the size on disk to memory size instead because this is everything that is getting loaded into memory related to this ector and in this case that's about 83 megabytes so not that much at all the other thing that we want to take a look at is going to be our reference viewer that's going to be a graph which this shows you every single class in your game every single asset that references directly this in this case the player character class itself on the left side and then on the right side we have the same thing but everything that is referenced by the player and this is the kicker because for the player it doesn't really matter right the player is going to be loaded into memory pretty much always so you can get away with a little bit more but if this is not very optimized for a certain type of enemy or maybe a certain type of decoration or whatever that has very bad references that can become a bit more of a problem because right now I have at any time the blueprint for my sword or my Scythe or whatever is loaded in it indirectly loads in the player as well but the sword is never going to show up in the game without the character also being there so that is fine but I also in a lot of gameplay abilities for the enemies I'm casting to the player character but it's more so the right hand side here that I want to talk about today which is everything that is loaded in the moment the player is active because some of the things like the animation blueprint of course that's always going to need to be present whenever the player is present but I also have a couple of things here if I go all the way down I've got this specific gameplay ability here uh that is a Teleport P attack and that is always going to be loaded by uh the player eventually I'm going to move this to being always loaded By The Sword so this will only be loaded when the sword is active uh but this gameplay ability that also loads in uh a couple of animations and those animations don't need to be in memory until I actually am ready to play them the game is going to have about 60 to 70 animations for the player and those don't all need to always be available in memory especially since this teleport attack can only be done when the sword is the active weapon when we go to a different active weapon you can't even do this ability so there's no need for the animations that are specific to the sword to be stored in memory uh when I can't even use them to begin with and this is where we get to the soft object pointer thing I needed to explain the issues with hard object references before we can go into how to use the soft object reference es because I'm going to replace these right now for you on video with soft references instead that way I personally choose when I load them in and out of memory which is vastly preferable for most cases so let's look up that gameplay ability and open it up so what this does is it does a lot of stuff right uh we have these montages to play and you can see there's quite a few of them and they are a blue pin and this type of blue means a heart reference but if I update this to being a variable instead uh we can say Montage to play one we can set this variable instead of being a animation Montage object reference we can set it to a soft object reference instead uh and we can change that variable type that is entirely fine and now if we reconnect this it will automatically uh change the pin from a soft one to a normal ref reference but it's not going to always keep this asset loaded if we try to run this now this is going to either not work or in the worst case scenario it's going to just straight up crash the game because this is a reference to something that's not loaded into memory yet we need to manually load it into memory so let's go back to uh event uh activate ability which is just like the G play of a gameplay event and then after we have decided yes we're going to commit this ability so we're going to actually do this ability that is when we're going to load these things into memory and we'll need to make a little bit more room for this in a moment you can make this into a separate custom event with like a delegate and event dispatcher call if you want to I'm not going to do that for right now uh let's get the soft object reference here and if we drag off that and we just type in load we can load asset blocking this is going to load in the asset on the main game thread meaning that it's going to pause everything rendering input registration whatever until this is loaded and and then it's going to move on which is less than ideal if it's just a small asset like this one you probably can get away with it and in some cases you need to do this because you need to make sure that when you move forward you have that reference uh working now but most of the time uh what you actually want to do is you want to load this async asset this is going to load this in the background the rest of the game is going to keep playing and this is going to load and you can keep executing other code while this runs and then once it is completed we uh run whatever code we put into the completed pin so in this case we'll commit the ability and then we'll async load this asset and then once completed is the rest of the code that way whenever we get to this over here we can be assured that the asset is loaded into memory because we manually told that hey we need this asset in a bit start loading it in and it doesn't execute any of the other code until it actually loaded in without stuttering the game and then if we go back here and refresh this reference viewer uh we're going to reset the camera view as well so that's going to be uh a bit of a thing but you can see one of these lines is now become a pink line instead of one of these uh white lines and this means it is a soft object reference so we still have a visualization as to okay this ability has a reference to uh whatever this animation is and whatever is linked to this animation as well but we can see it is a soft reference now so this doesn't automatically load in anytime the player character is active at all it only becomes loaded into memory when we tell it to now in Blueprint at least I don't think uh there is directly a way to clear up the memory that you allocated when loading in this asset uh whenever this specific object in this case is gameplay ability uh is removed so when the ability ends that memory will be freed up anyway so you don't need to worry about that if you want to have exact control over when you unload that memory again if you really want to get that granula you will have to uh do that in C++ I'm afraid and we're not going to talk about that right here today just as easily as that though I can promote uh this other animation to a variable as well Montage to play too I can say hey we're going to change this uh to a soft object reference and you can do the same thing with class references right most of the time you don't really use class references too much at least I don't but if you have a class reference that more or less works the exact same way whenever you have a hard reference it just loads in the entire class whenever you have a reference to it you can have a soft class reference that does the exact same thing uh more or less so what I'm going to do here is this is not exactly optimal in any way shape or form but I'm going to async load this asset and then I'm going to async load this next asset only once this is completed and move it on in a chain like that that might make the loading time for this ability a little bit longer because it's first loading in all of the assets and then moving on this is hardly ideal because if we stack like a lot of assets like this in the beginning of my ability there's going to be a noticeable delay between me trying to execute the ability and the ability actually execute in this case we're only doing this for three animations in total that's the third one that I'll do in a moment so it's not going to be that big a deal but do be aware that async load asset does actually take time so whatever may be the case there could be better places in your blueprint to implement it compared to what I'm doing right here and let's load in the third asset in much the same way again we're going to wait until the last one completes before we start loading in this one what we can also do is not wait for this one to complete and just start loading in the next one immediately and just kind of trust that by the time the third one which is the last to start loading in is loaded that the previous two are already loaded in as well usually you'll probably be fine doing it this way this might save you a split second I like being safe about things and just doing it this way instead one at a time and back in the reference viewer if we update that now again we'll be able to see that we have to scroll quite a bit down but we now have three pink lines because we have three animations that only get loaded in when they become relevant and if I go in here my special ability will still work fantastic there's nothing wrong with this this still works and again that special ability is done uh that space in memory is just immediately getting freed up anyway so in this case I saved myself a little bit of memory by not loading in animations animations are really light on memory user so it's not the most important place to be doing this uh things like loading in static meshes or skeleton meshes that kind of thing with materials attached to them which have textures which that thing adds up real quick uh those kind of things it's much more important to do this kind of thing with and if you're referencing entirely separate actors that you're doing things on as well do make sure that if you can get away with it you use soft references instead of of hard references because it's going to save you quite a lot of memory usage making your game perform just that much better specifically on Lower End Hardware and a very big thank you to all of my patreons you can see them on screen right now if you want to help out supporting the channel there's a link Down Below in the description to the patreon page and a special thanks to my cave Digger tier patreons Serge Thomas
Info
Channel: The Game Dev Cave
Views: 3,620
Rating: undefined out of 5
Keywords: unreal soft object references, unreal soft reference, soft reference, soft object reference, soft object, unreal tutorial, the gamedev cave, soft object tutorial
Id: BazkY5aqoig
Channel Id: undefined
Length: 11min 58sec (718 seconds)
Published: Wed Feb 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.