An Easy Guide to Save and Load Your Character in UE4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so there you are taking on all the enemies grabbing all the loot leveling up your character upgrading your abilities but for some reason the devs don't have a way for you to save your game so the next time you boot up your game all your progress is gone can you imagine [Music] well let's not go back into the past let's look forward to the future in this video we're going to cover some of the basics of saving and loading your game in unreal engine 4. of course saving and loading can mean slightly different things from game to game depending on the complexity of your game and what's needed but in general the idea of saving and loading your game is to allow the player to close the game and when they return at a later time just pick up in the same spot that they left off in and for this tutorial we'll be keeping it simple by just covering saving one character inside of one level it's important to remember that each game is different so you might need to load just small things like what level the player's in uh some complex games might need to know specific things like which quests have been completed which quest is currently active and which step of that quest the player is currently in so clearly there's different needs for different projects but what they all have in common at least if they're using unreal engine 4 is that every project has a save and load feature that utilizes a save game class to save all of the information that they needed to pick up where the player left off and that information is always stored inside of a save game class with specified variables and for this tutorial i made a small example project where we have a main menu where we can select new game to start a new game or we'll be able to load our game in our level right now we haven't set up any functionality for load game so if we click it nothing will really happen on new game if we click new game we'll load into our level and we have some pickups inside of the level we scripted some functionality inside of these coins basically whenever we overlap our animan character or increase our coin count and destroy the actor our animan character basically has an integer variable that keeps track of how many coins he has collected and while we're playing we can collect a coin and it updates on our heads up display we can keep collecting coins and it goes all the way up until we run out of coins but right now we don't currently have a way to save anything that we've done so if we play again new game we're back to zero coins with our animan character so it's basically like we restarted the whole game and that's probably what your game looks like right now so what do we have to do in order to save this integer variable from our animan character well we're gonna have to create a new type of blueprint called a save game class this is something that we mentioned earlier and the save game class is going to save variables inside of a separate file in our game in fact we can go to that location right now if we go to ue4 projects under saved there'll be a new folder that's created when the save game object saves the game to a slot we haven't done so yet so the folder's not even here but this is where the save game is going to create a separate file with all that information right here inside of our folder and when we load the game we'll need to read that file so that we can get those variables and apply them to our new game instance so let's make a new save game object inside of our project so that we can more thoroughly explore it we can right click on our content browser go to blueprint classes and search for save game under all classes then we can click select i'm going to call this save game object player since this is going to keep track of our player information that we want to save it doesn't really matter what you name the save game object this will just help you keep track of which save game object you're using if you have a bunch of them and if we double click on our save game object it'll open up and it basically looks like a normal blueprint and if we want to add a variable to the save game object whenever we save the game object to a safe slot it's going to take whatever this value is and put it into a file in that save slot right now in this example project for this video i only have the one variable that i want to keep track of which is the amount of coins that our players picked up so instead of this being a bullion we can have it match by being an integer since our animan character uses an integer variable to keep track of how many coins he's collected and we'll just call this saved coins collected so now what do we do we have a save game object class this right here inside of our project and that class can store one integer when it's used but how do we use it that's the real question and this part can be a little complex because you have to choose a blueprint that you want to use to save and load your games some people like to put all of their save and load functionality in their game instance for other projects people like to put it inside of their game mode i'll let you determine where you want to put all of the save and load functionality but just be sure that it's in a place that's accessible by the player and any checkpoint systems that you might have set up whenever you want them to be able to save or load the game for this tutorial i'm going to be putting all of the save and load functionality inside of my game instance and right now i don't have a custom game instance to use that i have access to so i'm just going to create one i'm going to go to blueprint class and we can select game instance and hit select i'm just going to call it game instance underscore tutorial and whenever you make a custom game instance you just want to make sure that it's assigned to your project by going to your project settings maps and modes and under game instance class just change it to whatever your game instance is it looks like mine automatically selected game instance tutorial and if you're wondering why i used a game instance for this instead of using like a game mode or player controller or even just a player character uh game instance is pretty useful since it stays persistent throughout an entire play session so if i start the game in my main menu widget and then i load a new level to my tutorial level one i'll still have the same game instance and all of the variables and everything will still be the same inside of that game instance so it's just really convenient for me to keep track of which levels have been unlocked and everything if we have like a hub world level where you can select which level to load or something like that it makes it a lot easier in my opinion so let's open up our game instance and we can start creating the meat of how to save games inside the unreal engine and this part's really critical so the first thing we're going to want to do is give ourselves a way to save some information so let's create a new function and we can call it save player save game object and we can compile and save that in our function the first thing we're going to want to do is create a save vm object kind of like how you create widgets and things in other blueprints we can just right click and search for create save game object we'll need to specify which class of saved game objects we want to create this is important because if we have multiple different classes of saved game objects like one say game object saves all the level information one saves all the player information we'll want to specify that this one is going to be our player information so our save game object player and we're going to need to access this return value a lot so let's promote this to a variable and we can just name this player save game object so now with this done there's a blueprint that exists in our level that we can transfer our player's coin count to so what we're going to want to do is get a reference to our player save game object and we can set its saved coins collected amount and what are we going to set it to well we're going to set it to our player character our animan characters coin collected amount i don't think we have any references to our player character inside of our game instance so let's set that up inside of our animan at eventbegin play for him he can get the game instance and then we'll cast to the game instance tutorial we don't even have enough we don't even have a variable for our player character so let's uh let's set that up our bp and a man um i gotta have it here and a man character there we go player character so you can just set up whatever your character is here just make sure that your game instance has a variable with the same variable type as that character and then we're gonna get that variable that we just made and we're gonna set it set player character and then our player character blueprint is gonna set itself as that variable so whenever we save the game we can get our player characters coined collected value and set that inside of our save game object there we go so whenever we call the save player function it's going to create a save game object and it's going to set our player characters coins collected inside of that save game object now we just need to save it to a slot this is the part that actually generates the file that's stored inside of our saved folder and in order to create that file we're going to need to save the game to slot so we can plug that right in we're gonna need a reference to which save game object we're saving let's go with our player savium object and then we need to create a slot name we can name this whatever we want as long as we call this when we load the game up because that's how it's going to determine which file it chooses to load later on when we load the game i'm just going to call this player info and you know what let's promote this to a variable that way we can more easily and accurately load games later on and off the return value we can do a branch and we'll do two print strings if it's saved successfully to a slot we can say saved successfully and if it failed in saving then we can say save failed this will help us with debugging just to know what's going on so we're gonna want a quick way to call this function right now just so we can test out if it's working inside of our player character we can promote the game instance to a variable i'm just gonna call it game instance and on keyboard press maybe f we can get the game instance and save player save game object so we can test this out if we minimize here go into our game load a new game and let's collect some coins collect like five hit f and it says on the top left that we saved successfully so if we go to our folders we'll see the new folder that it just created called save games and it's called player info because that's what we set the slot name to be called so this is our save game data let's see if we can open it with like notepad or something now we'd kind of just get a bunch of gibberish but it does have our saved coins collected amount don't really know what integer property these are but yeah this is our save game data now we'll just need to reach out and retrieve this data when we load the game because saving the game doesn't really do anything if we don't load the game and to load the game let's go back into our game instance we can create a new function and we'll just call it load player save game object in this function the first thing we can do is right click and search for load a game from slot plug that right in and we'll just get our slot name that we created earlier or you can manually type it in there and what this is gonna do is it's gonna check this folder the save games folder to find out if there's any file with this name in there with the player info name and it'll get us a return value off of that we're going to want to cast to our save game object player just to make sure that it's valid and also a save game object player type and when we get this we can set our player save game object to the return value of the cast that way we have an easily accessible reference to this save game object that we loaded from the slot so once this is done we can get our saved coins collected variable that's the saved amount from the slot and we can get a reference to our player character and set their coins collected variable to match there we go so this part is kind of the opposite of the saving if you remember we are getting our players coin collected value and setting it inside of our save game object to save and when we load we're getting the value from the save game object and setting it in our player character so this should work inside of our animan i'm gonna give us a quick debugging way to do this i'm just gonna do keyboard press g maybe and off of our game instance we can load the player save game object so let's play let's go to new game and i collected one but if i hit g it should load the values from the file that's in our save game folder it looks like we had five when we saved the game last time so we can collect a few more and get this integer number up up and up then let's hit f to save save successfully we can go back to new game and when we hit g we can load and we're back at 12. and that's awesome our coin count goes to whatever it was before and that's exactly what loading is in our last play session we got the value of what our coin count was we put it into an external file then we booted up this one we got access to that external file found out what the value was and set it as our coins collected so how can we expand on this let's say i start the game and i want to load into the same spot so i collect a few coins i save here and then exit out and then i hit g so i'm back to my same coin count but how do i get to that same exact spot that i was in last time when i saved the game for that we're gonna need to keep track of more than just the coin count total so let's do that in our save game object right now we're just keeping track of the one integer we can add a new variable and this one's going to be our transform for our character we can call this saved transform or saved player transform that one might be a better description of what we're trying to save here and in our game instance tutorial under our save player save game object we can send our saved player transform let's just move all this over to the side or you know what might be better i i think this is going to be better if we set this here and then we just put all of this in a macro so actually setting the data inside of our save game object can be its own macro and we can just expand the macro whenever we want to add more things to keep track of and let's plug that into the output okay so in here there we go perfect i'm just going to call this set player data and this actually doesn't need an output we can just get another reference to the player save game object and delete that there we go so inside of this macro we're gonna set all of our save game object parameters what are we gonna set our save player transform to what we're going to get our player character and we can get the actor transform whatever that value is we can set it inside of our save game object here now when we load the game we'll need to load more than just the saved coins collected which is what we're doing right here we can set our actor transform by getting our saved player transform and plugging that right in there and let's see if that works so just a couple little moves and that should work let's go here we'll need to collect a couple coins hit f to save and new game we can hit g to load and that moves us into the spot generally when you load the game you want to do it at begin play and that should move you immediately as soon as you start playing in fact i think we're at a spot now where instead of hitting these two buttons to save and load i can set that up inside of my main menu widget inside of the event graph so when i hit the load button i can set my input mode and then load into the level and also load the game the system that we have set up might work for some people if you really have a rudimentary save and load system if this is all you need if the player has full access to saving and loading on command but generally you want the player to save and load from menus instead of just hitting a button so let's take it one step further and just get this all set up from our main menu widget so inside of our main menu widget we're really going to want a reference to our game instance i already made a variable here we can get our game instance cast 2 the custom one that we made and then you might have to promote it to a variable i already have a variable made so i can just plug that right in now we easily have access to this game instance and inside of our game instance event graph we can just make some simple scripting for loading games let's create a custom event and we can call this menu load game we can call this from any menu as long as the menu has access to our game instance so when we call the menu load game we're just going to want to get our load player sgo that's going to load up the game from the slot and do all this we probably don't want to set the player information because there is no player character inside of our main menu level so we don't really want all that to happen we could either create a new function for this and call it from the player and event begin play so when they're created it could load all of it i think i'll do that yeah i'm going to make a new function specifically for this let's uh let's do that we can get rid of these inputs and i'm actually not even going to call it when we load our player save game object i'm just going to call this transfer player data and we don't need any inputs except for the execute since we already have a reference to our player save game object so in our event graph we're going to call load player save game object which is going to load the one from the file right here and it's going to set it to a variable now we could simply just open a level by name and manually type in what level we want to open but we can make it a little more sophisticated than that we'll put this over at the end here if we keep track of what level our character is in when we save the game then we can just call it from this function right here so let's go to save player save player sgo actually our set save player data and inside of our set player data we can right click and get the current level name it's going to come out as a string so let's not plug that in just yet we're going to need to add a string variable to our save game object so that we can keep track of the name of the level that we're in we can call this saved level name there we go and off of our save game object we can set the save level name to match our current level name and we only have one level inside of this tutorial so it's really not necessary to do this but if we ever want to scale this up it's really nice to know what level we saved in when we save the game so that we can open up that level when we load the game and when we load the game we can get a return value but we can get the saved level name and have that as an output there we go now in our event graph we can get the save level name and plug that right in all right so let's call this event inside of our main menu we can get a reference to our game instance make sure that it's the variable type of your game instance and we can call menu load game so when we load the game it's gonna load whatever level we had and all of our parameters inside of our player character at event begin play so whenever we start playing as him what we can do is get our game instance and we can call the load player save game object and we can also call the transfer player data function as well and that's great so we can do that let's play and if we select new game our animan character is automatically loading all this information even though we selected new game so we need to do one more thing we can create a variable inside of our game instance called load game question mark just set it as a boolean and by default it can be set to false but when we load our game we can set it to true [Music] the only thing we're really going to use this boolean for is inside of our animan we can get load game [Music] and we're only going to call these two functions if load game is set to true and it's only set to true when we do the menu load there we go that should be a little better so now when we click new game we'll just start off like normal and we can collect some coins collect some coins let's manually save and if we load the game we're in the same spot and we still have six coins so that's about it for the basics of loading and saving your game inside of unreal engine 4 we'll probably have a follow-up video about how to save level states so that these coins don't keep respawning every single time we load the level up otherwise we can get infinite coins be sure to like comment and subscribe it really helps out against the youtube algorithm i'm joe von d here to help you think like a game developer stick around for the next video on the channel [Music] [Music] [Music] you
Info
Channel: Joe Von D
Views: 7,425
Rating: undefined out of 5
Keywords: Unreal Engine 4, unreal engine 4, unreal engine, ue4, ue4 tutorial, ue4 beginner tutorial, ue4 beginner, unreal engine 4 beginner tutorial, ue4 series, ue4 tutorial series, unreal engine 4 tutorial, ue4 save game, ue4 load game, unreal engine save game, unreal engine 4 save game, ue4 save game to slot, ue4 how to save, ue4 how do i save?, ue4 save guide, ue4 save character, ue4 game instance, ue4 save state, ue4 save location, ue4 load character, ue4 save tutorial
Id: rgc4c3nIpD0
Channel Id: undefined
Length: 24min 29sec (1469 seconds)
Published: Wed Oct 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.