How To Save And Load Player's Location - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another religion 4 tutorial in today's video i'm going to show you how to save and load the player's location now i have done a video on just general saving and loading but people wanted a more specific one for the location so i'm going to be going through that today so let me show you what's going to look like so we're going to hit play we spawn here in a default spot we can walk around if i press one that's my save button you can see in the top left it said saving you don't need to have that i'm just gonna walk about a bit if i come out and go back in we're gonna spawn in that location if i move all the way up here i'm going to save again go out go back in it's spawning in that location and you can see it's spawning in that location and rotation so this is what we're going to be doing today it's a very simple safe and load feature for the player's location in your map and your level and stuff like that so like i say this is what we're going to be making today so let me delete this code and i'll show you how i've done it so the first step we want to take is we want to actually create our save game so what we're going to do is we're going to right click on our content browser go to blueprint class you can see the bottom here we have all classes for you it might be like this so you can hit the arrow there to open it up then search for save game there hit save game and press select i'm just going to name this one save game lock for safe game location you can name this whatever you like so you can have this just save game or player save or save slot one anything you want because this doesn't have to just be location you can put anything you want in here so we're gonna open it up straight away in here we're gonna create all the variables that we want to store so that we want to save so hit a plus variable here i'm going to name this one spawn transform as that is what i want to save i'm going to change it to be a transform variable there hit compile and that is all we need to do in here again you can put any variable that you want to save in here so this could be the location the amount of time supply has jumped the distance traveled the time of day if you have a day-night cycle or anything like that you can just put that in here so we're going to save and close that as that is what we need to do in the save game itself then we're going to go to our character blueprint so for me that's content third person bp blueprints third person character but if you use could be third first or if you've named it once we're in here we're gonna find some empty space so i'm gonna go down here i'm gonna right click and this is where we're going to get the keyboard event for how we want to save so you can do this in a widget if you want so on the unclicked button event or again just by a simple button press so i'm going to be doing the one keyboard event but you can set this to be whatever you like so in a pause menu you have a widget on clicked simple videos on that in my main menu and pause menu videos it's not specifically saved but it goes through how to use buttons so again i'm doing off the one keyboard event i have pressed i'm gonna get a print string now you don't need to do this this isn't necessary this is just for me so i know that it's doing it so in here i'm gonna write saving now this also won't show up for the player as you can see it's development only so if you want it to show up for the player i'd recommend creating a widget so again that's not necessary but after this what we're gonna do is we're gonna get a does save game exist this slot name we're going to right click promote to variable and just call it slot name we're going to compile so we can change its default value and in here we're going to write the name that we want for the save game so i just want this to be save slot one so you have more than one the name doesn't matter you just have to make sure that you spell it exactly the same in each reference that you use to it which is why i'm putting in a variable which is why i'm storing it there so i can then make sure the name is correct each time and if i want to change the slot name i can just change it there i don't need to change it in each one so i will actually also just name that slot name one after this we're gonna hold down b left click to get a branch plug in the condition into the return value there so this is checking to see if the save game does exist already obviously it won't as we've only just made it so if it does we want to load it so we're going to load game from slot like so the slot name for this again is going to be our slot name one variable that we just made there and then off of false so if this game doesn't exist want to create it i'm going to create save game object like so so if it already exists we're going to load it if it doesn't we're going to create one of the return value of either one of these we're going to right click promote to variable and call this saver subclass like so and we're going to set that off of both of these like so so it's the exact same variable that we're setting like so so we're going to load game set save for subclass or create game set save a sub class and i should also say in the create save game object in the save game class we need to put this save game we've just created so i named mine save game lock so that's what i'm going to put in there again what if you named yours put it in there and that is where we stored our variables that we want to use by either spawn transform then after this we're going to come out of the set save our subclass so the return value there i'm going to cast to the save game we just made so again that's for me cast two save game log there again we're going to do that on both of them there so we're going to cast off of both of those there so after the load and create game all of this code is the exact same for each line however we can't merge it into one line so as you can see these can only go into one return value and the same with the cast so we do need to just keep them separate so they still work individually on their own but out of as safe game lock or your save game we're going to set the variable that we made so i called mine set spawn transform like so plugging in there like that then what we're going to do is again do that for both of these and then for the spawn transform we just want to get the player's current transform so that is then where they've saved that's where they want to respawn so we're going to right click get active transform like so we're going to plug that into the set spawn transform of both of these like that so it's going to get the player's current transform and set that for where they want to spawn next out of these we're going to save game to slot so we've edited the spawn transform now we need to just save it again so it actually keeps that so get one of both of these again like so and this was this one we could actually merge at the very end like so we're gonna save game there the save game object being our saver subclass so get save subclass there the slot name being slot name one variable we made there and now that is the saving done so whenever we press our button we're gonna save the game if we already have the save game it will load that and update it with our new save if we don't have one it will create it and then create all the saves that we need in there so now we need to load it i'm just going to load it off of event begin play so when the player loads in but again you can do off a keyboard event like i have here you can go to which loads or anything like that like i say i'm doing offer begin play so i'm going to right click and get begin play and that will take me to it up here you see i have a sequence that's just because i've got the code going into here as well but you don't need that if you don't want it now then one or just event begin play get a does save game exist with again the slot name and slot name one out of this we're gonna get a branch again if it does exist as we only want to attempt to load it if it does exist so if true we're going to load game from slot like that where the slot name is again that variable so i'll just come out that one there return value of this we're again going to set the saver subclass and out of that we're going to cast to save game lock like so out of as safe game lock we're going to get the spawn transform this time not set but get and out of that we're going to set act transform like so plugging into the cast there and that is the code done so this is now going to load it so when we begin the game we're going to see if the save game exists if it does we're going to load that save game i'm going to set the player's transform to be what we saved as so this should be the code working perfectly for us now so we can save it and we can load it so let's hit compile save minimize hit play to test it so you can see we spawned here by default as the save game didn't exist so it didn't load anything if i go up here press one we've got saving up in the top left if i come out of this hit play again we've loaded back there if i move about hit one to save move again we're gonna go back in and we loaded in where we saved the game so this works perfectly so i think that'll be if this video is we've done everything we want to do we've set up a very simple save and load game feature for the player's location and again you can adapt this to be absolutely whatever you like which again i've done in previous videos as well 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 you in the next one you
Info
Channel: Matt Aspland
Views: 10,759
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, save, load, location, rotation, transform, save game, ue4 save and load, ue4 save, ue4 load, player, players, player's, ue4 save and load location, set, actor, in game, game, level, levels, create, slot, game slot, ue4 save game, ue4 load game, matt aspland save
Id: pM7XYBv-JUc
Channel Id: undefined
Length: 8min 29sec (509 seconds)
Published: Sun Feb 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.