Getting started with Unreal Engine 4: Create your first c++ project and link code to blueprints

In this tutorial we’ll teach you how to create your first Unreal Engine 4 c++ (or cpp) project, set it up, and create your first c++ class. The goal is to show you how you can make c++ code communicate with blueprints, and how to make blueprints communicate with c++. We’ll do it by creating a first person shooter where you fire bullets at dummy targets which will then let you know they’ve been hit and fall down.

To begin with, you’ll need the freshest installment of Unreal Engine 4. To get it, you have to go to, hit download button, and select your platform.

After you’ve downloaded Epic Games Launcher and installed it, you need to get the latest version of the engine. In the launcher, there is a library tab which lists all your current projects and engine versions.

Since you don’t have any, you should select one by clicking on Add Versions button. Alternatively, launcher will recognize you don’t have one so it’ll offer you latest stable version. After it has completed downloading and installing, you can run Unreal Engine 4 and create a project.

Creating the project

In the new project window, go to C++ tab and select First Person template. You get an option to choose between blueprints and c++. If you haven’t had a chance to work with these before, blueprints are sort of a visual coding language that we’ll touch upon later in this tutorial. Project in c++ is a code driven game where you implement your game logic in Visual Studio or some other code/text editor.

If you’re an experienced developer, you might think that Blueprints are inferior to code as they are limited to only what you have access to through them. The truth is that in most cases they cover all the functionality you might need, and when this isn’t the case, you can always extend blueprint functionality with c++ and use them anyway. Also, if you’re making a purely Blueprint based game, there’s a big chance you might run across some sort of code or algorithm that’s simply easier to implement using c++. These are the reasons why we always choose to go with a combination of the two. If you’re still not convinced, there’s a lengthy thread on this topic where people list ups and downs of using blueprints, with main focus being on performance.

To clarify, templates are starting points of your project, and for different projects you can start with most suiting template. For example, if you were to start working on a top-down RPG game, you should select a top-down template. When making  shooters, use first and third person template, and so on. In this example we’ll use first person template and we’ll start from there. Name your project however you want and click Create Project.

Once your project is created and compiled your Unreal Engine 4 Editor and Visual Studio with your project’s solution will start up. If you haven’t used Unreal Engine’s editor before, here’s a few good starting guides.

Creating Classes

While in the editor, you need to go to the content browser tab which is in the lower left part and expand folder. Click on the C++ Classes and go to the folder named as your project. There you’ll see a few classes already generated – Character, GameMode, DemoHUD, and Projectile. I wouldn’t go into details on how these classes work, but they’re what makes First Person demo template function the way it does. You can go through them and explore, but for the sake of this example we’ll focus on creating new class. Right click somewhere on the empty space in the right part and select Add C++ class. We’ll make this class inherit Actor, which is a cellular base class for Unreal Engine 4. This means that it has its world transform including position, rotation and scale, and a root which you can add components to. To do so, click on Actor in the Choose Parent Class window and click Next. Name this class MyDummyTarget, and click Create Class.

This will generate MyDummyTarget c++ class and open up Visual Studio. It will generate code which will be used as a base for this class and it includes:

  • Constructor ( AMyDummyTarget() ) which is called when the object is created in the editor.
  • A begin play function ( BeginPlay() ) which is called when the game has started playing or when the object is spawned through some other script, but after Constructor has finished executing.
  • Tick function ( Tick(float DeltaTime) ) which will be called every frame while the game is running. Note that the DeltaTime is the time which has passed between two frames.

In order to have this newly created class visible in the editor, we need to create a Blueprint class which inherits from it and extends its functionality. Think of it as an entity which you can place in the world and move it around or do whatever you want with it. To create it, you need to go back to the editor, and in content browser click on Content folder, and right click in the right part. From there, select Blueprint Class.

As a parent of this class you need to select your newly created c++ class called MyDummyTarget and you can do so by navigating down to All Classes and type in “my”. You should be able to see it. Click on your class and then Select button.

This will open up blueprint editor from which you can implement functionality using Unreal Engine 4’s blueprint system. Navigate to Event graph.

Blueprint Editor

In the event graph you can see three events – Tick, BeginPlay and ActorBeginOverlap. First two behave exactly as explained above, while the third one executes when another actor has started overlapping with this class’ Actor. What we want to do is generate a new event and link some behavior to it. Right click somewhere in the grid on empty space and type in “Event Hit”. Select the first item in the list and drag an execution node to the right. When you release the node you’ll be prompted to select the next action and you can type in “Print”. This will allow us to to make actor display message we want on the screen when it is being hit by some other actor.

However, to make this work, we need to have a collision on our actor and a 3D model for it to interact with the world. To do so, go to viewport tab. On the top left, click the green “Add Component” button and type in Static Mesh. This will create a new StaticMesh component. In the right part of the screen you’ll notice Details panel and in the Static Mesh section you’ll notice it says None. Set Static Mesh to Shape_NarrowCapsule by clicking on None and typing in Capsule. This will create a white capsule that has collision enabled and that we can hit.

Now click compile and save. Navigate back to the editor and in the content browser you’ll see your blueprint class. Drag and drop that class into the viewport of the game and you’ll notice that it’ll be displayed there.

Now, you can hit Play button, which is above the editor and you’ll enter the game in first person mode. You can use WASD keys and mouse to move and look around, and left click to shoot. You’ll notice that, if you’ve followed steps correctly, a message saying “Hello!” will appear in the top left corner when you hit your dummy object with a ball.

Starting With C++

What we want to do next is demonstrate how blueprints and c++ can communicate between each other. To do so, we will create functions that we’ll call from c++ in our custom class, and a custom event that we can call from c++ back to blueprints. To do so, we’ll need two new functions in our c++ class. Lets call these functions TakeDamage() and Died(). Now, if you’ve done any c++ development before, you might think that defining these might be enough, but to actually see these functions and events in blueprints, we need to expose them to the editor. To do so, in our MyDummyTarget.h file, we need to add:
UFUNCTION(BlueprintCallable, Category = “MyPawn”)
UFUNCTION(BlueprintImplementableEvent, Category = “MyPawn”)
above them. What this does is that it tells Unreal that function called TakeDamage is defined in c++ and that the execution will move from blueprints to c++, and that Died() is a blueprint event that will take execution from c++ to blueprints.

What we also need to do is define what those two functions do. To be more precise we only need to define what TakeDamage() does as Died() will clearly be implemented in blueprints. So lets say that when our actor takes damage, it will instantly die by calling Died() function. To do so, in our .cpp file, we define TakeDamage() function with it only calling Died() method.

Save both files and return to the editor. You might notice that we’ve also defined a category for these two methods called “MyPawn”. This will help us find those functions in blueprints because when you drag a white line in blueprint editor and start typing while searching for functions and events, both of these will be listed under “MyPawn” category.

Now let’s change our blueprint so that it calls our newly created “TakeDamage” function when the actor is hit by another actor. This will call the c++ function which will then report back to blueprints calling our “Died” blueprint event. To do so, delete “Print String” node from blueprints, drag white line from EventHit and search for TakeDamage. We now need a new event for Died, and to get it, right click somewhere in the empty space and type “event died”. Our custom event will show up and you can select it. Drag out white line from it and type “print” and you’ll see print string again.

To make things a bit more interesting, lets make our actor fall down when hit or react to physics. In order to do so, drag out another white line from the print string node and type in “set simulate physics”. This function takes in two arguments – Target and Simulate. Tick simulate to true and make sure your “Static Mesh” component is connected to the Target node. If it’s not, just simply drag and drop it from the left menu onto the node.

Now hit compile button, then save, and go back to the editor. When you hit play you’ll notice that when you try shooting your actor for a few times, it will say that it’s hit and fall down. If the actor doesn’t fall down at once, that’s because the physics of it were frozen when it was hit and it couldn’t simulate it in time. To fix this, simply go to your viewport editor of the blueprint class, select static mesh component and move it up a bit.


So that’s it! Now you know how to create your first c++ project, combine blueprints with c++ and make them communicate both ways. Where do you go from here? Well just try searching through endless tutorials on c++ or blueprints in Unreal Engine 4 and you’ll be on your way in no time.

Good luck on your gamedev journey!

Facebook Comments