Save And Load If An Actor Is Destroyed - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another engine 4 tutorial in today's video i'm going to be going over how to save and load if an actor has been destroyed so you might have it so you've destroyed an actor you've killed an enemy or you've cut down a tree or you've picked something up you leave the game you go back in and it's back and you don't want that to happen once you've destroyed it you want it to stay destroyed so i'm going to be going over how to do that today so now i've actually got this from a thread on the unreal forums and the original answer was by scott spadespadi spadia sorry if i've watched your name there but this is also from 2016 so it's quite a while ago however there's no video tutorials on this and it actually came up because someone in my discord server asked how to do it and because obviously it's quite a long explanation i sent them that link but it can be a bit confusing to understand if you're a beginner into it or anything and i needed to get into it and test it all out to properly understand it all so i'm making a video tutorial on today and again i'll link the original thread in the description down below so go check that out as well but i'll show you what we're going to make today so i get in when you saw one destroyed there so actually let me delete the save game one second so i've just deleted the save game so if we test it out again we get in nothing happens i go over i pick up this item i save the game if i come back out it's gonna load the item is now gone if i pick up this one as well save the game leave go back in both of those are gone and if i pick up all of them save the game and leave go back in all of them are gone as it is saving us picking them up now you can see there's a split second where they are on screen and then they disappear that's just because it's having to load the game checked as if they need to be destroyed and then destroy them so obviously if you want to avoid that you can just put in a tiny loading screen to avoid seeing that or they might not actually be in plain sight of the player straight away anyway so this is what we're going to make today so again credit to scott thank you for this incredible answer it's a very efficient and good technique sometimes we're using his answer and a little bit of my own mixed into it as well to make it a bit more up-to-date and efficient with the newer engines since this is four or five years ago now again big credit to you so thank you i'm going to show you how to do that now today so without further ado let me open up a new project and get right into it so once we're here i've already got my item bp here and all it is is simply just if i walk into it it's going to pick it up by destroying the actor there so the first step we want to do is we want to create something called a game instance and so to do that i'm going to do it all in this one folder and to create it we're going to right click go to blueprint class we're going to open the all classes here and then search for a game instance there selecting game instance hitting select and i'm going to name this one my game instance you can name this absolutely whatever you like and we're going to open up straight away now obviously if you already have one you can do all the code in there just we need a new game instance for us to be able to use again if you already have a new one use that in here we're going to create a variable so hit the plus variable there and we're going to name this destroyed actors we're going to change it from a boolean to be a name variable and we're going to change it to be an array so click the little dash there i'm going to change it to be an array so the 3x3 grid compile that and now you can see we can put multiple items in here to see which actors we have already destroyed so what we're going to do is we're going to right click and add a custom event and i'm going to name this one destroy item scott named it collect persistent item because they were doing a collection system i'm going to name it destroy items that makes more sense for me and out of this what we need to do is we want to add unique now you can see we don't have any values to go in there so in here you can see a 3x3 grid that's going to be our destroyed actors array so we're going to be adding a unique value to our array of the destroyed actors so this is now asking for a name into this so if we drag out of that onto our custom event it's going to add a pin onto that if we select our custom event we can change the name of this to be item id or actor id or anything like that and that is just essentially the name of the actor we're destroying whether we want to put into the destroy actors here so we're going to compile and save that and now we're going to create a function in here as well so we hit the plus function here i'm going to name this one check if already destroyed and as it sounds it's going to check to see if we've already destroyed a certain actor which we are spawning into the game so out of this we're going to do is again get our destroyed actors array that we've made there out of this we're going to get a find item connecting the item there into the start node of our function and again i'm going to rename this to be item id so we are searching for the item id in the array out of the integer value of this we're going to get a greater than integer so an integer is greater than the integer and we're going to set it to be one and then what we're going to do is we're going to come out of the check if we already destroyed start function we're going to get a return node and we're going to drag out the boolean of that greater than into the return node and i'm going to name this one destroyed already so if the integer is greater than one we have already destroyed it if it's not we haven't already destroyed it and if we haven't already destroyed it then we don't want to destroy it on loading the game so we can do is we can compile and save that that is technically all we need to do in the game instance however i'm going to keep open as this is quite a large project it can be quite confusing so it's always good to keep it open until we're done with it one thing we do need to do though is go to edit project settings once it loads we're going to go to maps and modes and at the bottom we're going to change the game instance class to be my game instance or whatever you just named it and that just means that the game is going to actually use this new game instance we've just created so again if you already have one in there that you were using then use that one but i wasn't using the game instance there there's nothing in it so i made a new one to be able to use called my game instance there then we can close the project settings like so and also minimize our game instance as like i say we're done with that next up we're going to open up our pickup item bp or whatever it is you're destroying so if this is an item bp a gun you're cutting down a tree anything along those lines just open up that item and this can be multiple blueprints as well it doesn't just have to be one so it can be a gun and it can also be a chest or anything like that but i'm going to do it in this one specific blueprint named item bp here just as an example what we want to do is we want to come down underneath our code hold down p and left click to get event big in play if you've already used the event we can play you can go to that and if you have already used it then hold down s and left click to get a sequence connecting that into there and then zero into the code you already have then went into this code i don't have any code so i don't need it after this we're going to hold down d left click to get a delay i'm going to leave the duration as 0.2 but you can increase this if you need to so what we're doing here is we're just allowing the game enough time to actually be able to load the game before checking if we need to destroy it because we need to be able to load the save before checking it so the larger the game the longer this duration will need to be however 0.2 is good for me and it probably will be for quite a while as well but if it doesn't work the first thing you can do is just increase this duration and see if it then does work after this we're going to cast to the game instance we just made so i named mine my game instance the object for this is going to be get game instance like so and as my game instance what we're going to do is we're going to call that function that we created so i named it check if already destroyed like it says we're going to check to see if this item we're currently in has already been destroyed so to do that we need to put an item id in here so what we're going to do is we're going to right click the item id promote it to a variable and name this one actor id like so we're going to compile i'm going to change the default value of this to be actor id and then we're also going to take instance editable so that means that however many blueprints we place down each one can have a different actor id which is obviously what we want so the first one placed down we'll have actor id one the second one will be actually d2 and so on and so forth so that each one is independent in its own right depending on whether it's been destroyed or not and out of the destroyed already we're going to hold down b left click to get a branch with the condition being that boolean there because obviously if we have already destroyed it we want to destroy again so out of true we're going to get a destroy actor like so and false do nothing because when we begin the game we're going to see if we've already picked up this item if we have we'll do it again if we haven't we won't do anything so that will then work perfectly what we also want to do is set up code for picking it up so we have this code here you can leave that there what i'm also going to do is right click under here get another custom event which is get a custom event sorry not another one and i'm going to name this one pickup you can name this again whatever you like out of this i'm going to cast to the game instance again so i'll just copy this cast to my game instance with the object as get game instance and as my game instance what we're going to do this time is not check if we're already destroyed we're going to get the other custom event which we made which i named destroy item like so item id is again going to be our actor id we created in this blueprint so now it's going to pick it up and then also destroy it if we need to and so to fire off this pickup code i'm going to place this in up here so this is where i'm picking up just by walking into it what i'm going to do is just before we destroy the item i'm going to pick up so cool the customer event i just made and pick up like so very simple compile and save that and that's all we need to do in here so again it's going to pick it up and tell the code that we have picked up and we have destroyed it and then the next time we load up the game it's gonna see if we have already destroyed it and if we have it will get rid of it so it will destroy again so that we don't know it's there so we're gonna compile and save that like i say and i will close this one as that is all we need to do in there i know that one is right i know the game is this is right as well but again i'm going to keep it open what we want to do next is we want to create a save game so this way we can actually save and load whether or not we've destroyed it or not so i'm going to right click in the content browser go back to blueprint class again and in the all classes i'm once again going to search this time searching for a save game like so i'm gonna select that i'm gonna name this one d a save game for destroyed act to save game you can name this absolutely whatever you like and you can just put this in one whole generic save game which would make more sense and that would be better for you to do so i'll actually just give this a more generic name actually so i'll name it my save game like so don't just name it save game as that will confuse you later on as there is something else just named savegame as well but my saved game works for me that's why i'm going to name it and that works perfectly so then we're going to open that up straight away or again if you already have a save game then open that up in here we're going to hit a plus variable again i'm just going to name this one destroyed actors the same way we did before and again like we did before i'm going to change it from a boolean to a name and to an array as well compile and save and that is literally all we need to do in the save game it's very simple we just need to create somewhere in which we can store the actors that we've already destroyed so i'm going to close that as that's all we need to do you can minimize this again and now we need to create a player controller again if you already have a new player controller use that but i'm going to create one so i'm going to right click go to blueprint class once again and again in the all classes we're going to search for player controller selecting that and i'm going to name this one my player controller and opening that up straight away again if you've already got one use that and we're going to go to the event graph i'm going to delete the event tick but use the event big and play and out of that i'm going to cast to my game instance with the object obviously being get game instance like so and out of this what i'm going to do is right click promoter variable and i'm going to name this one game instance ref like so so we have a reference to our game instance because that is where we're storing the destroyed actors variable and array that we have there because that is where we're storing all of the actors we've destroyed back here i'm going to compile and save and in here is where we're going to do the code for saving and loading the game so what i'm going to do is right click add a custom event naming this one save game out of this i'm going to get a does save game exist with whatever slot name you want so i'm just going to right click promote that to a variable naming this slot name like so and i'm going to compile that and change its default value to be save one as i want the slot name to be save one but you can put this again as absolutely whatever you like but that makes sense for me and i'm doing it in a variable because that way we know that we can use this multiple times wherever we need it with the name being the exact same and if we want to change it we can just change this variable out of this we're going to hold down b left click to get a branch connecting the condition into the return value there and this is obviously if the save game does exist it will be true if it doesn't it will be false so if it does exist we're going to do is load game from slot with the slot name being that slot name variable we've just made the return value of this we want to cast to our save game we just made which i named my save game like so and as my save game what going to do is right click promote a variable naming it save game ref like so so now we also have a reference to our save game out of fault of this branch so if the save game doesn't exist we want to create a save game so we're going to come out of this and create save game object like so with the save game class being our save game that we created which for me is my save game like that and again we're just going to select the cast and the set reference there and duplicate that down here as we want to do the same thing so we're going to cast to our save game and then create the save game reference there as well like that now just after this what we want to do is we want to get our game instance reference that we made to get game instance ref and out of this we want to get destroyed actors so we're getting the list of actors that we have and we want to set this in our save game so i'm going to move this down here i'm going to get our save game reference here so get save game ref out of that we're going to set destroyed actors and we're going to set this to be the destroyed actors from our game instance so we're setting the list in the save game to be what we have in our game instance as well so that they are then up to date we're saving what we've destroyed out of the save game ref again what we're going to do is save game to slot so we're saving it so we put it in the save game now we also want to save the game and the slot name will again be that slot name variable that we made earlier and then just connect the set into both of these set save game references there like so i'll just put that in the middle like this and this should now work perfectly for us in saving the game so it's going to save the game put the list of actors that we have already destroyed so we compile and save that and what i'll do is i will comment that so select it and hit c i'm just going to name this save list of destroyed actors like so and now let's set up loading it so we've got saving it now we need to load it as well so what i'm going to do is just underneath this i'm going to right click add a custom event naming this one load game like that now with this what one do is get an is a valid node with the question mark in front of it the input object is going to be our game instance reference there because what it's going to do is it's going to fire this code off just before we actually create the save game reference here so there'll be a split second where it doesn't work which will just give us an error and also break the code so we just want to see if it's valid first if it is valid we want to do the code if it's not we want to check again because the next time we check it probably will be valid so out of is not valid we're going to get a delay i'll leave the duration as point 2 completed we'll go back in the execution like that i'll just put it underneath there to make a loop so essentially if it isn't valid it's going to check to see if it is now yes that will do it every single time until it is valid but the way we're doing it is we're going to load the game as soon as we load it up as soon as we hit play so we want to be able to load it so we want to be constantly checking because we want to load it if it is constantly checking it doesn't load then obviously something else in the game has gone wrong and you'd want the player to restart and the player would restart because they're going to be confused as to why they've lost all their progress so that is why we're doing this just to make sure that the game does actually load out of is valid so once we can load it we're going to get a load game from slot the slot name is going to be again that variable that we made earlier so this is why it's helpful to have that variable because it just goes in all of them making sure we spot it correctly and we can change it very easily later on the return value we're going to cast to my save game one or again just your save game and as my save game we're going to again set that to be the save game reference there so now we're going to be loading this straight away so there will be a first time where we load it before the play is saved so what's going to happen is it will just come out cast failed it will try to load the save but it won't so it will come out cast failed and nothing will happen which is perfectly fine as that's what we want then out of this set safe game reference here we're going to get destroyed actors and now instead of setting the save game from our game instance we're going to set the game instance from our save game so we'll get game instance ref again and it's time it's set destroyed actors connecting that into there and the destroyed actors list array will be that one there so up here in saving like i say we're saving the destroyed actors from the game instance into the save game now when we're loading we want to get the saved actors from the save game putting it back into a game instance reference like this and that should be that part done it's very simple to load it so i'm going to select this hit c to comment here naming this load destroyed actors like so compile and save that and that should be saving and loading done so what i want to do now is minimize this because like i say this is saving and loading so we're gonna minimize this go to the world settings of our level and if you don't have that you can go to window and click world settings it should take you to it and we're gonna change the player controller class to be that new player controller we just made which i named it my player controller again if you already have your play controller in there then that's fine then what we want to do is make it so that we load these actors when we load up the level so i'm going to go to blueprints and open level blueprint and then i'm going to hold down p left click to get the event begin play node again if you've already used it get the sequence that i went over earlier out of this we're going to simply cast to my player controller with the object as get player controller like so and as my player controller we're going to simply just call the function load game that we've just set up and created here so it's going to load the game and get all the destroyed actors which we already have so we compile and save that now this is the code done so what i'm going to do is just set up a function in which i can save the game so you'll probably have this set up already so you can do this in a menu when the player presses quits before it actually closes the game it will save it and if you want tutorials on that then just let me know in the comments down below but what i'm going to do for the moment is just on a button press so i'm going to open up my character blueprint which if i just select my character there and press edit third person bp what i'm going to do is just find some space get a one keyboard event as this is just for testing purposes now pressed i'm going to cast to my player controller the object as get player controller there and then as my player controller i'm going to simply save game like so and now this should be the code done so if we compile and save that we can hit play to test this out well firstly we need to put in the items so i'm going to drag in item bp there go to details drag another one in drag another one in and i'll i'll get four to test this one out so i get four there i'm going to select one of them you see we have actor id you can set this one to be actor id one this one to be actor id2 this one actually d3 and this one actor id4 now they're all different so they're all different in their rights so if we destroy one that is the only one which will be destroyed now that can be quite tedious if you have a lot in your game already so if you want i can look into better and more efficient ways of doing it probably by just accessing the actual name of the blueprint as well instead of us having to put it in so again let me know if you want something along those lines and i can look into it let's hit play to test this out so we go in and we pick one up here if we go out and go back in nothing should happen that's great because we didn't save the game if i go in pick that up hit one to save the game we come back out and go in that one did appear again so there's something wrong it didn't work first try it to be honest i didn't really expect it to because it's quite a big bit of code so let's have a look at why that happened so i've just found the error luckily it was quite easy to find very quick in our my game instance in our function of check if already destroyed what i did was i put greater than one this is wrong it needs to be greater than minus one if i put greater than minus one there not one this should now work better for us if we compile and save we can hit play to test this again well there you go perfectly if we load it in it's not there because we already saved it we load in it doesn't appear if i pick another two up and then save the game again by pressing one we quit we go back in they're not appearing so that works perfectly again that was one very easy mistake to make but i also noticed that straight away which is also perfect so now if i also pick all of these up so i pick the final one up as well press one to save go back in these have all disappeared as they have saved we've destroyed them now i'm just going to delete the save game if you're wondering how to do that by the way all you do is you go to where your project is saved so i'm going to go to horror that i have here go to saved go to save games and you just delete which one you named it i believe it was save one those other ones are just from previous tutorials now if i hit play they have all appeared because i've deleted the save game now if i pick up one of them let's pick this one up press one to save it that should have created the game the save game sorry hit play that has now disappeared it's no longer there so this works perfectly so i think that'll be for this video as we've done everything we want to do we've set up a system in which we can save whether or not we've destroyed an actor already so you see i've picked two up i've saved the game and go back in and they have been destroyed they are no longer there which works perfectly for us great little system which again was originally come up with by scott i've just put a video onto it because nobody else had done that and it was quite confusing for beginners and i've also just improved it a little bit to update it for the newer engine four to five years later so thanks so much for watching i hope you enjoyed and i hope you find helpful and if you did make sure to like subscribe down below so thanks so much for watching and i'll see in the next one
Info
Channel: Matt Aspland
Views: 13,233
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, ue5, unreal engine 5, save, load, ue4 save, destroyed, actor, destroy, destroyed actor, ue4 save destroyed actor, ue4 save if actor is destroyed, pick up, picked up, item, id, item id, save and load, delete, deleted, array, list, items, info, about, name, unique, persistent, state, check, already, play, begin, start, saving, loading, leaving, after, left, join, joined, re-joined, leave, game
Id: 1JNoP9u99ZA
Channel Id: undefined
Length: 23min 24sec (1404 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.