Set, Save And Load High Score - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to another i'm religion 4 tutorial today i'm going to show you how to set save and load a high score in your game now in today's example i'm going to be using the practice range that i made in a previous tutorial and it will look something like this so you can see i already have a high score there of 110 and if i get any more points it won't go up unless it goes above that high score so if i just quickly get some points and then i'll get back when i'm about to get over the high school so as you can see i just went up over the high school there so now every time it goes up it will increase the high score as well and then if i come out and go back in it saves that 180 there so let's get right into it so now i've deleted all that the first step we're going to take is creating our save game so to do that we're going to right click go to blueprint class down here you open the dropdown menu for all classes then we're going to search for save game like this hit that and then press select and we're going to name this whatever you like i'm going to call mine save game hs for high score but make sure you don't just call it save game as that will then confuse you further down the line as something else it's already named that as well so once you've done that we're going to open that up straight away and in here this is where you store all of the variables you want to save so for me all i want to do is get the high score so i'm going to hit plus variable there and name this high score like so and i'm going to make this an integer again put in here whatever you like you can compile save that and you can close out as that's all we need to do with that at the moment and then we're going to go into our first person character or third person character basically just wherever you are storing the points variable is where we then want to create another high score variable so as you can see down here i have points if i hit plus variable again i'm going to name this one high score exactly the same as we did in the save game and name this integer as well like so this just makes it easier for us to do stuff later on so again we're done with that so compile save and close it and now we're going to go back into our target blueprint or basically wherever you are calculating this score so if that's also in your character leave it there but this is where i am calculating the score and increasing it so once you open that up which again for me is the target what we're going to do is create a save a subclass so to do that we're going to hit the plus variable name this save a subclass and this is what we use to be able to reference our save game that we just created so once you've named that we're going to change the variable type from boolean to search for save game and we're just going to get save game object reference which is why we named the other one something else so we knew which one was which so you can do that compile it now we have that there and now what we want to do is to be able to create our high score when we need to so if we go down here where we are setting the points so when we shoot the head or the body we get our points up so go there we can do is come out of this at the end get a branch and everything can go into this one branch so i have two different ways of getting points for two different points so they can both go into this branch like so and the way we're going to calculate this so the condition is going to be if these points you have currently is higher than your high score so obviously if it's not bigger than the high score you don't want to be setting the high score to something lower you already want to set it when you get above that and you beat it so to do that what we're going to do is come out of this points here so if you don't have that go to character ref which we should have or cast to your character get the points variable we're going to drag off this and get a greater than or equal to integer you can plug the boolean into the condition there and then again out of the character what we can do is now get the high score so drag off get high score and that can be plugged into the bottom one there so this is going to check if our points that we currently have is greater than or equal to the high score it will then set the new high score so that will work like that i'm just going to double click these to create a reroute node to make this look a little nicer like so just to keep things organized so once you've done that and everything's going in there after this like i say we're going to set the high score so if we again now drag off of our points again over here so we get points drag off of this and if we then set high score up at the top here we're going to be setting the high score to the points we currently have if our points is above that so we can plug that into the true as obviously we only want to do this if it is higher so that is like that and then the target is again character ref from over here like so so now we have it set up to actually set the high score but what we need to do is save this as well and so to do that we're going to create or load a save game so straight out of this set what we're going to do is drag out and get a does save game exist and so as like it sounds this is checking to see if the save game we're trying to access already exists or not so the slot name you can name this whatever you like but i'm going to put this in a string so we don't have to rewrite it every time and we can just simply change the string if we change the name of the save game and it will change it everywhere we use it so to do that we're going to go plus variable i'm just going to call this slot name like so change its variable type to a string if we hit compile in the slot name here under the default value is where we can now input the start name we want so i'm just going to call this high scoreslot and you can name this whatever you like but make sure it isn't the same as your save game so i called that save gamehs make sure it's not that so i called my high score slot and again just plug that into the slot name there so you can just drag it on drag and drop it on and it will work like that so again this means that whenever we use this we can just change the variable to change it everywhere so then as you can see the return value is a boolean here so obviously we're going to drag off that and go into a branch like this so if it does exist yes or no if it does exist what we want to do is load game from slot so load game again get the slot name there like so and if it doesn't exist we want to create a save game object so create save game object there the save game class is going to be our save game we made so save game hs for me and then out of the return value of both of these what we're going to do is set our saver subclass like so so just drag and drop the saver subclass we made earlier on to there to set it like that and then once we've done that we want to cast to our save game so out of the saver subclass off that return value there we're going to cast to save game which allows us to access anything that is in that save game file save in blueprint sorry so again do that for both of them cast to save game hs like so then as safe game what you want to do is set the high score that we have so again drag out this set high score you can do that there like so now you can just put these all in the same one so as these are pretty much identical you can do them together although it's sometimes better to do it like this if you want to change different things so if you want to do something different when you load and do something different when you save like a noise or a sound effect or anything like that it's just a lot easier to have them separated before so you can easily do that then again set the high score down here copy and paste plug that in there and then as you can see we need to actually put the integer in for the high score and so what that is going to be is the high score that we set over here so we set the high score here drag this over and plug it into that like so what i'm actually going to do is again add the reroot nodes here to just once again make this look a lot nicer so do that like so and put that down there and then i can drag off of this and set that in there as well so now that's done so now we are going to save and load our high score when we shoot and get a point however this isn't going to load it when we first load up the level so one final step to do that is up here off of event begin play so if you haven't used it yet just get effective you can play but i have up here so i'm just going to drag out the timelines over here so drag those out to give us some space and we're going to load the save game when we start so again come out the character here and again we want to check if the save game exists so it does save game exist like that with the slot name that we made earlier from that string again return value goes into a branch because it's true or false the false will just go back into the timeline so if it doesn't exist then it's not going to do anything different it just goes and does what we want it to normally but if it does exist it's going to load that so load game from slot like so slot name once again this what string we made earlier return value again save a subclass set it there so it's basically what we did just now but up here instead so we are loading it when we first loaded the game as well again save for subclass we're going to cast to our save game give us a little bit more space and then out of the cast we're going to once again get the high score and then we're going to set the high score from the character ref so get the character reference there set high score and that will go into the one from the save game so plug that in there and again out of this go into the timeline so it does everything we want it to so this should now work so down here what we're doing is when we get points if the points is higher or equal to the high score it's going to set the new high score and when it does that it's going to see if we have a save game if we don't it's going to create one and set our high score if we do it's going to load that and then again set our high score and then when we load the game back up again it's going to see if we have a same game save game if we don't it will just do everything like normal but if we do it will load that and then set the high score and do everything like normal again so we hit compile save and we can close this now we're going to add this to the screen so i already have a widget from the normal score so open that up or create a new one by going to right click user interface widget blueprint to go to the points widget that we have i have this up here what i'm going to do is just make this a little smaller like so and then just add a text in front of it saying score so score like that make it the same size and you can do this in the same one but this is just a lot easier for me anyway so we'll do it like that just so we know which one's which and then we'll get the high score so we can duplicate both of those like that put them there for me it doesn't matter too much if they don't look perfect as this is just for testing then we get high score put that there this text box again what we're going to do is select the text box go next to text and this is for our high score next to text we're going to hit the bind button and we're going to create binding like so now what this is going to do is the binding sets the text based on a different variable we have an ie for me that is the high score in this example so if we just move this return node out a little bit like this what we're going to do is after the get text so the start of this binding we're going to cast to where we stored the variable and so for me that is the first person character again this can be whatever it is for you object for me will be get player character so i'm casting to a character and so as we're casting we can now access everything that blueprint so because of that we're going to as first person character get high score and then we're going to plug this high score straight into the return like that which will give us a two text to integer like this and then i'm going to change the minimum integral digits from 1 to 3 meaning it will not just be 1 it will be 001 which just looks a lot nicer so again compile save we can close that and now this should all be working so if we hit play to test this out we see that we have the score in high school up in the top up there both are zero we get a point they both go up to five 10 20 like that and if we leave go back in that didn't quite work as we missed one little thing which is back in the target we're gonna need to do again is once again save the game after we have done all this so we've set the high score here so if you don't know where we are we're just after we save it here after we get some points when we're set in the high school we just need to save it again so save game to slot slot name is obviously our slot name there save game object is our save a subclass so we can get plug that in there and we do this for both of them again so there's just one little thing i forgot about that i missed out but it's very important otherwise this won't work so now we've done that so again save to get the game afterwards otherwise we're just setting it without saving we've done that now if we try this again we see score and high score zero we got some points that both going up as we just set the high score something new so that's 55 so if we exit go back in the high score is once again 55 and if we get some points that's the score is five the high score is 55 as it's not gone up yet he's getting some it's not going up yet as you can see when we get above 55 it's gone up to 60 and they are now going up together like so so this is working perfectly so i think that'll be it for this video we've done everything want to do we've set up so we have a high score system in which we can set and load it like so so that we have the high score and it only goes up when we actually beat it so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did make sure to like subscribe down below so thanks so much for watching i'll see in the next one [Music]
Info
Channel: Matt Aspland
Views: 17,362
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, 3d modelling, blender, unity, games design, graphic designer, ue5, unreal engine 5, save, set, load, score, points, fps, high, system, save score, load score, set score, ue4 high score, hi, game, save game, load game, from, slot, to, blueprints, widget, screen, bind, binding, higher, than, then, greater, equal, or, set save and load, save and load, ue4 save and load, integer, minigame
Id: Iu3pgdEJnv8
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Mon Sep 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.