Modifying the Hello Sphere Blueprint

Double-click on the HelloUnrealBP to open it. Now, you should be able to see our spherical friend front and center within the Viewport. You will also notice that the components we added to our Hello Sphere can be found in the Components panel and as variables in the My Blueprint panel. This is so that we can get references to our components within our Blueprint Graphs.

Our goal is to have it, so we can approach our Hello sphere and the 3D text will change from Hello World to Hello Player. To do this, we are going to need to add another component, work with the Blueprints Event Graph, and utilize an Event.

The first thing we need to do is provide a volume we can use to detect other overlapping actors. To do this, we need to add a SphereCollision component via the Add Component button in the top-left corner of the Blueprint window. Do this now, and name it SphereCollision. With the new component selected in the Components panel, you should be able to see the collision sphere visualized within the viewport. Scale the sphere volume via the in viewport widget (like we did in Chapter one), so it is approximately 3.5x bigger than our sphere mesh. You should be presented with something that looks similar to this:

Modifying the Hello Sphere Blueprint

The next thing we need to do is ensure that this component will generate overlap events. With the sphere collision component selected address, the Details Panel on the right-hand side of the Blueprint window. Under the Collision section, you will see a series of options, one being a checkbox titled Generate Overlap Events, with this checkbox ticked, this Blueprint will fire the ActorBeginOverlap event within the event graph when it overlaps another object also signaled to generate either hit or overlap events. It is important to note that both offending bodies must be set to generate these events or nothing will occur.

Working with Blueprint graphs

Now, we need to modify the Blueprints Event Graph. This will be your first attempt at creating custom functionality with Blueprints. Open the Event Graph either by double-clicking on the Event Graph element under the Graphs section of the My Blueprint panel, or selecting the Event Graph tab above the Viewport panel. You should see a graph with three slightly translucent Event Nodes underneath comment bubbles. These are currently disabled events. They have been presented to you by default as they are the most commonly used events; however, until functionality has been appended to these nodes, they will not be triggered.

You should see a node titled Event ActorBeginOverlap as follows:

Working with Blueprint graphs

This is the event that will be triggered when other objects overlap our SphereCollision component. To enable this node, we need to append functionality to the node. We can do this by left-clicking on the hollow white arrow on the right-hand side of the node and dragging off into the grid, then releasing the left mouse button. Upon releasing the left mouse button you will be presented with a list of available functions, events, and logical nodes. This list is your access point into the Blueprint node library as well as any custom functions, events, or Macros you have created within the Blueprint itself. Functions in blueprint appear in the form of Nodes, which are a visual representation of the function within our graph.

This list will be context sensitive by default, meaning that you will only see options that are concerned with the Critical Execution Path and are related to the Blueprint. This path is the order in which your nodes will execute and it can be denoted by a white line that runs between nodes connected via white arrows. The question is what function do we need to call to change our text? What we need to do is, set the text parameter on our TextRender component. As our list is context sensitive and we have a TextRender component included in our blueprint, we can assume that we will be able to find the function we desire. Given that, simplistically, we wish to Set Text let's try searching for that. By typing or clicking in the search box at the top of the list, we will be able to perform a keyword search for our function.

By typing Set Text, we are presented with the following options:

Working with Blueprint graphs

As you can see, the context sensitive search has provided us with all of the functions that we can call that either partially include the phrase Set Text or are related to the TextRender component. You will also notice that all of the function calls also have (Hello World) following the function name, this means that if you select that function it will already be set up to affect our HelloWorld TextRender component that we included in the Blueprint. Select this option now. You will see something similar to this:

Working with Blueprint graphs

Compiling Blueprints

Congratulations! You have created your first Blueprint function! As you can see, two of our nodes are linked via the white line. This means that the Set Text function will be hit immediately after the event ActorBeginOverlap has been triggered. You will notice that the event has one pin titled Other Actor on the right hand side of the node, whereas our Set Text function node has two pins titled target and value on the left-hand side of the node. These are the node's inputs and outputs. Inputs act in exactly the same way as function parameters do in C++ and are found on the left-hand side of the node. This means that those pins are for data that you wish to pass into a function or node, whereas pins on the right are the resultant output of a function or node and are similar to output parameters or a return type in C++.

As you can see, the target pin has already been populated for us. Our Context Sensitive node search has provided us with a Set Text function that has already set our TextRender component to be the target. The small node titled hello world is our visual representation of the reference we have to the component. This reference can be found as a variable under the MyBlueprint panel. Select this small node and press the Delete key. You will notice that the Target pin is now empty, and in its place, the field has been populated with the word self. This means that when no input is specified for this pin, it will default to use a self-reference, that is, a reference to the blueprint you are working from.

On trying and compiling the blueprint by clicking on the Compile button in the Toolbar panel, you will see that there are two errors preventing successful compilation in the Compiler Results panel, which can be found at the bottom of the Blueprint window:

Compiling Blueprints

Each time you compile a blueprint, you will receive a verbose report of any errors or warnings within your blueprint. You may double-click on these report lines to take you to the area of the Blueprint where the error occurs. The first error states that the default parameter of the Target input pin self, meaning a reference to the blueprint we are working from, is not of a compatible type with the function input pin. This makes sense as the Blueprint we are working from is not of type TextRender component. The second states that the value of the Value pin is invalid. This is because we have not specified any input value for this pin. The final line is a summary of the compilation, including compilation time in milliseconds.

Using Blueprint variables

The first thing we need to do is replace the HelloWorld TextRender component reference we deleted. To do this, we can click and drag the variable from the MyBlueprint panel into the graph, then select Get from the drop-down menu. You can then click and drag the pin on the right-hand side of the reference node and connect it with the Target input pin on the Set Text function node. While you have the HelloWorld reference pin dragged, you can see a small box underneath the marker. This will inform you of the action that will take place upon releasing the pin.

Tip

When clicking and dragging variables from the MyBlueprint panel, you can hold Ctrl to spawn a get node or Alt to spawn a set node.

The next thing we need to do is populate our Value pin with the text value that we want to appear when a player moves within our Sphere Collision Volume. To do this, we need to summon our function search widget, you can do this by right-clicking on blank space within the graph. We need to search for a Make Literal function; these functions are utility functions that allow the creation of temporary variables during Blueprint execution. Specifically, we require a MakeLiteralText node. Select this option from the search window and type Hello Player in the Value box on the left-hand side. Then, connect the Return Value pin to the Value pin on the Set Text Node. Now, Compile! No errors or warnings should present and the compilation will be successful.

Now, we can run our project and see the fruits of our labor! After pressing the Play button in the main editor window we will possess a flying first person avatar, fly close to our hello sphere and you will see the text change!

Using Blueprint variables

Utilizing the Blueprint palette

We are nearly done with our Hello World Unreal project. There are still a few basic facets of Blueprint usage that have yet to be covered. We are going to extend the functionality of our HelloSphere so that it can say hello directly to the object that has entered its bounds. To do this, we will need to get the name of the object that has overlapped with the HelloSphere, prefix that name with Hello and convert the resultant string to a format the Value input pin will comply with. We should also change the text back to hello world when the object leaves the bounds of our sphere.

We will be finding our desired blueprint functionality in a different way than before: we will use the Palette Panel. To bring up the Palette panel, select Window | Palette from the drop-down menus located at the top of the screen underneath the tab banner. This will bring up the Palette panel on the right-hand side of the Blueprint window. This panel acts as a search directory for Blueprint library nodes. Nodes found in this panel will not be context sensitive, meaning you will have to populate all input pins yourself. At the top of the Palette panel, you can see any nodes that you have specified as a favorites. The bottom half is the search directory titled Find a Node. Here, you can specify a class you wish to search for within for the node, and a search bar where you can enter the keywords for your node search.

The first thing we need to do is ensure that our text will reset upon leaving the bounds of our sphere collision volume. To do so, we will need to utilize another event, similar to the ActorBeginOverlap there is an ActorEndOverlap node. We can find this by searching for it in our Palette or by right-clicking on graph. If you used the Palette to search for the function to create the node all you need to do is select the desired function from the list and click and drag it into the graph.

With our ActorEndOverlap node now in the graph, all we have to do is replicate the previous functionality we created for our ActorBeginOverlap event yet instead of using Hello Player as our literal value we can use Hello World!. You can copy paste blueprint nodes if you wish. All lines and associations will be preserved. I would advise you to keep copy pasting of nodes to a minimum. If you are finding yourself copy pasting functionality quite frequently, it is likely you would be able to wrap the functionality into a blueprint function.

Blueprint meta-data and string manipulation

Now, to create the functionality to print the name of the object that has overlapped our bounds, we are going to need to retrieve two more function nodes and adapt our ActorBeginOverlap functionality. Retrieve GetObjectName and Append now. We will use these two nodes to complete our functionality. As you can see, the GetObjectName node takes in an object reference (remember our Unreal Object Hierarchy), which is the base type of all Unreal Objects. This input pin is used to specify which object we wish to get the name from. This kind of information is known as meta-data and is usually present in large polymorphic inheritance chains.

We are going to drag the Other Actor pin from our ActorBeginOverlap node to this input pin. This will retrieve the name from our overlapping actor as a string. It is important to note that our collision functions ActorBeginOverlap and ActorEndOverlap all expect the colliding object to be of type actor, that is, because UActor is the base class from which all objects that can move or be physically present in a scene should inherit from and UActor inherits from UObject allowing us to make the conversion.

Now that we have our offending actor's name, we need to prefix the resultant string with Hello via our Append node. By default, our append node will have two input pins, A and B, in short the resultant string will be whatever value is plugged into A plus whatever value is plugged into B. For our purposes, we wish to plug our offending actor's name into B and leave A unconnected, but type Hello in the field box provided this will result in Hello B. Ensure Hello is followed by a space.

On the right-hand side of our Append node, we have an output pin for the resultant string. There is something else on this node; however, there is an Add Pin + button. This is because our Append node has an expandable number of input pins. By clicking this button, we will add a C input pin; do this now. We now have an extra input pin on the left-hand side of the node, the resultant string will now be A+B+C. Fill the provided text field for C with ! to add some extra enthusiasm.

We are now going to plug the resultant string from this append node into the input pin provided by the Make Literal Text node. You will notice that string and text are two different types. With the string output pin, click and drag the mouse over the input pin for the MakeLiteralText node. You will see that the little box by the mouse that shows what will happened upon releasing the pin is not a green tick but a box stating Convert String to Text.

This means that upon releasing the left mouse button the Blueprint will create a conversion node that takes in a string type and outputs a text type. This is very similar to conversion in C++. However, conversions between types in Blueprint could be concealing more complicated functionality as some types may require additional steps to convert between. With this step complete, you should see something similar to this:

Blueprint meta-data and string manipulation

The node arrangement for the ActorEndOverlap event will appear as follows:

Blueprint meta-data and string manipulation

Now, run your project and encroach on our spherical friend you will see the text change to something along the lines of Hello DefaultPawn_1! Then upon leaving the spheres bounds, it will change back to Hello World! Well done, you have completed your first Blueprint! Now that we have created a hello world project and a hello world blueprint, I think it is time we sank our teeth into your first unreal game project Barrel Hopper! We will be expanding our Blueprint skillset and learning some new editor tricks. We can finally close our hello world project!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.190.58