Skip to content

4 ‐ Classes and Blueprints

ashtongilbert56 edited this page Nov 4, 2023 · 14 revisions

A. C++ Parent Class

  • 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’.

Screenshot 2023-11-01 at 1 58 39 PM

  • Select a ‘Pawn’ type and hit next.

Screenshot 2023-11-01 at 1 59 05 PM

  • Name your class and create class. In this example, we'll call it 'Aircraft'.

Screenshot 2023-11-01 at 1 59 50 PM

  • This will start to add the new class’s code into the sim.

Screenshot 2023-11-01 at 2 00 21 PM

  • Visual Studio or Xcode may open your class’s C++ and header file.

Screenshot 2023-11-01 at 2 01 04 PM

  • 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.

Screenshot 2023-11-01 at 2 01 59 PM

  • 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.

Screenshot 2023-11-01 at 2 02 50 PM


B. C++ Child Class

  • 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.

Screenshot 2023-11-01 at 2 03 56 PM

  • Name your vehicle class and create.
  • We now have our vehicle class which is operating under our Aircraft (physics) parent class.

Screenshot 2023-11-01 at 2 04 34 PM

  • 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

Screenshot (17)

Screenshot (18)

  • 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).

C. Model Possession

  • After importing a model into your blueprint (see related 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’.

Screenshot 2023-11-01 at 2 08 25 PM

  • This is the blueprint our level uses to operate. First right click and search for ‘Get Player Controller’.

Screenshot 2023-11-01 at 2 08 55 PM

  • 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.

Screenshot 2023-11-01 at 2 09 33 PM

  • 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.

Screenshot 2023-11-01 at 2 10 02 PM

  • There is now an option to create a reference to our vehicle. Select that node.

Screenshot 2023-11-01 at 2 10 27 PM

  • 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.

Screenshot 2023-11-01 at 2 10 58 PM

  • Remember to always Compile your blueprints after making changes. If the Compile button is yellow, recompile to make it green.

Screenshot 2023-11-01 at 2 11 43 PM

  • 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.

Screenshot 2023-11-01 at 2 12 09 PM


D. Calling C++ Functions

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.

Screenshot 2023-11-01 at 2 14 51 PM

  • 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;
}

Screenshot 2023-11-01 at 2 15 37 PM

  • 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.

Screenshot 2023-11-01 at 2 18 07 PM

Screenshot 2023-11-01 at 2 18 24 PM

Screenshot 2023-11-01 at 2 18 38 PM

  • In Xcode, rebuild by selecting Product -> Build.

Screenshot 2023-11-02 at 11 40 52 AM

  • Once the rebuild is complete, you can open your Unreal sim and open your vehicle blueprint. You can do this by right-clicking on your vehicle pawn (F-16) on the right-hand side of the main screen, and selecting "Edit".

Screenshot 2023-11-03 at 10 00 17 AM

  • 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.

Screenshot 2023-11-01 at 2 19 52 PM

  • 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.

Screenshot 2023-11-01 at 2 20 21 PM

  • 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.

Screenshot 2023-11-01 at 2 20 57 PM

  • We can modify how our strings are printed by expanding the print node, where we can change the color and duration.

Screenshot 2023-11-01 at 2 21 33 PM

  • 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:

Screenshot 2023-11-01 at 2 22 41 PM


E. Example Tick Function

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;
}

Screenshot 2023-11-01 at 2 24 36 PM

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, y 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.

Screenshot 2023-11-01 at 2 25 38 PM

We will compile the blueprint and run the sim. Seeing that our vehicle moves increasingly by A with every time step.

Screenshot 2023-11-01 at 2 26 02 PM

Clone this wiki locally