Unreal Engine 5 Tutorial - Save Game Normally

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is going to be a new tutorial about saving a game and you may find a lot of based tutorials about saving games like how to save a character location or how to save a cube location but uh unfortunately on uh normal projects it will not be enough to just save a character location you will be you will have to save a location of a big amount of actors of other characters of animals enemies you will need to be able to save like inv inventory data and so on so uh in during this tutorial series we will be able to create some kind of system that will help us save in a game in more easy way so but first let's create a new folder and call it save game and the first thing that we need to do we need to create a blueprint class called save game and it's going to be just save game select it and name it somehow I will call it BP Main save well this is an empty uh blueprint and it will contain some variables that have to be saved well now we have to create some components that will handle loading and saving so let's create a new blueprint class actor component and let's call it save game manager save everything and this one will have such functions like save game for example load game and that's it for now well what do we need to do not every actor on the scene needs to be saved only part of them for example my static mesh of a stone should not be saved it will be at the same place every time so we have to define whether we want to save an actor or not so let's create a new actor component and create it savable actor save all and let's open it up first how we first thing that we have to do we have to get an owner of this component get its tags and add some tag well my tag is going to be something like savable just savable well now we will be able to get all actors that should be saved it will be every actor that has this component Also let's add a new variable and call it should be saved and by default it's going to be true because for example you may have one nectar but in different states and in one state it should be saved in another not for example during a building you will spawn a nectar of a wall and while you haven't builded it it should not be saved it should be saved only after it has been builded well now we have to get some data to be saved so what we're going to do we're going to create a new structure let's create a blueprint structure and call it s savable actor data well what it will have it will have some basic uh Fields first is going to be an actor class of type actor class well second one is going to be a transform transform of type trans form well I don't think that I need to save it in my game but maybe you want to have some field like is actor currently visible or is it currently hidden but for now that's fine also some of your actors may not have only transform or class it may have some custom variables it may have some custom enumeration for example like my animal can have its own parameter for its current behavior and uh and so on for example threee can have a damage and uh the way we're going to do that we're going to create a new variable and name it custom properties and it's going to be a string map to another string well now let's close this a savable actor data and open our savable actor here we will create a new function called request data and um what we going to do here we going to ask an owner of this actor to give us some data well by the way we have uh to think whether it should be in a synchronous way or asynchronous maybe you want to finish some Works some jobs on your Rector till you will return some data or not let me think a little bit I think that I will note that's why I will be able to just return this data here well and and the data is going to and we must have an output called Data of Type S how have we called our structure savable actor data so it's going to be savable actor data well now we have to ask our re actor somehow so let's create a new blueprint and let's create a new blueprint interface and call it BPI save data provider save all open this interface and here let's create a function called get savable data and it will have an output called data and it will have a type of savable actor data compile and save now here we going to get an owner and call this function call get savable data and we're going to return it okay now let's open for example my stone basement that's it looks like this and I'm going to add this component called savable actor save compile by default it is enabled and also the thing that we have to provide have to do is to implement this interface that we've just created so let's open class settings also I'm going to delete save Bor from here and add it into apparent class of my basement it's buildable part actor so I'm going to add savable actor here open class settings and in implemented interfaces I'm going to select BPI save data provider save and compile now open my Blueprints and on interfaces you will have a function that is not implemented yet get savable data double click on it and you have to implement such function so we have to return this data let's do it like this let's make S save able Data actor and an actor class is going to be get class oh not an interface just get class well get class yes that's correct get class why can't I plug it here okay object is going to be self and now we can plug it here then we're going to get actor transform plug it here well I don't have a custom parameters that are changeable for my buildable part but you may have them and if you have them you can just make custom properties and enter some key and some value let me do it for example like this let me add a variable called health of type float and I'm going to provide it so that it needs to be safe so I'm making a map and first key is going to be HS and the value is going to be HS converted to a string compile and save now I can close the B buildable part and go back to savable actor what should I do here nothing I think so let's navigate back to save game manager now we have to implement a function called save game well first thing that we need to do before saving game we should collect all data that needs to be saved so let's create a new function and call it collect savable data okay actors data compile save well what we're going to do here we're going to get all actors with tag and the tag is going to be one that we've added in begin play of savable actor my tag is savable so I adding it here so we're able to get all actors with text now let's get component let's for each of them we're going to get a component by class and component is going to be savable actor so first thing that we have to check whether this actor should be saved so let's get should get should be saved and plug it here compile and save next thing that we have to do we have to call get data not a savable data how it's called request data we're going to request data and we should add this data into some array so let's create a new local variable called datas of type um what is the type of this data savable actor data savable actor data and make it an array now let's get it here and add a new field data well when everything is finished here we're going to return this array so let's add a return Noe on completed node of for each loop we're going to get datas plug it here and let's call it data now let's navigate into save game function what we have do what we need to do here we're going to collect saveable actors data at first and we have to check whether currently we already have a save game slot name is a slot name so you may have save one save two save three save four and so on so it's going to be a save one by default but by by the way it should be provided in an input of a save game function or count it or it should be counted like the last game save game slot was save two then a new one should be save three well we will only work with one slot for now now let's check whether save game already exists also I'm going to call it from a small letter save one because I think that I have already one from a big one from a capitalize so if save game doesn't exist then we have to create a new so let's create save game object here so it is f if it is false we are creating new save game object and the class is going to be BP Main save that we have created earlier well now we need to open our BP Main save and here on variables we have to provide a data that is going to be saved with this main save so let's create a new variable and call it actors data and it's going to be an array of savable actors data structures make it an array compile save well back to save game manager here after creating a save game object we have to set this variable so we just call set set actors data and it's going to be this data let's promote it into local variable and call it actors data well get actors data from local variables and plug it here now we fill this set maybe we will have a other fields that have to be saved but now it's just on data now we can just save game to slot we have such function and now it will be written on a disk so we have to provide the save game object this is one that we' just created and Slot name save one it's going to be the same as here compile and save well now our game will be saved in another case if this save already exist then we have to override it so let's load save load game from slot slot name is going to be save one well we don't need to load it by the way we can override it with in the same way so we don't have to check whether a save game exists right now it may be used in future for for example telling Char whether he he sure that he want to overwrite a currently existing save but now we don't need it and let's at the end let's print what whether save was successful now we have to call this function from somewhere let's add this save game manager to our character let's open BP main character in my case it's called BP main character and okay let me delete this file save and add this component save game manager well we have to call this function save game and load game from somewhere so let's add some input thebug key o for example for saving and we're going to get save game manager and call save game from here and let's add a new one The Bu key L for example and we're going to do the same but we're going to call load game from from here compile and save it well now let's check whether our save will be saved I'm typing o and it's written true every time so it's over reading and everything is saved maybe well now let's Implement function called load game so uh what we're going to do we're going to check whether a save game exist does save game exists so a save game at slot save one and if it is if it exists then we're loading another case let's print some text like save game doesn't exist well on the TRU we're going to load game no load game from slot and the slot name should be the same as here also in default it should be provided somehow from an input of a load game but we don't have it right now okay slot name is going to be save one we're going to cast it to BP Main save because by default it is just save game object well from here we can get actors data well now we have to handle the sectors data somehow and uh print it oh and not print it I mean I mean we have to spawn some vors with the data that is stored here so let's uh create a new function and call it load actors from save data and save data is going to be a data savable actor data it's going to be an array compile and save and let's call this function from here and plug ectors data here well what we're going to do here we're going to Loop through each actor data that we have then we're going to break it and first thing that we need to do we have to spawn an actor you have to spawn actor from class well now we have some custom properties and they cannot be handled by the save game manager and they cannot be handled by this savable actor by the way they can be handled only somehow by my buildable part because my buildable part knows about health in this custom properties but for example another Vector that have such component like savable ector will not know about such parameter and it will handle them in its own way so what we're going to do we're going to open our B save data provider and create a new function and call it handle handle uh C okay load custom save properties properties and now by the way it's not a provider already but I don't know how to name it right now and an input will be a properties properties by the way we should uh honestly make a new interface but but I don't like how the management of interface Works in unre so I don't want to create a new one well now we have this new function and on save game manager we going to get the sector and call this function heler handle well um custom Pro load custom save properties and here we have to provide this custom properties well by by the for now it will not do anything but let's open my buildable part or any actor that implements this interface and you will have a new function called load custom save properties and well it is an event now because it doesn't return anything so here we already can get oh we can find we know that custom properties for this object should have health and we're going to get no not get health and we going to type in heals and we're going to set Health here like this and let's print I don't know how we will test it by the way but it will work it should work now let's open our save game manager load game and check what we have here we're checking if a game exists we are loading a game from a slot conting it and loading actors from save data now let's test it out let me pick up my hammer build a basement for example uh build some other types of a building well and now let me tap on o if I remember right oh now the game should be saved now let's restart game and let's type on L and as you can see everything that I've created appeared at the same locations well and unfortunately I really don't know how to show you how Health Works let me let me let me um okay let's do it like this so let's open a stone basement for example or not let me open my buildable part and I have a function called set prev and it is false when it is builded well and when my for example basement was buil it I'm going to generate a new health so let's set HS and let's make it a random float in range from 0 to 100 and let's print it so let's print HS so now when I will build some part we have a printed value on the left corner like 25 HS now let's print it on load so let's open load custom save properties setting HS here and let's print it again here so we're going to print a HS that was loaded okay let's so let's build a basement one and its health is 74 now let me save a game restart it and load the game and its health is 74 the same as it was so that's it for this video also uh I would like to mention that it works easily with uh some basic types like float integer and deceptor converting I mean converting from string to this type but string cannot be converted for example into a trend trans form also uh string is hard to convert into a enam well it's not hard but you have to write your own converter you will write something like this you have to switch on the string add some pins and if it is basement then you're set in a type basement if it is a wall you're setting an enumeration type well that's it so that's it for this video hope it will help someone and thanks for watching
Info
Channel: Dengojaba
Views: 2,264
Rating: undefined out of 5
Keywords: unrealengine5, development, tutorial, game, ue5, unreal engine, multiplayer, coop, online, replication, system, fps, optimization, blueprints, instances, save, save game
Id: D2c2wv3pQ_E
Channel Id: undefined
Length: 26min 37sec (1597 seconds)
Published: Mon Dec 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.