Unreal Engine 4 Tutorial - Saving Level States

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to this special episode this episode is special because it was voted for by my patreon members and youtube members so thank you first of all to everyone who voted and showed your support and voting for this episode so what are we doing today today we are looking at level persistence what that means is baby basically traveling from one level to the other while saving what state the level was in prior to us leaving it therefore we can go back to it and see it state so the purpose of this experiment and test today i've set up a simple experiment here with two levels and each level has five pickups that i can go around and pick up there's five there and if i go to level two they'll have a five there and i've got a set to a key bind there for now so level one i'm gonna pick up two items here and you can see i've got a hud interface element here showcasing the two points i've just collected now if i go to level two i can go up here and collect these two points up there and now i've got four points now if i go back to level one you'll see it saved which two i picked up leaving the last three that i have yet to pick up and similarly on level two it's remembered which two i've picked up and not the other three and i can go around and affect that by picking up more go back to this level and pick up more there and there you go so we're gonna look at two things in this so we're looking at level states and transferring them over when we save them and also looking at player data so there's points for example how do we transfer those data points over i've also set up how to set it so you can store where the player's location was um as you can see here if i'm going to this corner here transfer to level one it saved that position and moved us into position in that same position level one now you'd use that for like checkpoints or other things like that to remember where the player should last be so uh yeah pretty cool so what we'll do is gonna go through and show you how to set this up if you want these project files you can find them over on patreon gold members and hire can access all of our project files as part of their tier rewards so let's get started on how to get this set up so to get started we're gonna not go through like how to do pickups or level transitions anything like that because that's stuff that is basic and is found in all other tutorials all over the place so that's not the point of this video if you want to know more about those sort of things you can find plenty of information about that sort of stuff elsewhere on my channel but what ultimately you're going to want to do is apply what you're going to do in this video to whatever it is that you want to save and store across your levels so in this tutorial we're going to do something quite basic but what you want to do is probably a bit more elaborate it's exactly the same process it's just multiplied by how many things you want to save you'll see what i mean when we get started here and to get started we're going to look at the third person game mode in this case so the game mode is what's attached to each world and is loaded when the map is loaded up and is closed when the map is switched out or unloaded so on the game modes we're going to set up our save functions for our game here so i'm going to go to my game mode and in here we're going to create a new function called init level save data so initialize level save data and in here we're going to first of all get the level name so come out of here and get level name and that gets you well the level name it's pretty simple really and that comes out as a string now this is useful because what we're going to do is we're going to store this save data in the same name as our level name now what's the benefit of doing it like this well it makes it so we can just make this function once and it will work for every single level that this game mode is using so you don't have to keep making custom functions for each level so i'm going to get current level name and we're gonna do does save game exist and the slot name is the file name of your save file so drag that across like so and that gets you a boolean the boolean is gonna go into a branch because it's if we're going to return true or false if it's true we have got a save file so if you want to load it and if it's false we don't have a save file we'll have to create one so let's start off with if we have got a save file existing so on true here if that's the case we also want to get the current level name again because what i'm going to do is say to load the game from that slot so drag out from here and do load game from slot and you'll see you need a slot name hence why we needed to get current level name again once you get load game from slot if you hover a mouse over turn that you can see that this is a save game object reference not a particular object that we need because we need to make a custom save game object so let's create that so let's go into our content browser here go to add new blueprint class and in all classes box down the bottom search for save game object select that and click select and you want to name this like level save data or level save object so level data object for example and i'm gonna go into back into my third person game mode and on the load game from slot we're gonna drag the return value out here and we are going to cast to level data object the object we just made so once we've got that we then want to store this object reference as a variable so come out of here and promote that to variable and we'll call that level save object that's important because we need to be able to access our level save data to in order to save it to the slot if you want more information about how to save and load games please check out my saving and loading videos there's three of them i'll go over a few things there worth checking out if you want to get a handle on how so saving and loading works but the general gist of it is whatever variables are inside this level save object when you at the time of being saved they're the objects that's the data that's getting saved so now we got that loaded up we want to go down the default path so this is if we don't have a save game object so in the force here we're going to come down here and do create save game object choose the level data object type and then i'm going to drag my level save object variable out and choose set plugging that all in like so so now that is going to save and create our level save game object hit compile then go to the event graph on the event graph we can go begin play and we're going to tell it to initialize the level save data so drag that function out so we just made and there you go so now when the level was first loaded up a save file was being created and that saved file is basically empty now so we need to add data to it so let's go into our save game object in our safe game object as i said before this is where you store all the values that you want to save onto disk so on the variables here we're going to create a new variable and this is going to be the collected pickups so if you've got things like uh physics actors that you want to store the location of like the players throwing stuff around you want to save that location this would be just like movable actors you just get all actors that are movable and store them to this uh an array and that's really all you're doing so this will be collected pickups and this is going to be an array of actors so actor array hit compile so this is the main thing that's been saved okay it doesn't matter if it's anywhere else it has to be here so on this save data object we're also going to put in a function that has a loading of which actors need to be destroyed because they've been collected so on the functions we're going to a new function and we'll create this one as load collected pickups so these load collector pickups basically means that we're going to go through the collector pickups here and remove them from the level because when you load up a new level everything's loaded that is set inside that level what we need to then do is start destroying all the things that the player has either moved or destroyed in some kind of way or it may be you want to deactivate or destroy stuff based on what's in this array so for example if you've got a group of enemies that the player has killed you'd store that list of enemies in an array and you do the exact same functionality doing here but to destroy all the enemies from the map so let's get this load collected pickups working i'm going to drag out my array and first of all we need to check the length of it so get uh we'll just type in length and we're going to check if this is greater than zero it is great that means there's something in there we can continue with so next we're going to drag the collector pickups back out again and do a for each loop to pass through every item in our array with every item we're going to check if it's a valid reference so do is valid and you do the question mark one and if it is valid we're going to take it to destroy itself so drag the array element out here destroy actor like so and that is how you load up the data so this is going through all the collected items that we've got stored in our array and destroying them from the map so that's how we load it but how to actually save which ones are going to be in this array so when we compile this and let's look at the pickup so on the pickup we've got a very basic acting overlap cool to play a character and add points essentially what is and add points on the player character simple function which adds adds score to it and updates the hud you also see alongside here my begin play we've got to create hud add it to the viewport but more importantly i've got the game mode cast a third person game mode and stored as a reference this is important because we want to keep hold of a reference that is directly to the game mode without having to cast every single time so now we have that game mode reference we're gonna go into the pickup and we're gonna use that on here so as a third person character because we've used it here on the third person character we can actually access that game mode but drag out from here type in game mode and you'll see get game mode from there we can now get access to its saved data object so from here we can drag out and get level data or save object here and from that save object we're going to get the collected pickups we're then going to drag out from there and go add unique now add this object reference to this array so that's uh we'll come down here and do self adding it to that array after which then we'd have to destroy the actor like normal okay add points get game mode level save data click the pickups add unique and then destroy the actor finally so now that's being added to that array uh by default it's not going to do anything it's just going to be added to that array we now need to take it to save that array to the file so that's handled on the game mode so let's go back to the game mode and in here we're going to create a new function and this function is going to be save level data and in here we're going to drag our level save object out choose get and then from here we're going to get level name and then we're going to go save game to slot now the save game object is your level save object and the slot name is the return value of your get current level name hit compile when you're finished there so that's the function we now need to call it so where you save it's up to you like it could be when you hit a checkpoint at a certain time interval cutscene whatever it may be i'm going to save mine when i transfer out of the level so i've got that currently set up on my player character with a simple key press so one goes to level one two goes level two very simple so on here i'm going to do the game mode from the variable list and then from here i'm now going to call the save level data plug that in i'll do the same for level 2 here now there's loads of ways of doing this this is just one of a few so try to figure out how it's working here then you can sort of customize it to your own needs when you make your own games but more importantly here we push one it's going to save the level data i.e that array of collected items and then open the level correctly afterwards now we've saved it we now want then to now load it so let's go to the game mode and in here we're going to go to the event graph at the end of begin play we're going to drag the level save object out and call that load function we made basically clearing all the ones we've collected let's hit compile and test that out so walk up to these pick up a couple of there and push one here to transfer to back to level one and you can see those two are still gone take the middle one here and transfer around and you can see those two are now gone so that's not working we now need to do that for the points on the screen so at the moment our score is one and it goes back to zero every time we do this so at the moment we're saving level data and that's persistent across levels so that hang on be best if i show you that um so oh hang on by the way if you when you're testing this out you may want to find yourself resetting your save data easy way of doing that is going to your project files and deleting the save data so i'll show you where they are in your project folder look in the saved folder go to save games and just delete the data that's there if i push play again they're now back so as you can see here i pick up two here go to level two got these two go to level one and they're still gone okay so it's remembered what my state of the world was in so let's now get working with the points on the screen so what we can do here is we basically create a whole new save file um that's separate from the level save files so at the moment in our folder here you can see we've got level 1 level 2 being saved out we have another one here for player data so let's go into our game mode and add that in so similarly to init level save data we can do init player data now the easiest way to do this if we go to our function list for init level save data right click on that and duplicate it and we'll call it init player data player save data and then my only difference here is that we don't use get current level name so we'll remove that and just type in the name here of player data similarly here player data and there you go that just does that we also want to change this save game object so currently we're using it's got set as level data object we're going to create another one for the player character to go to add new blueprint class and search for again save game object and this will be called player data object go back to a game mode and we'll change the create save game object here to player save data update object sorry and we'll get that away and with this return value we'll promote that to a variable and call it player save object at the top we're going to change the cast and the set here so then from the return value we cast to player data object and then we set that to the player save object that we made just now like so hit compile go back to your event graph and just drag that in here we go init player save data like so so what we're going to do here is we're going to store the points the player has on this player save data so let's go into the player data object and make a new variable in here called points and that'll be a verbal type of integer compile and that's basically it then on our player character we want to in here when we add points to our game on the end here we're going to get the game mode out in the game mode going to get the player data no player save object there we go and we're going to get the points from it so set points and this is going to be set by the player um at a particular point so here we don't want to do it every time we're adding points to the score here instead what we're going to do we're going to add it to when we transfer level so let's move these along further drag that down and in fact we can just use this game mode that's pre-existing save as a node and drag this set points in like here and plug that in like here too now points is going to be equal to the points here and here and that target goes down there too so there's some basic stuff going on there okay we're just setting the players uh player saved data's objects point score to be the same as the point score on the player pretty basic now when we take to save that object it's going to save the points so let's do the saving on the uh third-person game mode here we're going to create another function here called save player data on save player data we're going to get the player save object out remember that's where we're setting our points and then from there we're going to save game to slot and we just type in name player data hit compile so that's saving that information what about loading it okay so now what we're going to do here is when we are creating and loading the data object up we're going to go into the third person character and after do we set the game mode and begin play we're going to get the player data object uh player save object i called it yep there you go and from there we get the points i'm going to set that to the points on the player and there you go so that's now loading the information up from the player save object the actual loading of the object happening on the game mode when we first start the game up with init player save game object with load game from slot so let's save this and and oh sorry then we have to to actually save the file um so on save player data here we have to call this on the third person character so when we move we're going to call this out from the game mode sorry and call save player data hit compile oh target there you go hit compile so now when we're adding points to it we're then saving the player file and then we've got a load file when the player character is first introduced there you go so let's see how this works testing it out we'll remove the previous save data we've got here and then played this game so points are set to nothing at the moment so click one up one two three and then if i go to it number two points is still showing zero and then i pick one up it says four so why is it saying zero at the start well when we're going into the third person character and we're loading up the points here we haven't actually told it to update the hud as well so let's grab this update hud function here and just drag that on to there like so so the number was correct it just wasn't updating the hud so let's play to test that again you'll see the save file stored how many we've got we've got those rest there and go to level two there they are pick up those ones go to player one two level two sorry that one level one level two and you get a gist so there we have it storing not just player state but also now level state so we can now use that to combine the two and you can do this for as many things as you like so you probably have a save file that is for settings alone one for player data make one for each level or you may do one object which contains all the level data you may have different arrays based on level data so for example my level data object could be just one object that is spawned and it's fixed in name and i may just have collected pick up so level one like to pick up level two click pick up level three and so on so forth or this could be structs it could be all sorts of things whatever it is you just save it to a separate level data object and load it up except from the rest of the objects in the game and that's basically it um and there you go so to test it out don't forget you just have to remove the save data you can see the different save files different slots here and you have quant may have a few in here but ultimately there you go just remove those and push play and that's all back again i'll take the middle ones like so it's level two one and you see it's still gone and that brings us to the end of this episode uh so there's lots you can do with level persistence and this is just the tip of the iceberg you can save a lot more data to your thing now keep in mind the more data you save the longer it will take to save and load this is where you use loading screens to hide these saves or hide these loads that way it doesn't get hitch ups or weird stuff happening as things are spawned in then suddenly removed in front of your eyes so make sure you try and build a load screen in if you're doing too many things at once but ultimately that's it you just amount of scale now you just add in diverse arrays or structures based on whatever information in the store maybe will store the exact position and location of various enemies that could be a map for example each one being a reference to the enemy actor and their index and then storing their transform it could be as many things you like like that so thank you very much for watching this episode and thank you to all my patrons and youtube members who voted for this episode if you like what i do and you want to support me head over to patreon.com for slash rightly and even if you become a five dollar tier uh you become able to vote on next month and each month after on what videos you'd like to see next so if you want to join that head over to website and show your support there thank you to everyone who has subscribed and if you haven't yet subscribed to the channel make sure you hit that subscribe button it really helps me out so make sure you do thank everyone for watching and i'll see you all next time bye everyone [Music]
Info
Channel: Ryan Laley
Views: 25,929
Rating: undefined out of 5
Keywords:
Id: avwVygn97N4
Channel Id: undefined
Length: 26min 38sec (1598 seconds)
Published: Mon Feb 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.