Unreal Engine 4 Tutorial - Saving & Loading Pt.1 Basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome back for another ue4 tutorial this episode was voted on by my patron so big thank you to everyone who voted and showing your support this video is all about saving and loading how to create save files hadn't load from those files and various in using various variables to do so so the worm will approach this video is do it as soon as the videos actually is start with this basic how to save and how to load and some thoughts behind it and then what I think I'll do is do separate videos for individual use cases so things that particularly people have asked me for such as start saving in inventory system for example so we're going to showcase at the basics in this video and then go into more specific ones in separate videos so the whole saving loaded system in ue4 is actually really simple it all handles on one object that with the create and it's our manipulation of that object which we have to work around now they should go without saying that you need to make sure you're planning well ahead when you are creating a game this in particular comes in even more into light when you start working with saving and loading systems you need to make a list of all the certain things you want to save and load makes it a lot easier going forwards than it is to go back and try and fix things so before you get started try make a list of the big things that you need to save and load okay so for this video I'm actually just gonna show you how to do a single variable and then we'll go into how we can do multiple variables as well so prior to filming this I set up an XP system for my player character here so I call it XP bar in the top right hand corner there and if it the X key it increases okay but if I push play you can see it doesn't save so that's what we're going to try and do make it site stores and saves that experience bar value so it's very basic example okay so the purpose of video is just to teach you the fundamentals of had a semi loading and but then you'll have to tweak it and learn from this to use it for your own use cases so the waste saving and loading works is that you create a single object and that object is a saved game object so I'm gonna go add new blueprint class and if you haven't got this all classes expanded just expand open and search for save game choose that from the list and to click select and you will name this one way we're gonna call it so I'm gonna call mine save game object and this safe game object export is actually saved to a save file it's not various actors or their very various values is only this object so the difficulty that you have to manage when making a game is the transference of verbal data over to this safe game object so before I get cracking on now I'm actually going to show you how we actually create a save game file so this object hands with data and that is stored to the file so the file is Craig quite easy with a single node however we want to be able to create this and have a reference of it throughout the whole entire game now we can't put on a game mode because game modes could change based on what level win in fact there's only one really wild one real object that it goes across and it's persistent across all levels and all modes and that is the game instance so we're gonna create our own custom game instance and we do that by right clicking again and she was blueprint class and again your search box you wanna cut search for game instance and you want the basic game instance and you're gonna call it my game instance now a game instance it's the very first thing it's loaded up it is the thing that initiates a whole entire game and what we're gonna do in here is open it up and we are going to set up our safe file so we want to set this so file as soon as the game is loaded in this case anyway so on my init event sounds for initialization since the game is ran I want to be able to store or create a save game file and if there is one already existing I want to be able to load from that save file so to do that we need to first of all check with another save game file exists so you just search for a save and you'll see does save game exist come up choose this and plug it in so our save game exists node requires two inputs has a slot name and a user index so the user next we can leave a 0 that's fine we don't really need to do much to that and the slot name however is the name of the basically file name ok so it's a check for this file name so you could just type it in here however I prefer it have a variable this allows me to change things on the fly for need to or add multiple save game slots and check for them and so on and so forth so it makes it a bit easier so I'm just going to do save slot name and you can change to a string and click compile and we will click compile we can set a new default value for a safe slot name and here I'm gonna call it save data okay drag your save slot name on to your slot name input and that's now going to check for a file inside it saved game folder named after this save shot name and that will spit out true or false which we put into a branch like so so if it's true it does exist we want to load data from it if it's false we want to create data let's start off with creating data so drag down from force and type in save again and you'll see create a save game object choose that node and here you can choose a class that you want your safe game to follow so you want to choose your safe game object that you made this will get you a return value the return value we want to store because we need to keep referencing our safe game object throughout the whole entire game experience because we need to be able to write data to it whilst the game is running so we need to drag out from a turn value and click promote to verbal and I'm gonna name this one save game data and that'll do there the next thing we need to do is do this true branch so hit the false creates the safe game object and stores it The Trews going to load a save data so from true we're drag out and call load load game from slots there we go and here you required a slot name and we know exists because we've just used here we can just drag in a verbal and hook it up and if the load games from slot is triggered it would turn a save game object reference now I can set it to my previous one however you can see I can't connect it that's because this is a generic save game return value we need to give it a specific saved game data reference because this safe gamer is our personal one we just made so we need something in between it so we need to cast it first of all so choose cart so save game object and then hook it up like so and that is all you need to do to set up the save file so checking whether no exists if it does exist we're loading it casting it and check in there is a safe game object and storing it as a reference in safe game data if the safe file doesn't exist we're going to create a save game data file and set it as our save game data click compile and we are now done so that's the game instance now we need to tell the game to actually use this incidence instead of the default one and we do that inside the edit project settings and it is this way of doing it it's a search in details at the top and type in instance you'll see game instance class right there at the top you can choose my game instance just close this window so now this game is using my game instance notice the game still runs just as it normally would it's just using a different instance to initialize the game but now we have a safe game file so how do actually save data to it well a safe game object needs some variables to store data to so we could quite simply just put in a plus variable here and call it XP and my XP is done as a float so if I could type in float and click compile my XP or my player is a float if I show you it's there see XP bar is a float I want to make sure that a is exactly the same type in my safe game object as well so let's actually save data to it so when I push the button on my third person character I wanted to actually save that data to that safe file now say file exists in a reference form on the game instance so what I want to do is get a reference to the game instance now here my begin play I've just got my head up display showing XP bar and after that I'm gonna get my game instance and store it as a reference so topping get game instance anyone a cast that to my game instance I'll store that as a reference by promoting it to available and I call it the gain instance now reason why I'm didn't begin place because caste could be quite expensive to do in in CPU time so Walmart do is a typically do it once I need to do it once so I've built my begin play and go reference to date the specific game instance being run now I've got that I can drag my game instance available out and just get and then from there I can get my save game data and with my saved game data I can now set the XP of that value what makes people so when I pushing the button is increasing the XP by then I'm storing it inside my safe gained reference okay click compile all so then we need to actually save the game so what we can do is if we just type in save game to slot you've got options here to choose the save game object which is this one here and the slot name which is on the game instance slots name over to there and click compile and this is a save the game and if I push play my XP increases and it's now saving it to the game showing any errors over when we push play again with us it hasn't loaded up that's because we're only saving a data to file whether read from the file to like the load so what we need to do is accomplish that is go into our third person character and on begin play we're going to set the XP bar - oh sorry set the XP bar to a value stored inside that saved game data so in the game instance you should have a reference now to to save game data in there you can get the XP and hook it up like so go back to the game push play and you can see it is now stored that saved data from the file if I increase it by number to exit push play you see it save that data so what we've seen there's basically how it works however it isn't perfect because it's kind of messy really the save game object is only got variable in it and the third person character is doing all the work it's got all this stuff imagine doing this for every single bit of data the player could have okay so it can be quite long so what we want to do is set up some functions to make this a lot easier and a lot neater so to make it a lot neater we're going to go into a game instance and now game instance we're going to add some a function in here which allows to save and load the player data a lot easier so I'm gonna create a new function behind the functions list on the left hand side new and I'm gonna call this one save player data and when we click the save player data function we want to just go through the motions of what we had in a third-person blueprint so in here we're going to get player character and we're going to cast that to the third person character and the third person character is then going to give us all their date data so we can get their XP for example okay so what we need to do now is get a safe game data so drag out from your reference list on the rebels and we need to set XP and I'm gonna place a local variable for now and store this as play out Ref and with that reference I can now access its XP bar that's on the player character and store it in the XP variable inside the safe game data so when I'm saving I just it handles all that for me okay so what's a player data I'm gonna load play day as well load player data and much like the same we did we need to get the save game data here get XP and we need to store this on the player so we need to get player character cast it to a third person character and then from there on the pub promoter to a local bevel again player and set the players XP like so the reason why I'm setting the player reference is because it saves a lot of line beings lines being injected over the place from the blueprint so if you've got a player reference you can just drop that in whenever you want to get variable of the player or set it for example so click compile and that'll do for loading and saving a player beta and and then on the player itself we're gonna change what's going on here so here on the loading event begin play I've got my game instance being made and storing the gaming's there rather than doing these three notes I can just drag from here and type in a save looks like load player data okay and likewise on X only X key here rather than doing all this malarkey I can just get the Kane instance and to save player data like so I've just remembered I've forgotten one thing we were doing a safe player data so I could compile on that double-click my safe player data I've got I she save it so he was sitting the savegame data and here I just wanted to type in to save gained his slot so choose the save game data and a slot name yeah okay and that will do there click play now and you'll see it still loads up still fine allows us to increase it escape and load up again but it's a lot neater now because we've got all that data all that hard work being done by the game instance rather than a player character so in look keeps it nice and clean but why else can we do with this well what if we want to clear this safe data on the safe day on a gain instance we're gonna create another function called clear save data and that's quite a simple as doing clear well save data as a function all right let's not yet save the der where is it delete so delete gaming slot there we go and this would delete the save file that you want named click compile and we now just have a key for this so I'm just gonna apply a character and I'm gonna do seeking so wait first I'm just going to call that clear data clear save data compiled so now well push play if we push the C key and exit I push play and you see it's cleared that safe data completely deleted the slot which allows me to redo a demonstration again so that's the clear in safe data what if then you want to say autosave so what I can do on that front is on the game instance I'm gonna click on functions and click new function I think type in auto save and all auto save function to be on a loop so if I go to my event graph on initialization I want my game to do a loop so here I'm gonna go and set timer by function name and the object itself is going to be itself so you just type in itself function name is what we just made such autosave time I want it to save every 5 seconds and I wait to loop okay I may want to do a loop a timer autosave you may want to do other things as well like checkpoints for example you make a saving checkpoint it must have much the same as this just without timer so an autosave function here which can do save player data like so and compile so if I there's no way to test that because we are moving around and in it but saving anyway what we can do Danny instead is also show you how to save more variables other than just one which we've got so I'm a save game object look at XP but what I want to store say the X and Y and Zed location of the player character so here I could just add another local another variable here called vector vector type but I'm asking do something a bit more clever and instead what I'm going to do is create a struct so if I right click and create a structure in blueprints and I can call this one prior save data and here I wanna store all the variables that I'm gonna store about the player so this could be location location vector and I may also want be XP add a new one XP which is gonna be a float and it structures just a collection of variables my save game object rather than use the XP here I'm gonna delete this and I'm going to add layer data and I want to use that structure as a vocal type player save data click on pong and there you go so close this go back into my game instance and let's have a look at save and load player data again so click on save player data so rather than doing this because this doesn't exist anymore we need to do something a bit different so let's delete that and well move this along I want my save game data and all assets to be player data but here I can right click on the player data and split it and here I can put in the player day of XP and all can also get the get actor location for the player data location that can go into the safe slot there and there you go yes it compile and that's because I haven't done the load data I don't load a top so let's do that again for the load data so from a saved game data reference I'm going to get the player data structure split it and the player reference we're going to set the location so add a location to this and would play reference again set X P like so compile and that should okay okay oh so by default player beta is set at zero zero zero therefore I'm stuck in the floor becomes zero zero zero for this world is in the floor so let's just get the default value for this so if I click on my start location here by default location is - 750 390 into two six roughly so if I just go into my safe game object and my default location is going to be oh that's just okay so location is minus 750 it's a default on location 390 in the wire and 2 to 6 roughly said compile play and so I start in the correct location now also save is carrying on in the background so every 5 seconds it should be saving my players location and XP so that's what that should be fighting us by now that's good escape push play and there you go ok and there you go there's the basics of saving and loading now as I said I appoint do specific use cases as separate videos so if you have any specific queries about saving a loading that you want to see let us know in the comments below and I'll think about what if I can make a video though thank you to everyone has been supporting me thus far on patreon and subscribe to my channel thank very much been a big help knowing that I've got you guys got my back it means the world to me thank very much once again if won't support me ain't get access to videos nice and early head over to patreon calm for slash Ryan Lane Lee and thank you very much again to everyone disappoint me and I'll see you guys next time for the next video bye bye [Music] you [Music]
Info
Channel: Ryan Laley
Views: 68,646
Rating: undefined out of 5
Keywords: unreal, unreal engine 4, ue4, tutorials, how to, explained, beginner, blueprints, saving, loading, variables, save game object, player data, data, files, slot, load, save, clear, autosave, game instance
Id: hf98GRZA2d8
Channel Id: undefined
Length: 26min 45sec (1605 seconds)
Published: Wed May 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.