Make Unreal Engine Game User Settings BETTER with SIMPLE C++ | Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Have you ever made an Unreal Engine settings menu and been like, "I love this game user settings node"? And after some time, you were like, How do I save a custom value in settings? And after a Google search, you were like, "Wait, I need to make a save game class just to save a single value?" And then you learn that it will not save those settings in the config file like other settings, but instead, it will be saved as a save game file, that you can't even edit by opening it in a text editor. Wouldn't it be cool if you could simply make a better game user settings node that would save your setting values together with other settings? Well, we can do that, by simply struggling a little bit with C++. The first thing you need is Visual Studio or some other programming IDE that's set up for Unreal Engine development on your PC. If you don't have that set up on your PC, the Unreal Engine documentation has a guide on how to do that. You can find a link to it in the video's description. The first thing we need to do is create a C++ user settings class. To do that, we need to open the Tools menu, then click New C++ Class, then All Classes, and then, as a parent class we need to select the Game User Settings class. Then click next and choose a name for your class. I'm ok with a default name, so I will leave it as it is. And then we can click the create class button. Now it will take some time until the class generation is done. Because our project was a blueprint only, we will see a warning message telling us to close the editor and build it from our IDE.  Press OK, and then we will get a message asking if we want to edit the code now. Press yes, and after some time your IDE should open; in my case, it's Visual Studio 2022. You can also open your programming IDE from the Unreal Engine editor's Tools menu, or you can open a Visual Studio solutions file from your projects folder. Now, before we write code, we should close the Unreal Engine editor. We can see that Visual Studio has already opened two files for us: our game user settings class header and CPP files. If you don’t have these files opened, on the right side, you can open a solution explorer, from which you can locate and open your project files. Let's explore two ways we can add variables to your game settings and use them in blueprints. The first thing we need to do is declare the setting's variable. To do that, In the header file, after the line generated body, we need to type "protected" and put a colon after it. Then, in a new line, we will type in all caps UPROPERTY; after it, in round brackets, we will type Config. Now, in the next new line, we can define our variables' type and name. For this example, I will set the type of the variable as a string. So I need to type FString to set the variable's type as a string. If you want to use other types, here’s a list of the most common blueprint types and what you would need to type for them. Now we need to type a name for our variable; in my case, it will be custom setting variable 1, and we also need to put a semicolon after it. If we would compile this code right now, we wouldn't be able to access this variable in blueprints, so we need to make getter and setter functions that we can access in blueprints. Now in the header file, we need to type "public" and put a colon after it. Then, in a new line, we will type in all caps UFUNCTION; after it, in round brackets, we will type BlueprintCallable. And in the next line, we will define our setter function. Its type is void, cause it's not returning anything, and I will name it Set Custom Variable 1, and then in round brackets, I will define that this function takes one parameter that matches the same type as our previously defined variable; in my case, it is a string, and I will name it a New Value, and we end this line with a semicolon. With just a definition in the header file, our function will not do anything, so we need to write what it does in the CPP file. To make the function's definition in the CPP file, in Visual Studio, we can right-click the function's definition and use quick actions to create a definition of the function in the CPP file. Now we will see that Visual Studio opened a small window that shows us the definition of the newly generated function in the CPP file. We can edit that in this small window, or just close it and open the CPP file. If you want to define a function manually in the CPP file, you need to write the function's type, then the class name in which it's defined; you can see the class name in the header file.  Then follow that with 2 colons and then write the function's name, and then you finish that with round brackets in which you put in the function's parameters, just as in the header file definition. And after that, you open and close curly brackets in which all function logic will be written. In this function, we want to update our settings value with a new value. To do that, we need to write that our previously defined variable is equal to the function's parameter and end that with a semicolon. Now, in a similar manner, we need to make a getter function. First, we define it in the header file. This time the function type is a string because we want to return a string value and we don't need any parameters for it. We can also see that I have added a const at the end of the function and that instead of BlueprintCallable I used BlueprintPure. With the const keyword, we declare that this function will not modify anything in this class, including its variables. It would work completely fine without the const keyword next to it, but it's good programming practice to do so. Blueprint Pure means that we will get a pure function in Unreal Engine blueprints, and pure functions are generally used for getter functions. Now we can add the function's definition to the CPP file. We can see that the const keyword is also added to this declaration and that this function returns an empty Unreal Engine string object because this function's type is a string. We want this function to return the value of our setting variable, so we need to replace this empty string object with our previously defined variable; just make sure not to lose the semicolon at the end. Right now, our variable's default value isn't set; we can set that in the class constructor. Before declaring our constructor, we must change this line in the header file to include the UCLASS in the middle; otherwise, we will get errors when we try to compile this code later. Now we can declare the constructor in the CPP file. Just copy the constructor code from the video and change the class name fields to your class name. Now we can set the default value for our setting variable. Simply type the variable's name, the equals sign, the desired default value, and a semicolon. We can see that I typed TEXT, then brackets, and only then, in quotes, a string value. We are using TEXT because Unreal Engine says to always use the TEXT macro around string literals; otherwise, we can get undesirable string conversions, and in C++, all strings must be written within quotes. Also worth mentioning is that if you want to have a backslash, single quote, or double quote in your string, you need to put a backslash before them. Also, here are examples of how we can set values for other variable types. Now our settings variable has a default value, but there's still one issue. If we would compile this code right now, we wouldn't be able to get access to it in blueprints. The Get Game User Settings node returns the Game User Settings class, which is a parent class of our settings class. To get access to our class, we need to make our own function that will return our class in blueprints. We need to declare a static function. First, in the header file, we write UFUNCTION, BlueprintCallable, and then, in a new line, we write static, then our class name, an asterisk sign, and the name of our function, in round brackets, and end it with a semicolon. In the CPP file function definition, we need to write return, cast, then open angle brackets, our class name, close angle brackets, open round brackets, write the parent class name, 2 colons, get game user settings, open and close round brackets, then close round brackets, and put a semicolon at the end. This code will get the regular game user settings object and then cast it to our class type and return that. We could compile this code right now, but I mentioned earlier that we would look into two methods for adding variables. So for the second method in the header file, under the public section, we write UPROPERTY, open round brackets, then write config, comma, Blueprint Read Write, close round brackets, then in a new line, we write our variable type, its name, and end it with a semicolon. And that's it; we don't need to write any more functions for it. If we want, we can go to the class constructor and declare its default value just like we did for the previous variable. But why even bother with the first way of declaring variables if we can do it so much faster this way? Let's say you added a difficulty setting to your game, and you have several places where you can change the difficulty. And one day you decide that you want to add some achievements to the game. Let's say you want to add an achievement where whenever the player has changed the difficulty 69 times, you want to award him with a nice achievement. If you used a public variable for the setting, you now have several places in your project where you need to add some logic to track whether the setting has been updated. You don't have to worry about all the places where the setting is used if you use the getter and setter functions. You already have a place in the code that is called every time your setting variable gets updated, where you can easily add code for your achievement. It's a terrible example of why you should use setters or getters for setting variables, but I can't really explain other benefits without explaining other programming principles. Back to the code, now we are two steps away from having our new settings variables in our project blueprints. First, we compile our code. We open Solution Explorer, right-click our project that's located in the Games folder, and click Build. And now at the bottom output panel, we should see that our build has started, and it might take some time until it's done. If you did everything right, then you should see that the build succeeded. If your build failed the first time, make sure that your Unreal Engine editor is closed. If the build still fails, scroll through the error messages; they might show you where you made a syntax error that you can correct. The last step is to open the DefaultEngine.ini configuration file, which is located in the project's config folder. I opened it in Visual Studio, but you can open it in whatever text editor you like. Now in the file, scroll down to the /Script/Engine dot Engine section, and under it, write GameUserSettingsClassName, equals sign, /Script/ your project name, dot, and your newly created user setting class name. Just make sure to remove the U prefix from the class name, and don't forget to save the changes to the file. Now we can open our Unreal Engine project and look at our results. Now we should be able to get our new settings in blueprints by using this new function we created; in my case, it's named Get Better Game User Settings. We can see that we can get and set both of our custom setting variables. We also have access to all previous user settings. Also, you can still use the Get Game User Settings node. So if you used that before in your project, you don't need to replace it with our new setting node. Also, remember to use the save or apply settings nodes to save setting values in config files. You can find this tutorial in written format, link to it into the description.
Info
Channel: Continue Break
Views: 6,409
Rating: undefined out of 5
Keywords: unreal engine 5, unreal engine, unreal engine 5 tutorial, ue5 tutorial, unreal engine tutorial, unreal engine 4, unreal engine 5 beginner tutorial, unreal engine c++, save and load, custom settings, game development c++, unreal engine save settings, unreal engine load settins, easy unreal engine c++, save game settings, ue5 c++ tutorials, ue4 c++ tutorials, ue5 c++, unreal engine programming c++, game user settings, extend game user settings ue, game user custom settings
Id: 7rRUE2FKOXQ
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Wed Jan 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.