Save And Load Player Stats - Unreal Engine Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another arrangement 4 tutorial in today's video i'm going to be doing another save and load system this one is going to be specifically for saving and loading your player stats because again this is another one which has been requested just simply advancing upon previous ones i've done however you don't need to follow those this can just be a standalone one which you watch so let me hit play and show you we're gonna make today so you can see i have my health stamina and ammo on screen here just as three different examples of what we can save and load and i'm also gonna be doing the stamina as well so at the moment i just have different buttons to be decreasing my health and stamina but you can obviously do this however you want so obviously getting hit or running around or anything like that and also one for my ammo as well so i'm just pressing two four and six to be decreasing these and when i press one it's going to save my game which you can obviously do in a different way as well so maybe on a save button when you go to the main menu or anything like that but for me it's just the one keyboard event and then when i exit and go back in it's going to load so you see it's loaded all the way up here where we saved our health stamina and ammo have also changed and what i can also do is increase these two and then go down here save again and go back in and you can see it's now updated with our position health stamina and ammo like so so this is what we mean today a nice simple save and load system so without further ado let me take this code and i'll show you how i've done it so the first thing we want to do is you want to actually create our save game and what a save game is is it's a blueprint which allows us to store details and install variables which we can then save and load off from as well so i'm going to be doing this in my save games folder which i have here in here i'm going to right click go to blueprint class and then open this drop down menu of all classes and in here search for save game like so i'm going to press the green select button and name this whatever you want however do not name it save game as that will confuse you later on as names will get confusing so i'm just going to name this stats save like so opening it up straight away again i said this is just a place where it could store variables so imagine it could kind of be like a notepad or a word document with the variable stored in it so we just want to create our variables which we want to store in here so i'm going to hit the plus variable here to create all the ones i want and again i want to have health which i'm going to create as a float i want stamina which i also want as a float i want ammo which i want as an integer and finally last but not least i also want location which is going to be a vector or in actual fact we can also do transform so then we have the rotation of the player as well as the location if you wanted i'm going to compile and save that now we don't need to set up any default values in here because again we're going to override these values to set them to be what they should be whenever we need to use them so we're going to set these and then load them as well so we can close that as that is all we need to do in there and this here is just the hud which i'm using then we're going to open up our character blueprint which me is going to be content third person bp blueprints third person character now in here you can see what i've already got is just simply on event begin play i'm creating the widget and adding it to the screen and this is also how i'm just increasing and decreasing my values just for testing and the purpose of the tutorial obviously for an actual game you want to have to set up differently so we're going to ignore that and what we're going to do is create two different functions one for saving and one for loading so let's start off with creating a function here for this save game so that's what i'll name it save game i'm gonna come straight out this function with a does save game exist because we want to know if we need to create a new save game or load it to then overwrite that save so the slot name we can drag into our save game function there so we can input a different slot name every time we want to save or load that way it just makes it a little bit more efficient then after this we're going to hold down b left click to get a branch so that was n so b to get a branch with the return value going into the condition there like so because again this is going to be true if the save game does exist or false if it doesn't so we'll start with true if it does if it does we want to load it so we're going to load game from slot like so and so the slot name wants to be this slot name here from the save game function so we can just drag it in there however you see we get this line go through the code and we can use rewrite nodes however what i'm going to do is drag out slot name and just search slot name and what that does is because this is inside a function we can use the inputs on the function in here like so so we can get a reference to them like that that's a nice little tip if you don't know that already so for example if i were to create a new input on here just called tester with it being a boolean you can see i have no other variable called tester but inside this function i can get tester like that but if i were to be outside of this function i won't be able to get tester because it just creates basically a local variable inside this function so that was just going over that now what we need to do is out of the return value of load game from slot we're going to right click promote a variable naming this save reference or save ref like so that's just so we can easily access this later on then we'll come off a false of the branch so if the save game doesn't exist we need to create a new one so that will simply be create save game object now the save game class is going to be the save game which we just created so for me that is the stats save like so and you can see we have saved game there already which is why we don't name it save game because again that will confuse you there and then the retirement of this we are also going to set the save reference like so so we're setting it to whether we are loading or creating a new one like that and then out of the bottom set safe reference we're going to cast to our stat save or just your save game which we've created at the start and the execution will go into both of the set save ref like so so you should have something which looks a little like this and now this cast here is going to allow us to access all the different variables inside of our save game so this is how we're going to set them so because i'm saving the game here we want to set and override the variables already inside of the save game so we're going to come out of as stat save and set our different variables so i'll start with set health like so now all we want to do for this health is just connect in the health variable we have inside of our actual player so the health i'm connecting in here is the player's actual health which we want to save as you saw i dragged it in for my variables list on the left then undo as stat save again set the stamina with this again being the stamina which i want to save in the actual player blueprint like so and you're just going to do this for every single stat which you want to save so again i have health stamina ammo and location but just do it for all of the ones which you want in your game like so it's a nice easy repetitive process like this and the final one for me is set location which is actually a transform because i decided i also want to have the player's rotation on there as well so for this one i don't actually have a variable for it because it's just the player's location so i'm going to come out of it and get actor transform like so so it's going to get the player's location rotation and scale but obviously i'm just focusing on the location of rotation so let's compile and save that so now we have overrided the variables inside of the save game however we also need to save it so what we've done at the moment is we've opened it and changed it but now we need to save it so very simply we can come out of this at the end and save game to slot like so with the save game object being our save reference which we have here and the slot name being again get slot name like so so nice and simple there that is how we're going to be saving all of our stats with player and just saving the game so we'll compile and save that i'm going to close that function and back in my event graph what i'm going to do is i'm going to figure out how i want to save the game so again for me an example i just want to do the one keyboard event so i'm going to right click and get a one keyword event pressed is going to be call function save game like so the slot name being the stock name which i want so what i'm going to do is right click promote the stuff variable naming this sort name save just so it's not a slot name that might confuse us as well compile and change this default value i'm just going to name it slot 1. you can name it absolutely whatever you like just make sure it's the same in every reference which we're using and just so i know that i've done it i'm going to put a print string here just for development purposes saying saved like so compile and save and that is now how we can save our game again if you want to maybe do this in a pause menu so when you're pressing save game when you're pressing a button for quit to access this you can cast to your character blueprint and then as that character blueprint for example adds dark clashing character you can call the function save game the same way i've just done here and that will work the same way for you and just as well so now we've got saving let's also set up loading so it's going to be very similar so we again need to create another function naming this load game like that and this one is a little bit more simple so again i'm going to start off the same way with a does save game exist slot name going into the event start like that then hold down b left click branch with the condition going into the return value and the execution there like so then this time we only want to come out of true because we only want to load the game if the save game does exist if it doesn't exist then there's nothing to load so we're only going to come out true and that's just with a simple load game from a slot like that slot name once again being get slot name like that and as you'll also see there we have the default of get slot name save and underneath that just get slot name so this one is the variable inside the player blueprint and this one is the variable inside the function so that's the difference there you can see in the bottom here we have local variables in the load game function and in here we're going to come up with the return value with a simple cast to our save game which me is the stat save and as that save what we're going to do now is instead of setting the variables we're going to get the variables so as stat save i'm going to get health like that and now we're going to set the health in the player blueprint so get health from the character variables list set it and we're going to set it to the value from the save object now i'm going to do the same for all of them so get stamina as well and again just do this for all of the ones which you have the same way we did for saving just now we're getting instead of setting now finally last but not least i've once again got the location so this is going to again be slightly different because we're not setting a variable this time what we're going to do is actually set the actor transform like that and that is all we need to do that has now loaded the game as well so we've got to save and we've got to load so compile and save because again what we're doing now is accessing that save game retrieving the variables and the values inside of it and setting our variables to be the same as the one in the save game so we'll close that and now we just need to load the game when we start it so i'm going to come up with event begin play here with a simple call function load game simple like that slot name again gonna be our slot name save variable which we made earlier i'm also gonna do a print string of loaded now this is gonna print off every time we start the game just because it's on event we can play but again it's just something the developer sees just to make sure it is firing off so compile save hit play and test this out so you can see we've now spawned here perfectly with the health stamina and ammo all to the max values because i haven't actually created a save game already so now again if i press these different buttons i have to lower and increase the health stamina and ammo you see all these values are going down and if i were to walk over here as well and also change my rotation and location press one says saved if i come out and go back in the health stamina ammo and location rotation are all the same as to when we last saved the game so this works perfectly so i think that'll be it for this video as we've done everything what to do we set up a save game system in which we can change our player stats or any variables which we want and then we can save and load this so i hit save now and then load it again when i press play you can see all of them are the same as when i last saved like so and again if i don't save it's not going to change 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] you
Info
Channel: Matt Aspland
Views: 33,975
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, unity, games design, graphic designer, ue5, unreal engine 5, level, up, exp, leveling, levelling, system, save, load, experience, point, points, level up, minecraft, save and load, levels, player, stats, location, health, stamina, and, ammo
Id: HJxYPJF6wzk
Channel Id: undefined
Length: 12min 45sec (765 seconds)
Published: Tue Sep 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.