-
Notifications
You must be signed in to change notification settings - Fork 0
4 ‐ Classes and Blueprints
-
The parent C++ class is where we will eventually add our physics engine code. However, for now we will simply create a blank class where the code can later be incorporated.
-
Click the Tools tab at the top of the screen and select ‘New C++ class’.
- Select a ‘Pawn’ type and hit next.
- Name your class and create class. In this example, we'll call it 'Aircraft'.
- This will start to add the new class’s code into the sim.
- Visual Studio or Xcode may open your class’s C++ and header file.
-
After Unreal is finished adding the code, a message box may appear in Visual Studio saying the project has been modified. Hit the ‘Reload all’ option.
-
Then close your sim/engine if it is open, and select the green play button at the top of Visual Studio Start Debugger.
- This will compile your project with your new class and link the sim to it. This can take some time to finish.
- Once finished, the sim/engine will most likely restart and open itself automatically.
- You now have your parent class created.
- To verify your parent class has been successfully created, select ‘New Empty Blueprint Class’ in the blueprint menu and search “All Classes” for the name of your parent class you created.
-
We will now create our child Vehicle blueprint.
-
Select ‘New Empty Blueprint Class’ in the blueprint menu, and search “All Classes” for the name of your parent class you created and select.
- Name your vehicle class and create.
- We now have our vehicle class which is operating under our Aircraft (physics) parent class.
- We can now add this vehicle blueprint to our level by going to our level and opening the Content Browser with Ctrl + Spacebar. Navigate to the Blueprints folder and drag your created blueprint into the level. You'll now see your blueprint asset listed in the left-hand window
- You can adjust your blueprint vehicle location using the Transform values on the details window when you select your blueprint to adjust where your model starts (this is temporary as we will define our initial location as an input in our code).
- Now create a vehicle model and camera view in your vehicle blueprint before proceeding with the following sections (Complete the 5- 3D Models section now).
-
After creating a vehicle model and camera view in our blueprint (see 5- 3D Models section), we must tell the sim that this is our vehicle we want to control.
-
To do that, navigate to the blueprints tab at the top -left and select ‘Open Level Blueprint’.
- This is the blueprint our level uses to operate. First right click and search for ‘Get Player Controller’.
- Then, drag off of the ‘Return Value’ point on the ‘Get Player Controller’ node. This will open the node search bar like before, but by opening it by dragging off the node, the box only shows options that make sense for this context. We will search for ‘Possess’ and select.
- We’ll now go back to our level and make sure we select our vehicle. Then we can go back to the level blueprint and right click.
- There is now an option to create a reference to our vehicle. Select that node.
- Now connect this to the ‘In Pawn’ port on the ‘Possess’ node. Now connect the ‘Begin Play’ event flow to the ‘Possess’ node. We now have a blueprint to tell the level to possess our vehicle for control. The finished blueprint is displayed below.
- Remember to always Compile your blueprints after making changes. If the Compile button is yellow, recompile to make it green. Saving the blueprint is also a good idea but not required to observe the changes when the sim is ran.
- Now when we run our sim, we should possess the model with our view defaulting to the camera view we set up in our vehicle blueprint.
We now have a parent and child blueprint class. The child vehicle blueprint can call the functions inside the parent class. To create a function that can be called in blueprints, first open your project’s parent class .cpp and .h file in VS studio or Xcode.
We first need to declare our function in our header file (.h). I’ve created an example function called, ‘Ex_function’. Make sure to add both lines 22 and 23 in your code under the ‘Protected’ category. The ‘Category’ designation on line 22 is the category Unreal will organize your function under in Blueprints, more on that later. Next, define your function, with your parameters defined as the example does. Here, x0, y0, and z0 are the inputs, and x1, y1, and z1 are the outputs for the example function, and the & symbol is the address operator in C++.
UFUNCTION(BlueprintCallable, Category = "FlightSim")
void Ex_function(double& x1, double& y1, double& z1, float x0, float y0, float z0);
- Notice that we have defined a category called "FlightSim". This will be useful within our UE project for finding this function.
- Now open your C++ file (.cpp), and add your function and arguments. In this example, we will change the x, y, and z values by adding 20 to each every time the function is called.
void AAircraft::Ex_function(double& x1, double& y1, double& z1, float x0, float y0, float z0)
{
x1 = x0 + 20;
y1 = y0 + 20;
z1 = z0 + 20;
}
- After writing out our function, make sure to close your sim, if open, and save your C++ and header file. Then rebuild your project. This should take a decent while to finish, if the rebuild finishes within a few seconds, it did not rebuild correctly, so just rebuild again.
In Visual Studio, rebuild by right clicking the name of your sim on the right-hand window and select ‘Rebuild’. The terminal window at the bottom will show the message shown when complete.
- In Xcode, rebuild by selecting Product -> Build.
- Once the rebuild is complete, you can open your Unreal sim and open your vehicle blueprint. You can do this by clicking the "Edit F16" link to the right of the blueprint asset.
- Right click on the blueprint background and either search for your function name or the ‘Flight sim’ category that we defined in the C++ code.
- Select your function and it will create a node with your specified inputs and outputs. We can test our function by connecting some ‘Print String’ nodes to our outputs.
- Since our outputs are floats and not strings, when we try to connect the outputs to the print node it will automatically add a converting node. Connect the flow lines to the ‘Begin Play’ event node. The finished blueprint is shown below. We can test our function by specifying different inputs on the node itself as shown.
- We can modify how our strings are printed by expanding the print node, where we can change the color and duration.
-
Remember to compile your blueprint, then run your sim. You’ll see your printed outputs at the top left of the screen.
-
Remember you must rebuild your Visual Studio or Xcode project every time you alter your C++ or header file.
-
Running your project should now look something like this:
Let’s create a function that moves the vehicle forward incrementally by some distance every time tick. Unreal Engine defaults to centimeters for its units. We will create a function that is called every time tick and modifies the location of the vehicle.
Let’s open our c++ file to implement our function. We will create a function that is called ‘move_forward’ with the input ‘A’, which will be our amplitude, along with the input ‘loc’ which is the current location of our vehicle. The function will have the output ‘delta’ which is our changed location. The function is shown below along with the associated header file.
Header file code:
UFUNCTION(BlueprintCallable, Category = "FlightSim")
void move_forward(float& delta, float loc, float A);
CPP code:
void AAircraft::move_forward(float& delta, float loc, float A)
{
delta = A + loc;
}
Rebuild the project and open the vehicle blueprint. We will insert our function node along with the ‘Get Actor Location’ and ‘Set Actor Location’ nodes. These nodes utilize vector variable types, therefore, to modify a single value of the vector, we must break it up. So, search for the ‘Break Vector 3f’ node and connect to the ‘Get Actor Location’ node.
Connect the vector element you want to modify to your function; this depends on how your vehicle is oriented. In my coordinate system in the sim, x is the forward direction and will be the one I will modify.
After modifying the vector element, we must combine it back into the vector. So, search for ‘Make Vector’. Combine the elements together and connect to the ‘Set Actor Location’ node. We will define our amplitude A on the function node itself and connect the blueprint as shown.
We will compile the blueprint and run the sim. Seeing that our vehicle moves increasingly by A with every time step.