How to Make a Game in UE4 Blueprints: Main Menu and Save Game

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this tutorial series we're going to break down standard gameplay features that are included in almost every single game we've played and also something that players have come to expect this includes a menu system saving and loading games picking up items player scoring loading maps etc what makes this series unique as opposed to other series on youtube is that we're going to show you how to do it cohesively now you can learn about the safe system in a bubble and you can learn about the game instance in another bubble but how do you make them work together that's what we're going to try to demystify [Music] so here's a quick demo of what to expect we have a main screen if we press start it will be a little dimming in the background there's no safe data enter a name for your saved data we'll call them aaron accept around exists we can delete aaron and then start and we'll have jaquelin accept if we press start again we'll be teleported into the first level you'll see a bunch of coins we can pick them up the coin counter on the top left will go up as we go if we go through a portal we'll be teleported to a second area if we go back in the first portal again our cache is the same the coins we picked up have disappeared they no longer exist we can toggle a menu to continue save or go back to the main menu continuing will make you come back saving will save your position if we go back to the main screen you're still there you can start and you're back to where you saved if we pick up some skull and crossbones you will die you can continue and go back to the last save state you had or you can go to the main screen and start all over again picking up hearts will restore your health and again going back into the portal the health will remain the same so before we settle into starting off anywhere we should start with the most basic parts the first one we're going to look at is the game instance so when you start the game this is now your game instance it's basically the program itself it is an overarching and encompassing object that manages the entire game there's only one it's completely unique and i'll show you how to set it up in a moment and basically it cannot be changed destroyed or recreated it's a fully persistent object so if you are opening the game the game instance exists when i close the game the game instance has been closed now to create your own game instance we've created one here right click go into blueprint class and look for game instance this one and then out of it you can create your own once you created your game instance you can go edit project settings and in the project settings if you look for game instance here you can choose what class to use you can use the default one or you can add your own custom one which is what we did here this is what i meant by this is unique additionally the game instance can be accessed through any blueprints by using the function get game instance after which you can simply cast into your own game instance and you can even convert it to a purecast since it's a surefire cast there's no other game instances in the game so the overarching hierarchy or structure that we're going to be using throughout this uh tutorial is that the game instance is going to be our be all and all manager so other actors will interact with our player character and our player character will communicate this to the game instance the player controller will also talk to the player character but it will also communicate with the game instance and finally widgets and their buttons will transmit in what needs to be done to the game instance and the game instance will resolve the tasks so for instance if i click the save game button in the widget it will first be transmitted to the game instance who will then perform the task to save the game similarly if there is data to be given back and forth between the save game and the actual game the game instance will handle that transfer so you can save the game on the hard disk and this will remain unchanged you can transmit it to the game instance and while you play the game the game instance will update the data while never touching your previous save however if you commit your save you will bring it you will basically override everything from the save game with the new information in the game instance this is how powerful this tool can be in our case here the first function that we will input in the game instance will be the loading screen so many widgets and many classes can create loading screens but it's always best to leave it to the game instance so it always keeps track of is there a loading screen right now and if there is do i remove it as such you can create the two functions to remove the loading screen or to create the loading screen to remove make a validated get you can do so by right-clicking and either get convert to purekit or convert to validated kit if it is valid remove it from the parent to create the loading screen simply do a validated kit if it exists remove it and then add a new one on top to the player controller and then set it as the new loading screen and finally add the viewport the next thing we will be looking at is the save game but before we go further we're gonna work with structs so for those who don't know what a struct is you can basically add any sort of variables in it and you can even add more structs in it as we will see soon so the first truck we will create is called the struct underscore character stats and in it we'll add health coin count and a name integer integer in a string i highly recommend that you add some default values for instance for health if you add zero you'll probably trigger death and we don't want to do that and for the name having a default name helps to know if your saved data is actually working correctly we chose bryant for this case next we're also going to look at another struct that is going to store other types of data including the character stats that we've as we've created earlier so here we have character stats as a struct instruct we're also going to keep the last level we saved at as a string the last save transform point as a transform and finally guids a brief note on guids guid stands for i believe global universal identification it's a random piece of letters and numbers that you can generate that will always be unique you can definitely use this to track several things you can track events you can track items being picked up you can track if you have a multiplayer game and you have specific gear items you can track if if the data matches the server with unique tags and so on and so forth in this example this will be used to track whether or not we've collected coins the next thing we can do is create a safe game simply right click go to the blueprint class type save game and then make your own we called our save game my save game once you open it all you have to do is add as a variable your save data struct here you can see what it looks like broken down if you get it you can break it into the character stats the last level saved at the last save transforms and the guid arrays and if you break the character stats further you can actually access more data this will be the basis for the saving and loading into the game instance to briefly touch upon these three nodes when you save game to slot you're gonna need the save game object type and you need to know the slot next you can also load from a slot if you have a slot name and return a value and finally you can always get a boolean to know if your slot is cleared or not in this case if i choose to load or save to my save game i will do something like this load game from slot call my slot 1 return the value slot 1 will identify which save game it is and then i can cast into it and obtain more data through it the second thing we'll be looking at in the basic game instance for now is the main menu uh functions so we're gonna set up the main menu functions first and then we're gonna call these functions from the main menu itself so the first function is of course you have to show your main menu obviously so again do a validated get if it exists remove it and add it again and set it as your new variable that you validated earlier and add to the viewport the reason we do this is because we don't want to stack multiple widgets on top of each other if you don't do this here and you do it at the widget level for instance and you click a button you may stack multiple times the same widget new game is going to be a function that's going to check first of all for a game slot so when we click the new game button first it will need to know which new game do you want for the purposes of this demo all game slots will be hard coded as one two or three so the first thing we're going to do is do another validated get we're gonna remove it from parent if it exists otherwise we're gonna create the widget and set it in our window basically if we want the new game window to pop up this is how you would do it next the loading game so we need to know what slot we're going to load the game from then we're also going to set the slot to our current slot so this is a variable that the game instance will hold throughout the duration of the game it needs to know at all times in what current safe slot is it working with if the safe slot exists which we do a check here we'll simply load it cast into it then we'll create the loading screen as we've done previously and then we'll push some of the data in it as our temporary data in the game instance and finally we're going to open the last saved level if no saved levels exist we're going to open level 1 by default if the save game does not exist we're going to throw an error and finally deleting the game we're going to need to save slot again we're going to need to know if the save game exists if it does delete it when we started the game earlier we had this widget pop up so let's briefly take a look at it and learn how we can actually make widgets with more ease so the first thing is that we created something called a vertical box so vertical boxes are containers that will stack objects inside vertically next it contains a horizontal box it actually contains three horizontal boxes each horizontal box will then contain three other objects a border which contains a new game title a size box that contains the start button and a delete button the size box itself is another container that can determine a fixed width or height the great things about using vertical boxes or horizontal boxes as opposed to dragging individual components is that if you need to move something at any point you can do so by simply selecting the whole item at once as we can see here i wanted to move the entire vertical box i can just select it and move it around another thing is if you want to center it pick the highest hierarchy parent in the alignment the alignment will be between 0 and 1 which basically like a coordinate percentage and you can choose where your point of alignment is if you choose point five and point five the alignment point will be dead center and then you can switch the position x and y to zero zero and have it again that centered anchored let's take a look at the actual graph itself so first things first is start game so when you click the start game button as we've seen earlier one of two things will happen either it will help it will toggle a let's create a save game state or it will just start your game so the first thing we're going to do is if we have a slot name we need to know does the save game exist in that slot if it does load the game based on the function in the basic game instance that we've created earlier if it does not exist again casted a basic game instance and create a new game with a new slot since this is a repeating function we've made it a general method custom event and then when we click on start one we're gonna do start game event and we're gonna make a little string for the slot name being one again you can make this anything you want for the purposes of this tutorial we decided to make it a hardcoded value of one two and three for the delete game function similarly to the creating game when you click into it let's get the game instance cast into it and get the delete game with the slot we want to delete as we've seen earlier if we go back to the basic game instance we can see what these functions were earlier create a new game load the new game or load game actually and the league game the next thing we're going to look at briefly is making a new game so here we also added a border in the back that's slightly dimmed just so we can emphasize that this is what you need to do right now the event graph will have three events the first one is on text changed which can be done to set a variable at any point so whenever the user types something it will set the variable um this allows you also for more flexibility for instance if you don't want special characters you can always manipulate the string here or the text to make sure to do a check if it's invalid you can make a message appear if it's valid you cannot this makes it easier for players if they have trouble creating names instead of always pressing accept and getting an error message they can get the error message on the fly once you click accept we're going to create a new save game object we're going to have to define the class that save game object we're going to save it to the new game slot the game slot by the way is instance editable and exposed on spawn so when we create the new game here create new game widget the same variable is exposed now if it doesn't appear to you you might need to recompile it a couple of times both in the game instance and in your widget close it and open it again it's perfectly normal and what happens is that when you call the new game function with the game slot it will be directly fed into the blueprint or the widget before it appears so it already has the information ahead of time so once this information is known it's fed into the save game we're then going to load from that same game we're going to cast into it then we're going to get our save data we're going to break it apart and the character stats the name that we've included here which is the uh the variable that is saved on text changed we're going to set that specifically using the set members instruct so we don't have to set anything else this is great because we don't want to set the health or the coin count we just want to set the name and finally once it's set we're just going to save the game to slot again and remove it from parent and finally if we click cancel we'll simply remove this widget from the parent the next thing we'll look at is how to create a loading screen now because of how fast the computer loaded everything we didn't really have a chance to see it but you may need to actually have a loading screen in your case so what we did here is we just made a border that's completely black and we added something called a circle of throbber that in the center and so it just rotates there are no functions here and so the end result is when you press play you can delete the data you can start and create new data and if the game slot exists or the safe slot exists you can start the game that's it for part one so far we've covered the main menu creating a new game and saving an existing game and a little bit of the game instance next week we'll be releasing a second video in the series which will cover the many different types of actors used in this project so be sure to subscribe to get notified when we upload the source code for this product is in early access on our patreon until the final video is released so if you want to check out the rest of the features right away head on over to the patreon and please support us thanks as always for watching and we'll see you next time
Info
Channel: Pending Kill
Views: 16,168
Rating: undefined out of 5
Keywords: unreal engine 4, unreal engine tutorial, unreal engine tutorial for beginners, unreal engine tutorial blueprint, unreal 4 tutorial, ue4, blueprints, epic games, game development unreal, unreal game development, umg, main menu, savegame, save game to slot, load game from slot, gameinstance, unreal motion graphics, widget blueprint, load game, ue4 for beginners, unreal engine tutorial 2020, main menu ue4, unreal engine tutorial fps, unreal engine 4 blueprint tutorial, gamemode
Id: C8FNZ6B7bU8
Channel Id: undefined
Length: 19min 49sec (1189 seconds)
Published: Fri Oct 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.