Building System Part 2 - Saving and Loading Actors Placed By Player - UE5 and UE4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back my name is jackson and in this tutorial i'm going to show you how to do a saving and loading system for the building system that we created in the previous tutorial so what this looks like is if i press play right now we've loaded up this world full of that i've placed down on the map um and if i open up my building mode and we're scrolling through our actors this is the stuff we set up in the previous video which will be linked in the description and i place a bunch of these down just here and then i close out and then load back in all those actors have been saved right there um and this is stored on your hard drive and yeah and it also works for different levels so if i were to create a new level uh just a basic level like this okay and i drop in like this build exit load up they're saved like that okay so you can do this for as many levels as you want today we will be working in unreal engine 5 but this will also work in unreal engine 4 and the next video in this series will be building upon what we've done here with a couple of new finishing touches for this building system and then this whole thing will probably be available on the marketplace so if you're interested in just buying it not going through it maybe check out the marketplace and it might be ready by the time you're watching this video okay so we're building a system that can save the state of this world and all of the objects that you've placed down as a player to build with and then save that on your hard drive and then be able to reload that same level when you load into the game so that you've got a persistent um save game across different instances of the game so that you can build close the game reopen the game and be able to resume where you picked up off so the first thing that we will do is probably create a new game mode which is going to store all of the um the saving and loading logic so this can be a game mode um so i'm going to call that gm underscore um building saving and loading yeah gm building underscore building saving and loading and let's um let's go to our project settings and maps and modes and we'll change our game mode our default game mode to be gm building saving and whatever now inside of that we're going to need to set some things up so our [Music] default pawn class needs to be our building character and that's actually probably it so if i jump in we've still got our building character like in the last tutorial and everything builds just fine okay so now we need to keep track of we want to be able to tag all of the actors that we've built so that when we close the game we can find all of those actors and save their transforms and then load them back up when we open the game again so this is inside our building character blueprint every time we place build an object down here when we left mouse button and we build an object let's add a tag to the build object so we'll go add tag or get tags and then we will append that array append or can i just say add add yeah we'll just add we don't have to append it we can just add a new thing to it and this can be um i don't know build object is what we could call that or you could give it like the player name or something like that but you just want a way to identify that this actor that was spawned is a is a building um so built object is what we're going to call that now now what we can do is so inside of our game mode um let's get rid of all this here we have uh begin play which i think i just deleted and i think there's an end play note as well event and play yeah okay so when we be when we end play and if you're curious about all these reasons here so session was destroyed level transition blah blah blah quit basically we don't want to switch on any of these we just want to say whenever you end play we're going to get all actors with tag the tag is going to be our built actors thing but instead of manually typing that into there let's create a new function library down here so blueprints uh blueprint function library call this fl for function library or bfl for blueprint function library and just call this building saving and loading and we'll call we got a new function which will be called get built actor tag and this is just going to be a simple get function so we can just hit the return node like that uh and then we'll add an output which will just be a string and what was it actually a string i don't remember no it was a it was a name so if we if we convert this to a name um and this can just be tag name and then what if we go make literal name and then we go uh what was it built object yeah so that's our tag and then we want this to be a pure function up there so now inside of our building character instead of having this manually typed in there call this we're going to say get built actor tag yep just like that so now we can just call this instead of typing it out and if we want to change this name later we can change that easily so when we get all actors with tag we can put the tag name in there and then for each of these we're going to want to save we want to save these to a um to a save game so how we gonna do this i think we're gonna get the class we're going to get the transform get active transform um and these are the two bits of information that we're going to be storing you could save like the person who placed it as well um if you want to do that but we're not going to do that let's just do the simplest setup so let's um let's let's maybe let's maybe cut that and let's go back into our function library and we're going to call this save built save all built actors and we're going to copy this into here so i'd rather have this in a functional library so we can move this and it's not tied to the game mode so whenever we call save built actors we're going to get all of the classes and add them to an array and this array is going to be called um built actor classes and we're going to do the same with the transforms add to an array and this is going to be built actor transforms built actor classes and built actor transforms and then that's actually everything there so that's um get all actor get all built actor classes and transforms and then after we've done that then we're going to check um does save game exist the slot name um let's get current level name and we'll call the slot name after the level name or maybe what we can do is we will put an append here just so we can make this a bit more descriptive built actor save data and then underscore the level name so this this save game object is going to be yeah this okay um [Music] and then we need to do some save game so if a save game does exist then we're going to load the game from slot and you know what we might actually do a similar thing here that we did with the built actor tag so i might um cut this create a new function and call this get built actor save game slot name for level for current level bit of a mouthful but basically by setting it up like this we can have a different um save game object for each level so you know if you're in this level you can save the state of this world but you can also have another map or endless amounts of maps with their own save data so same thing as before return node and then we can just plug that into there and make that a pure function okay and now back in save all built actors we can get that new function get built actor save game blah blah blah blah feed that into the slot name like this um if the save game exists then uh we're going to load that game from slot we're going to cast two actually we're not going to cast you yet because we haven't created the save game object so this is the blueprint where you actually store the save game variables and this is the this is the file that will be stored on your hard drive so if you say save game here we'll call this sg underscore built actor save data and i guess inside of this so that you're going to have one of these for each level we're not going to have a new function we can get rid of that we're going to have two variables in here which are going to be the actor classes just like we did before class references and the transforms so these both need to be arrays so up here array and with this one here up there that needs to be an array as well this is going to be saved classes and this is going to be saved transforms so you're going to have one of these for every single actor so now that we've got that we can cast to the sg built save data and we can set we can set what was the thing we can set our saved classes and we can set our um transforms our transforms like that and we can set them to our local variables which we saved up here so like that and like that um you know what might even be no this is probably fine um okay and so if the save game doesn't exist then we create save game object and it's of this sg underscore built actor save data um and we do the exact same thing down here well actually there's probably a better way to do this what i might do is i might just cut all of this here i might cut all of this and i'm going to create a new function which is going to be called load built actor save data for current level and so when we call this we're going to use that function that we did before what we can do here is we can promote this to a local variable which is just going to be called sg underscore reference and then we can have the return node over here and we can copy this down here as well and use the same thing here and then feed that up into there and then ctrl drag to put that into there and then we can rename this to call this sg underscore building saving uh what oh just building save game ref um and that will return a reference to the save game so now we can use we can use this function whenever we want to access the save game for this particular level we're already at 11 minutes my god this is going to be a pretty hectic tutorial i think um what have i got myself into so in our blueprint function library when we're saving our built actors let's load um built actor save data for current level i might actually call that built actor save game by reference for current level yeah and this one has a typo in it as well so this one up here i'm just going to rename that so now in our save all built actors this need this can't be a pure function because if it's a pure function every time you call you use this value here it'll load up a different instance of the same save game but we want to work with just this specific one here so now that we've got that there we can we can plug these in here so the reason that i did that was instead of having two branches where you know one time if it was if it existed we'd do this one or if it didn't exist we'd do that one we can just have this one execution line up here and just plug that into there so that that that's just going to load up a save game and if one doesn't exist it's going to create one that's basically what that does and then we can get rid of this so this will be load slash create save game and save state of world basically and now that we've set those we need to drag off this and we need to say save game to slot and the slot name i might actually i might just put that into there whoopsy daisies slot name um and then back in save all built actors we're going to just drag that slot name through to there so i like having it like this because it's like you can see that that's being saved this save game has been modified here and then saved here and this is the slot name of this save game and that's this is being saved to that slot as well so you can directly see where the the connection there um okay so that's literally all you need to do to save so i might just have a print no we don't even need to print it let's just see if it works um so that's saving but we also need a way to load it but you know what we could probably even test that this is working already so if i on end play we're going to need to save all built actors okay and then we might have a print string for i don't know like 20 seconds or whatever which is they're going to say game game saved games okay so if i jump in and i press b and i place a bunch of these and then i press escape nothing happened nothing happened um are we using this game mode i wonder no see the world settings it's got a game mode override so we want to set that back to default so um b escape no you still didn't work gotta be me um that's the default game mode event end play okay i didn't want to print the string let's see if it actually saved the game so i'm just off on another screen right now and i'm just opening up my project file for this project which is called what do we call it building system tutorial yeah so building system tutorial in saved here save games you can see that that's it there so it has saved um so just to show you how to get to that unreal projects or wherever you've got your projects the name of this project saved save games and then you've got your save game in here so build tech to save data that's what we called it and then underscore the name of the map up there which is the current level name so that's your save game and if you want to delete it you can just press delete like that so it is saving but it's not printing the string for some reason which is a little bit odd but maybe maybe it's because it was in the separate window maybe if i oh no i'm playing in the editor window yeah i don't know um okay well it's obviously saving because it's right there so this print string isn't working for some reason but that can piss off now we need a function here to now load up this save data when the game begins because it's saving but it's not actually loading now so inside of our i wonder if you can spawn actors in a function library i don't actually know let's just let's just see spawn spawn actor from class no you can't you can't spawn actors in a function library so that's good to know so what we need to do then inside of our game mode is we're going to load our built no not save all built actors we want to load them load build actor save game by reference for current level okay that's the function that we created before we want to get the saved actors or the saved classes get the saved classes and then for each of those classes you can probably see where this is going now we're going to spawn actor from class and then we're also going to get the transforms save transforms and the transform that we want to use for the spawn transform we say get copy to get a copy of one of the elements of this array and it's going to be the array index of whatever array element is right there so that'll spawn each of these actors at their corresponding transform and then we want to add that saved actor tag which actually instead of copying this code let's go back into here and this is in our bp blue building character let's cut this go back into our function library and say add built or add yeah add built actor tag to actor i guess is what you'd call that copy this in here alt click that to delete that and then connect this up the target actor goes into there so yeah that's actor and then it's adding the tag to it okay um back in the game mode or sorry back in the building character now we're going to say add built tag to actor okay nice and simple and now we're not copying code we're using the same function over here so add built actor tag to actor so now once these are loaded up when we close the level again this function is also going to recognize all these actors and save them again to the hard drive it's going to overwrite the hard drive um i think that could be it so if we comment this [Music] end play save all actors with built actor tag and then begin play beginplay load all actors with built actor tag there we go do you reckon it'll work let's see huh okay nothing loaded uh is that because i deleted the save game it might have been all right so we've built them we've exited so it should have saved we did it we did it and if i exit again and load back in again yeah there so now if i were to um place some of these in and maybe one of these and we'll rotate some as well um like that okay should we put an ai in as well let's put an ai up you know up on here oh my god look at the legs it's got that um inverse kinematic footstep thing that's sick all right so we'll put you up there okay so take a mental picture of this back in there we go and now you can if you package this this will be all saved on the player's hard drive um and it'll also work across different levels as well so i think i will show that at the start um that might be it for this one guys hope that was helpful drop any comments you have in the comments section below and peace out
Info
Channel: Slightly Esoteric Game Development
Views: 1,849
Rating: undefined out of 5
Keywords: ue4, unreal engine, game development, indie games, game dev, epic games, artificial intelligence, dev log, gaming, streaming, tutorial, building system, the forest, minecraft
Id: TSjUFyG2CDo
Channel Id: undefined
Length: 21min 27sec (1287 seconds)
Published: Tue May 31 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.