Save And Load Current Level/Map/World - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the remote engine 4 tutorial in today's video i'm going to show you how to save and load your current level that you're playing on so we're going to be adding a save and load feature and this is going to be able to load the level that you're on so if you have a lot of levels like if you're playing mario or something you have a lot of levels in there you want to save which one the player was last on so they can then load that when they next play the game i'm going to show you how to do that today so i'll give you a quick example here so you can see i'm in this level if i hit 1 which is my save button that's then saved within this level if i then press 9 which is just my quick way of getting to another level just to test it out you can see we're in here if i press two which is load we're gonna go back here if i then go back to this level press one so save again go back to my level here if i press two we're gonna load back to this level so you can see we're going to save which level we are on and then load that as well so i'll get rid of this code and i'll show you how to do this now so the first thing we need to do is we need to actually create our save game object so to do that we're going to simply just right click go to blueprint class and then under all classes here you can open this drop down menu and i'm just going to search for save game we're going to get this save game object here hit select so we can get it in i'm going to name this one game save one you can name this absolutely whatever you like just make sure it is not game save so don't do it as that put something at the end or give it something different otherwise that will then confuse you later on as there is something else we need with that name so once you've done that you're gonna open it up straight away and in here this is where you then basically save everything that you want so if you want to keep track of the amount of jumps amount of distance traveled items they have anything like that you put it in this save game but today all i'm gonna do is get their level name so i'm gonna hit the plus variable here call this one level name and then i'm gonna change this variable from a boolean to a string so it's a string of letters and numbers so we can get the name of the level compile save and that is all we need to do in there so you can close it straight away then what we're going to do is we want to get the place where you're going to be able to save so if you want this to be on a button press in a menu you can do this the exact same way just do it on the unclicked event instead of this so open that widget if you want but for me i'm going to do my character blueprint so it's third person bp blueprints that person character and once i'm in here what i'm going to do is i'm just going to find some space so i'm going to go down here and then i'm going to right click i'm just going to get the one keyboard event because that is how i want to save it again you can set this to be whatever you like or you can do in a widget so when you press a button on screen but i'm going to do it this way you do the exact same code you just go off the unclicked event from the button instead but i will show you later on how to load it from the main menu once you've got that we're going to come out with the pressed event here i'm going to get a does save game exist this slot name here we can set to anything we want but we're going to get multiple references to this so to make our lives easier what i'm going to do is just right click on this and promote variable and call this slot name and then we can just put that there and then if we compile this we can change its default value here and the reason why we're doing this is because then if we have multiple references to this they need to be the exact same name each time so if we just change this variable here it's going to change all of them to be the exact same so it just makes it a lot easier for us so then in here you can again name this whatever you like just make sure it's not the same name as this save game we made earlier so i called my game save one just give it a different name so i'm going to call this one save game one now you might want to give these better names for you so if this is just a level call it level game save or save level something like that but this is gonna work for me i'm gonna compile and save that out of this you can see the return value is red as is a boolean so we're going to hold down b left click to get a branch plug the condition of that return value in there the execution in there like so so basically if the game save does exist or if the game save doesn't exist so if it does exist what i'm going to do is we want to load game from slot so come out true and load game from slot again here this is where we got this slot name so just plug that variable in there like so then if it doesn't exist so for false what to do is we want to create save game object so create save game object like that the save game class is going to be our game save that we made so i called mine game save one and that is this here so just put that in the class there and then what we're going to do is we want to create a safer subclass so i'm going to do is i'm going to compile i'm going to right click on this return value here promote to variable and i'm just going to call this saver subclass like so and that you can see is the save game object and this is why we gave the other one a different name from save game because if you were just making this you need to get this save game object reference obviously if we called that one save game then there'd be two and you wouldn't know which one's which so we can do that and then what i'm going to do is do the same for the create save game object but i'm just going to drag and drop the one we made earlier on to there you could just set plug that in there like so we're now setting the saver subclass on both of these like that so now with this we're going to do is we're going to come out the return value of that set and we're going to cast to our save game so one was cast to game save one so again that will be whatever you named it is basically this that we made earlier go back here and i'm just going to duplicate that as i want to do it off of both of these and we're doing the same code but we can't join them because these return values only go into one thing so obviously we want to have multiple as they want to have their own independent return value lines like so out of this what i'm gonna do is as game save one i'm going to set level name like so i'm not gonna plug that in anywhere just get it like that again duplicate it to plug it down here so set level name like that but how are we actually going to be able to get the level name that we're on there's a very simple note for this which will help us out a lot we'll just come on the executable there and we're going to get current level name so it's literally just going to do it for us it's going to get the name of the level that we're in plug the return view in there in the execution in there again do the same thing down here so it was going to get the current level name and then set it to be our string for level name in our save game meaning we can then save this later meaning we can then save this and load it later on so after this what we've done is we've actually set this string but we need to be able to save it as well so to be able to actually save this string in there all we're going to do is very simply just come out of this we're going to save game to slot this one we can plug both the executables in there like so slot name is obviously our slot name string we made earlier save game object is our save a subclass there like so so now what we're doing is we're going to be getting the current level name setting it to be our level name string made in our save game object and then we're going to save that game to slot so now what we've done is we've created it so we can actually save the current level that we are on so we've saved it now we need to load it and so this is also very simple so what i'm going to do is i'm just going to select this comment it and call save game or save current level and then since i did the software one i'm going to load on two so right click down here just underneath it and get the two keyboard events like so out of the press of this i'm again going to get a does save game exist but the slot name is the exact same slot name up here like so again get a branch to plug into the return value there so if the save game does exist if it does exist what i want to do is load game from slot with that slot name in there again like so return value is again going to be just setting our save a subclass like that so it's very similar to what we did here but we're just loading it instead of saving it which again is similar to this but this is loading to then save the new one this is loading to load the level off of false so if the save game doesn't exist we just want to open up our first level so we're going to open level like so level name is just going to be your first level that you want the player to start on so for me that's just going to be the third person example map again making sure that everything in these is spelt correctly then after the code up here for set saver subclass what i'm gonna do is again cast to our save game so game save one for me as game save one i want to get the level name this time not set but get level name and then i'm going to come out of the cast for the executable there i'm just going to open level open level like that and if we just plug in the level name into there so level name goes into level name it's going to convert the string to a name value and then that will work perfectly it's going to open up that correct level for us so we compile save that's going to work so now if i'm going to comment this as well so select press c load current level that makes sense for me compile save and this should work and then over here as well i've just got the code for opening the two different levels i have just to test this so if i minimize this hit play you can see if i press two it's gonna load this level as what it's doing is it's going to be getting this value here so if a site name doesn't exist it's going to open this default level so if i travel to my other level just to show you so level 1 save all this and then if i press 2 again to load it should load up this one as that's gonna be my default level for it to load so you see that worked perfectly however if i was in level one here and i press one to save it press two it's gonna take us there as that's what it's loading so if i go back over to the first level here so if i pass an example map so like so and then if i go in here press two to load again it's gonna take me over here so it's transported me to the last level that i saved at so i try that again so if i'm now in this level i hit one to save and then i go back over here so sorry then i press nine to go here if i press two i'm gonna load back here so we are saving the current level we're on and then loading it as well and this works perfectly so now the final thing i'll do is i'll show you how to load this off the main menu so i made a main menu in a previous tutorial which you can watch now be on the screen now and i'll show you how to quickly load this here as well so i'm going to do is i'm just going to open up that widget that i made so main menu main menu widget go over to the graph here and find the on clicked start button event that we have here all i'm going to do is move the open level out and so this bit here is as well just going to be creating a loading screen which i also made in a previous tutorial but you don't need that if you don't want that won't stop this from working so a very quick way of doing this is go back to our character here what i'm gonna do is i'm just gonna copy this load current level so you get it does save game exist and load to load a level control c on that control v in here to paste it and just plug this in like so move the open level over here actually delete that one as we have this down here then what i need to do is we see we have this string here but it's grayed out that's because we don't have it in this widget so hit the plus variable call this slot name like so and make this a string change variable type as it's done there close it compile and this should work like so we need to actually put the slot name back in here again so that was just double check down here save game one so we put in save game one then we also need to make the save a subclass as well so we hit the plus variable saver subclass make this one a save game object reference like that change variable type close compile and now this should work perfectly for us so if we open up our main menu widget as well actually what i'm going to do is i'm just going to open up this one save this so now we should open up in level one as that's what i've just saved and if we open up that map so the main menu so now we should load up into the main menu like so if i hit start we get the loading screen and then we open up into level one like so as that is where we last saved now if i go back in here sorry so i do this go to the third place example map press one to save again go back to the main menu press start we're gonna load up and open up into this level here and that is where i last saved so that works perfectly so i think i'll be this video because we've done everything we wanted to do we set it up so that we can save and load between different levels like so if i save here over here press two we can load this i'll go back over here save it and go back to this level and load we're gonna go back to this level here so we can save and load between different levels and this will work perfectly for you as well and we've also set it up so we can load it from the main menu like so and you can also save this from like a pause menu or anything like that very simply as well you do the exact same code that we did just obviously in a widget like we did here so you go off of the unclicked button and do this code here so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one [Music] bye
Info
Channel: Matt Aspland
Views: 26,199
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, 3d modelling, blender, games design, ue5, unreal engine 5, save, and, load, quick, easy, level, map, world, current, like, mario, save and load, slot, game, ue4 save game, ue4 save and load, ue4 save, ue4 load, save game, exist, does, quit, leave, main menu, menu, pause, playing on, were, last, recent, most, manually, the, multiple, between, levels, worlds, maps, save and load level, blueprints
Id: TM7LcO78Cbo
Channel Id: undefined
Length: 12min 4sec (724 seconds)
Published: Fri Dec 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.