2. Introduction to Blueprints

2021/01 02 20:01

UE4 Blueprints is a visual gameplay scripting language based on a node graph in which you connect nodes from left to right. It can create full-fledged games or simple/complex game mechanics, such as those that open the door to level-wide puzzles. The biggest advantage of Blueprints is that you don’t need a programmer to create the logic. Artists can easily make anything they want inside Blueprints and share it with a programmer.

This system is extremely powerful because it offers the artist a full range of tools that are generally only available to programmers. On top of that, C++ programmers can create baseline systems that can be accessed or modified by Blueprint users.

Nodes

In Blueprints (or any other visual programming language), a node is a self-contained functionality that does something unique. In Unreal Engine, Blueprint nodes are objects that can be anything—events, flow control operations, functions, variables, and so on. The source code for the node can be big or small. In Unreal Engine 4, a developer can use C++ to make a Blueprint node, which is accessed in the Graph Editor panel.Figure 2-1 diagrams the logic done in a node editor to open a door that requires a key.

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig1_HTML.jpg
Figure 2-1An example of nodes

Blueprint Types

There are multiple types of Blueprints, which you need to understand to use them efficiently. All Blueprints types (except Level) are created in the Content Browser.

BLUEPRINT CLASSES

The Blueprint class is the most common type used in your game because it self-contains gameplay mechanics, and it is easily reused across multiple levels. Blueprint classes are inherited from native C++ classes and can have their functionality.

You can create your own C++ classes and mark them as Blueprintable to create Blueprint classes. They can interact with each other to create interesting gameplay mechanics; for example, a light Blueprint and a switch Blueprint that communicate to toggle on or off a light. You could also make the switch interact with multiple lights to toggle them on/off randomly or sequentially.To create a Blueprint class, right-click the Content Browser, and under Create Basic Asset, select Blueprint Class, as shown in Figure 2-2.

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig2_HTML.jpg
Figure 2-2Select Blueprint class

Figure 2-3 shows an example graph with an event that, when triggered, lists all the actors present in the world (the Get All Actors Of Class node), goes through each of them (the For Each Loop node) individually, and calls the toggle light function for each of them.

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig3_HTML.jpg
Figure 2-3Example Blueprint class with a custom event

LEVEL BLUEPRINT

A Level Blueprint cannot be created manually but is included within the level itself. It is available when the level is loaded. You can reference any asset in the world and interact with it. If you reference a Blueprint class inside a Level Blueprint, you can access all the public variables and functions in that class.Note

A public variable or function is accessed from outside the class.

The advantage of using a Level Blueprint is that it is easier to access the actors on the level because you can directly reference them without casting. This is useful when creating events or functions that should be isolated to the level. An example is triggering a cinematic when a certain condition is met.Figure 2-4 shows how to open a Level Blueprint.

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig4_HTML.jpg
Figure 2-4Access Level Blueprint from Editor toolbar

In a Level Blueprint, you can reference any asset by selecting it and right-clicking (see Figure 2-5).

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig5_HTML.jpg
Figure 2-5Referencing an asset

BLUEPRINT INTERFACE

A Blueprint Interface is a special type of Blueprint in which you can only create functions with their input and output parameters and variables. Let’s quickly go over these terms.

  • Functions are graphs with a single entry pin and a single output pin. Inside it, you can connect any number of nodes that make up your logic so that when the function is called, it starts from the entry pin, activates all the connected nodes, and exits through the output pin. Interfaces cannot contain any function implementation, which means you can only create a function without any logic (graphs are read-only), and variables cannot be created.
  • Variables are nodes that hold a value or a reference to an object or actor in the world.

Interfaces can be added to multiple other Blueprints, and they are guaranteed to contain the functions created in the interface, which can then be implemented. This allows multiple Blueprints to share one common interface; for example, imagine you have two completely different Blueprints, like tree and ice. You can have an interface that contains a damage function and implement this interface in tree and ice. Inside the tree and ice Blueprints, you can implement the damage function that makes the tree burn and the ice melt. If there is no interface, then you must convert (a.k.a cast) the hit actor to each type of actor and call the damage function.Figure 2-6 is an example diagram that shows the differences between having an interface and not having an interface.

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig6_HTML.jpg
Figure 2-6Comparing interface vs. no interface

To create a Blueprint Interface, right-click the Content Browser, and under Create Advanced Asset, select Blueprint Interface from the Blueprints subsection (see Figure 2-7).

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig7_HTML.jpg
Figure 2-7Creating a Blueprint interface

Figure 2-8 shows the interface after it is created.

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig8_HTML.jpg
Figure 2-8Example Blueprint interface

BLUEPRINT MACRO LIBRARY

The Blueprint Macro Library is a Blueprint container that consists of a collection of graphs placed as nodes in other Blueprints. You cannot compile a graph in a macro library because it is a container. Any changes to a macro graph are reflected only when the Blueprint containing that macro is recompiled (see Figures 2-9 and 2-10).

../images/496849_1_En_2_Chapter/496849_1_En_2_Fig9_HTML.jpg
Figure 2-9An example macro that gets a random item from an array, which is a list or collection of elements
../images/496849_1_En_2_Chapter/496849_1_En_2_Fig10_HTML.jpg
Figure 2-10An example of how to use the macro in a Blueprint

Chapter 3 explains how to combine C++ and Blueprints. You can create your own classes and Blueprint-based classes. In addition, you can declare variables and functions in C++ that you can access in Blueprint.