How to ACTUALLY Load Levels in Unreal and Make Loading Screens

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you're probably used to loading in levels a little something like this you have a box which this redirects to a separate level by the way that opens up a level using the open level node so if we open up These Blueprints here we can see we have AC begin overlap we check whether or not that's a third person character and then we open level by object reference this is probably nothing new this is how a lot of people usually open up new levels in that games and by and large that works but there's a little bit of an issue because this opens up a new persistence level meaning that every actor in there is going to be a respawn a a new instance of that actor so your player character your player controller even your game mode is going to be the one spawned into that map so if you have any data on any of those objects that you want to keep track of you need to put them into your game instance or something like that and you need to pull them back out and begin play and it it's just a little bit bothersome to deal with so minimizing that would be very much a good thing for a lot of games and that's where we get into using level streaming to set up this kind of thing because if we load in a streaming level instead of loading in a new persistent level we can then just put our existing player character into a new location and then we can keep our maybe our HP stats our levels if we Mak an RPG our Mana our whatever stats you have on your character you don't need to reload them in and be all bothersome about it some other amazing things is that we can actually have loading screens that take as long as it actually takes to load in these levels so we don't have to guesstimate how long the loading is going to take and on lower-end Hardware that's going to result in issues either that or on higher end Hardware it's going to uh stick around for way too long this this loading screen will actually pop up load and then disappear once the level is actually finished loading and as you'll be able to tell uh from the recording another upside here being the fact that the music track which is being played in level through an ector uh will be able to continue through the loading screen and into the next level which also is a little bit problematic if you're loading new persistent levels I'm going to assume that you have a basic understanding of what levels streaming is and kind of how it works if you don't I have a separate video specifically about level streaming which I'll try to remember to link in the card up top so what we have here is a persistent level and two streaming level so if I turn off streaming level one you can see that's the one over there and streaming level two is the one over there and I've got this second different load new level actor and if we open that up I don't have any code in it yet but I do have a box which is just going to use uh the overlap so if we overlap with this we're going to do the exact same thing that we did before and that is we're going to start by casting to the BP third person character just to make sure that whatever overlaps with this is actually a player character and then we'll use async loading to load in the new level unload the current level and change the position of the player character so first and foremost if we do overlap with the player character what we'll do is we'll load stream level by object reference you can also do it by name which is a little bit more flexible in some ways but I like doing it with the object references because it gives you this nice little drop- down menu I also already made two variables here one for the level to load so let's actually call that level to load and one for the current level because we also o need a reference to the level that we're going to unload so we'll use the level to load for the load streaming level and then you will notice that this is a async node uh due to the little clock icon and what that means is it's going to run on a separate thread for the game so whereas the normal open level here is not an async node so it will just pause everything going on in the game until that level is loaded and then move on so your game will more or less just freeze this will run asynchronously so you can keep the rest of the game running and things like your music track and stuff like that they won't uh be bothered by this at all and this is also the way you might want to set up a loading screen there's a lot of ways to make this more complex and be able to get more information out of these and stuff like that today we'll just going to focus on the basics here make visible after load should be set to true because of course after we load everything in we want it to be visible so we can interact with it you can set this to false and uh just enable this to be set to True later that way everything is already loaded into memory and only needs to be turned on at certain points could be useful we're not going to be using that today so after the streaming level loads in what we want to do is we want to move our third person character to the location of where it's going to spawn in in that new level and for that going to uh real quick go back into the view Port here and we will add a capsule Collision you can just do this with any scene component really uh but we're going to be using a capsule Collision here because it's a good reference of what our actual player looks like because that also uses a capsule Collision coming down here we want to actually set this one to use no Collision this is just for a reference position and then we'll see that in the viewport here we have this capsule Collision so we can set this capsule to uh all the way over in level two and then we will spawn in wherever this capsule location ends up being so and then for the level two one we select the capsule and we can move the capsule over here to level one now how do we use this capsule for setting our new location that is quite easy going back into our event graph we'll drag in our capsule here and we can simply get the world location and we can get our third person character reference here and we can say set actor location could even also set the rotation with this we're not going to do that right now but you very much good and you just set that to wherever in the world the capsule location is if you want to keep all of your physics and stuff like that you want to set this to teleport through we're not going to do that for now so we load the new level and only when that new level is loaded will we set the actor location and then we we will unload the current level so unload stream level by object reference and we'll unload our current level and this is the basic setup of what we're going to be doing so we can uh go back into our level streaming map here and with this it is as easy as now putting in whatever level we want to load in in this variable over here and our current level which we're going to unload into this other variable over here and there's only one little issue left to cover that being these streaming levels are set to be streaming method blueprint because you don't want them always loaded you want to be able to load and unload them at will but that means that when we start the game uh nothing will be loaded in because blueprint streaming level method means that by default everything is going to be unloaded so we need to specifically load in level one usually you would in your persistent level then have something like a main menu setup which would have a new game which would then start by loading in level one or maybe a load game function and then on your save game object you would have a reference to whatever level you want to load in when you load your save data we're not going to bother with that right now today but that would be the basic setup for now what we're going to do is we'll come up here and go into our blueprint for the persistent level and we will just simply uh load stream level by object reference make it visible after loading and we'll just load in level one and now level one will be loaded in when we start the game so if we walk over here now we can see we get teleported over to level two and if we walk into this again we get teleported back over to level one and it loads in and unloads the levels when they are or aren't relevant in this way which is really really nice now this also allows for very easy loading screens that stay up as long as these levels are loading of course these levels are very very simple and they don't take too long to load but for more complex levels it could take a while to load in and with async loading your game doesn't freeze up so it could be the case that you overlap with your loading zone and then it takes like 2 seconds in which you can still run around and then suddenly you're on a new level which is kind of jarring but this is exactly why async loading is so good because unlike with normal level loading where the game just freezes up for a few seconds while it loads to Next Level we can actually make a loading screen that appears and disappears based on the loading time so if you just go in here uh into the content and really really quickly make a user interface uh widget blueprint and we'll call this loading screen we'll just add a very simple uh canvas panel here we'll add in an image into that and that image is going to be uh absolutely huge and it's going to be a black color just because that's a little easier on the eyes then we'll add in a little bit of a text which we can put here in the bottom left corner The Anchor Point will be the bottom left the text will say loading level maybe we can make this a little bit bigger this is all design stuff doesn't really matter for now uh but I like things to look halfway decent and then we can put in a throbber as well which what that will do is it will do one of those animations so that the player knows there's actually something happening and the game hasn't Frozen up and died another thing that the open level will have trouble with because the open level works on the main game threat and as such pretty much everything in the game freezes until that task is completed because it's not a synchronous task set this to uh the Anchor Point bottom left as well and now we have a very simple load loing screen so if we go back into our level to load uh blueprint here we can say before we start loading our new level we can uh create a widget and we need to move this all back a little bit more and the widget will be our new loading screen that we've made and we can then add that to the view Port then it will load the level then it will set our actual location then it will unload to level and once it has been unloaded we will remove remove it from parents again so we will remove from parents and now however long this takes on whatever Hardware you're playing the game on the loading screen will pop up and disappear again purely based on how long these things take to load in and out and obviously you probably also want to um disable the input here for your player because you don't want your player to accidentally be running around and and still be like jumping off and hurting elves so you want to get player controller for that and then at the very end we want to enable that input again so um this is a little bit messy I will upload this whole project to my patreon and YouTube members so if you want to look around these nodes uh when they are a little bit more neatly arranged which I will do after this recording uh there will be a link Down Below in the description to both of those so if you're a patreon or a YouTube member you will be able to download these project except that this will not work and that is because if you just logically think about what we're doing here is we're overlapping we're casting we're creating the widget disabling the input adding it to the viewport we are loading in the next level we're teleporting the character to that level then we unload and then we do the removing of the widget and the enabling of the inputs but you might be able to see where that logically doesn't make a lot of sense because the level we're unloading contains this object and if it contains this object and it is unloaded it will never be able to execute these other two nodes because these two will only be executed once this one is finished and at that point the object doesn't exist anymore so there's a bunch of ways you could go around uh fixing that through setting up an entire system through your level blueprint or whatever or making a separate like manager object for that uh for now I'm just going to move this unload level to the very end and that will uh fix that issue it might not be the most ideal way to do it in every situation because sometimes you might want to wait for the previous level to unload for whatever reason uh in this case we're just going to do it this way another way to fix this is to just always put these level loading zones into the persistent level and now we spawn in and you'll see it hardly even loads in the loading screen just because it loads in so so so fast so for now let's just put in a bunch of random separate meshes so that it has to load in uh more different Assets in this one location so now there's a bunch more assets and maybe that will help uh increase the loading time which usually you want to decrease loading time in this case it's it's still instance you could see it there for a half of a second real quick you could see a loading screen uh pop up the loading times for this specific map are so so low that it doesn't really make a lot of sense so a good way to uh make sure that the loading screen stays in for at least like a certain amount of time because you don't want to have uh this happen either is we're just going to add in a delay of uh 0.5 seconds for every loading screen just artificially increase that length if this takes like a second to load half a second more isn't going to bother most people and this way we get at least half a second uh in there again there's more complex ways to ensure that you don't have to add something but it does have a minimum length not getting into that right now this way the loading screen will be at least half a second long as we can see and now it works and the wonderful thing is as I mentioned in the beginning of the video is that we don't need to reload in any actors so our game mode is still the same instance so any data that we put into it will still be there and the same thing with our actor if we have a health value on our actor that will still be the same in our new level instead of having to do all sorts of weird stuff with loading it back in from the game instance or whatever or putting it in a data asset and reeling it back and that's all kind of weird workarounds uh for using a system that's for the most part not meant to be used the way a lot of people use it this is by and large the more preferred way to load in levels and and deal with things being persistent between levels 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: 6,171
Rating: undefined out of 5
Keywords: unreal levels treaming, unreal level loading, unreal engine, unreal, levels, level loading, level streaming, loading screen, unreal tutorial, unreal engine tutorial, level streaming tutorial, the gamedev cave, unreal engine 5, unreal engine 5.3
Id: K2A6un2Uf4M
Channel Id: undefined
Length: 16min 30sec (990 seconds)
Published: Wed Dec 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.