Unreal Engine 4 Tutorial - Saving & Loading Pt.2 Level States

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome back for another episode in our saving unloading short series if you haven't watched the first part please go ahead and watch that first before joining this video the first part I cover a lot of basics of saving and loading and it's good to get understanding of those fundamentals before jumping into these more specific use case videos thereafter so this one is all about saving and loading a world state and what I mean by world state is that you have a level and the player does something inside that level such as open some doors and then quits you wanted to save which doors were opened and when I load up the game talk about what doors actually load up what doors were actually opened previously so um prior to starting I made a door quite simple one-way door opening up proximity the door opens if you'll know how to do doors I've got loads of videos about how to do different types of doors just head on over and search in my channel but you can see there it doesn't close and it doesn't save its State okay just opens up as soon as I walk anywhere near it so what I want it to do is save it in a safe game object when the door descends okay and I want to make it so I can easily store multiple doors inside that single level so what I've got here is I need to first of all set up this door to have a variable to turn it tell it to be open from start or not so I'm open my door object here and what you see here is basic door stuff if you want my tutorials this should look quite familiar and we're gonna add one thing to this and it's going to be a boolean variable so click on 'add verbals and you want to call it is open and you'll leave it as the red boolean okay and when this time line finishes down is finished path here I want a set is open to true okay click compile and this variable is going to obviously change when it's actually open but it's also going to be checked at the start of the game whether this door should be opened or not and what we'll do here is that a begin play event which is gonna check that variable so if I drag that available back out and she's get and put it into a branch I can now go down to true and change the door mesh so it's opened so here I'm just doing a basic translation in in in world space so I moved it down here by minus 300 so what you gonna do is set that static mesh well set relative location set that static mesh and there's said 1 minus 300 okay so I'm just basically telling it to go into the ground if it's open it's true click compile now if you had an animation like that it's a bit trickier because you want to take to run that animation and take to start from the last frame so it runs it from the last frame and stays there okay so that's how you do it of animation and maybe do another video later on I've got an animated door I could possibly show you okay so back to what the video is about so here I've got basic is open being checked if it's true it changes the location of the door to be in the ground by default okay-dokey so why need to do now is stall the state of this world in my safe game object now like in the first part I made a play player save data one more structure I'm also going to make a level structure as well for each level I'll have my game and so in blueprints I'm going to structure and go level first base there's states of argument I'm gonna call it level one structure your safe data and in here I would have all the variables associated one okay so the various doors I have no lights off switch song it can be anything okay so here I'm going to call this one door doors and in the doors I'm going to change this to a door actor and I'm going to change the type here they the container type from a single variable to a map okay so I'll do a map video if I haven't met video up by Tom this video comes out so head up and check out the map video and how maps work essentially it's like a two-dimensional array we have a bevel here and then we have another associated to that okay so it's door being a reference to a particular door and this is going to be a boolean to turn whether it's not it's opened or not okay yeah it look like save and close that so my level one safe data is there so the level one safe data I'm going to store and reference in my save game object first of all so click on the save game object and add a level one data and level one save data it's the structure ok close this down okay so with the save game object data in place the level one data the trick is now we have to be able to communicate with a safe game data or per level basis okay so the only way we can really handle that is through the level blueprint but there's a catch with that we can't reference the level blueprint directly so how do we get around that well one way to communicate the level blueprint is through a series of event dispatches and using the game state so the game state is something to have to create so if you've got to add new blueprint class and your search field you want to search for game state base and choose select now of course one my game state and my game state is going to game state is basically a client only level specific state of the game okay so it's what replicates all the data across different clients if you were doing a multiplayer game for example but it's a handy way to store information about the level that we want to be able to access easily from various actors so with my game state once we made it when you actually tell the game purse you use it so if you go to edit and project settings and click on maps and modes and in maps emergency detected game mode there's a gamestate class and here we can choose the my gaming state and we can close that so anything to do the level changing and you want to store that change somehow game stay it's a good place to put it so with my game state you want to open us up and we want to first of all create an event dispatcher and this is how we're going to communicate to the level blueprint so this event dispatcher we're gonna call door opened so when we call when we open the door we're going to call this event dispatcher on the game state and we're gonna bind it to a function on the level blueprint sounds very confusing it's kind of weird what bleep but it's the best way of doing it and simply as well I could find anyway so with a door open event dispatcher we want to set a input for this okay so we mix elected on the right hand side and it perilously inputs and we want to choose an input and we want to choose the actor so the actor the door can't spell door that we're going to be opening so choose object reference for nut and name just one door actor like so click compile we go close app and on the door once it's finished the timeline we set is open to true we want to get the game state and we to cast it to a specific game state my games thing and as my game state we need to coal door opened and door opens you can see that door actor input is required and we just put in a reference to self okay click compile and now on to the level blueprint so go to blueprints at the top and choose open level blueprint and in here we need to set up that event dispatcher to call in here so that door when you open it is send to the game state to shout out that a door has been opened okay and in here we can tell it a listening function or a listening event to listen out for that call being called so on begin play we're going to set up that listener so you get the game state and cast it to a specific game state and as my game state we need to find event at a door opened so when it's called this event down here is bound to it so here I'm going to create a custom event and here is I'm going to call it door opening out door opened underscore I think to open to be fine so door opened if the bound event is cooling this custom event and he said director is now coming out of it on the verbal side of things we can add our level one data so level one data and it's gonna be a type of level one data save data I can pawn apologies so you're going to go drag that level one data out cut a structure we need to right-click and split it to get a level one dated doors and a fun there we want to choose add from the map options and you find out any utilities you can just type in add but you will get a few various options we'll make sure it's utilities map add ok and what the add does it will add it to the map verbal however if it does exist it'll just overwrite it okay so here door opened is triggered and what we can do because this is a specific door actor we can get the is open value and plug it in like so so now this is now storing data when we open a door so it's got one through that series of events what happens then so I'm just compile this and show go through it when we open a door it runs a timeline and when let's hunt finished it will change this variable to true get the game state and call the door opened event on the game state we have a event dispatcher called door opened that is what's being called by the door on the level blueprint we have set up a game States door opened event dispatcher to be bound to this custom event on the level blueprint so on this custom event we are getting the level 1 data we've created over here this is empty enough not related to the safe day to at all yet again they're getting the map inside of that structure and adding this door and it is open status to it hopefully that makes sense okay so that's the long and short of it so this is good now finally change this data Prevnar she saved it to the save game so now we actually need to save our data to our save file so on a level blueprint we're gonna create two functions we're gonna create the save level data and the load level data and what we need to do here is on the save anyway let's just say first you need to get the game instance and cast it to my cane instance and from there we can get the saved game data so a saved game data has that verbal on here too so we need to basically copy this data over to our saved game data so here set level one data too much this okay so now it is now copying that verbal data into the save game data slot here after which we want to save game two slots which we can get from my game instance this is lots name like so so we saved a level data we get the game instance caster to the game instance to get a specific one and we're copying over our level one data and then we're saving the game to slot to the file now we go to low-level data level data so low level data works pretty much the same we're going to get the game instance cast to my gayness notes and there's my game instance we're gonna get the safe game object get a save data and then from there we can get level 1 data and storing it in our there ball on our level blueprint so that is copying then the data from our safe data file into our level blueprint as actually used that data yet it's just loaded it up so on the event graph here we need to actually take when disabling to load so the savings easy on door opened after we've done the ad we want to call the save level data on the load level data though we won't do that on the event begin play so begin play at the end of the bind event we want to act on the load level data and that will now populate the level 1 data variable with stuff from the safe data file with that stuff we need to do something quite fancy so the problem we would encounter is normally we would click on the door here go into our blueprint right click and get a reference to that a specific door however all makes a bit clever so I'll have may have multiple doors in my level and I don't do that process the same process for every single door so instead of doing that we are going to get all actors of class and choose your door from the list and then for each one of these you know for each loop we are now gonna get the level 1 data here that we've got from the safe data item and this is the safe data we've done from the load here and then from there we can split it and get the map and choose find the arrangement now can plug into that find so this finds will search through that map for this particular instance of door and that instance of door is at that list of doors over five doors in there it will go through all five of them and find the fired doors in here now you've got two Buddhist here the top one is our one that we've sashay to the map okay that's alum the pot one's a true or false whether they finds it okay so I will do a boolean to here the bottom one because if it's false I don't want it do anything okay only do something if it is true okay and so each door you're finding this level one data I wanted n set that door so drag your element out here and go set is open to true and the top find boolean will go into the ears open and click compile so now will now open all the doors that need to be opened so let's test this out okay so I didn't work because of one thing the begin play of the level blueprint we can't determine when that's going to occur in the whole list of other begin plays they all just kind of fire so what was happening is the level blueprint begin plows firing after the level where the door blueprints begin plate was so to counteract out what we need to do is once we up there change this value here we need to tell the door to update its status basically so on the door asset we need to add only begin play here and it custom event called update door let's not let me custom event update door there we go and that will plug into the same branch to begin played us so basically you just run begin play again okay so back onto a low level blueprint once we've done the array element setting is open we can drag out from there and go update door so now update each door with the new value okay alongside the update door we need to add on to our open door value here to check webinar is actually open first before we run this timeline again so drag D is open out any we want to check it in a branch if it is false we want to SI if it is true nope only if it's open we want to open it so false and we want to open the door there we go nest avail that's what so if this is is now I can add multiple doors to my map so I'll just put one over here and just raise it up I'd have to do any extra code for this to work so you can see the door is opened here going straight through and then this one will open once it's fully opened it will save it escape push play and you can see them now both open and close oh they both open so this isn't makes it really flexible so you can add in many many doors as much as you like so if you want to do a second level you just make a second level save data and add that to your save game object say save game object you're saving data for each in individual level separately and then on the level data on a level blueprint sorry this is where we want to handle that data and what we do with it so you could have new things they come not just doors think about anything could be NPCs weather so you want to keep that on a level blooping because that is unique to the level itself and how it works that's why with a save and load level on the level blueprint rather than the game instance because this data is specific to the level that we're running hope this makes sense you if you have any questions or you want see more specific stuff in saving and loading please let us know in the comments below and thank you everyone is supporting me thus far in picture on big help everything effing you do for me is great and I can't wait to see where we go in a future with this channel thanks again and I'll see you guys next time bye bye [Music]
Info
Channel: Ryan Laley
Views: 25,060
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, level, level blueprint, how to communicate, functions, event dispatcher, game state, game instance
Id: i8enbadjRF4
Channel Id: undefined
Length: 22min 51sec (1371 seconds)
Published: Thu May 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.