Programming Quick Start

The primary goal of this quick start guide is to introduce you to Unreal Engine 4's (UE4) development environment. By the end of this guide, you'll know how to set up and develop C++ Projects in UE4. This guide shows you how to create a new Unreal Engine project, add a new C++ class to it, compile the project, and add an instance of a new class to your level. By the time you reach the end of this guide, you'll be able to see your programmed Actor floating above a table in the level.

1. Required Project Setup

You should have a copy of Visual Studio 2015 for Desktop (Preferably Community or Professional Editions) installed before starting this tutorial. For setup instructions, please see Setting Up Visual Studio for UE4.

You should have a copy of Xcode 9 installed before starting this tutorial.

  1. Open Unreal Engine from the Launcher. The Project Browser will appear.

  2. Click on the New Project tab and then select the C++ tab. From there, select Basic Code so we have a clean starting point, and make sure With Starter Content is set. We'll need to enter a project name, so we'll use "QuickStart". We can now click Create Project and get started.

The Unreal Editor will now open our new project. Visual Studio will also open and load the solution file that our project has created.

2. Create a C++ Class

  1. In the Unreal Editor, we can create a new C++ class with the New C++ Class... command, located in the File drop-down menu.

  2. The Choose Parent Class menu will open. Since Actor is the most basic class that can exist in an Unreal Engine level, we'll use the Actor class as our base.

  3. The Name Your New Actor menu will open. For this example, let's enter the name "FloatingActor", and then click Create Class.

Now that we have created a C++ class, we can switch to Visual Studio to program it. FloatingActor.cpp will be opened for us automatically, and Unreal Engine will automatically compile and reload the code with our new class.

3. Write and Compile C++ Code

In Visual Studio, we'll use the Solution Explorer pane to find our newly-created C++ files. In our example, they will be named FloatingActor.cpp and FloatingActor.h and will be inside the QuickStart project. 

We're about to write code. All code used in this tutorial will be found at bottom of the page in its current state as of completing that page's instructions. 

  1. In Xcode, we'll find our newly-created C++ files. In our example, they will be named FloatingActor.cpp and FloatingActor.h and will be inside the QuickStart project.
  1. In Visual Studio, we'll use the Solution Explorer pane to find our newly-created C++ files. In our example, they will be named FloatingActor.cpp and FloatingActor.h and will be inside the QuickStart project.
    We're about to write code. All code used in this tutorial will be found at bottom of the page in its current state as of completing that page's instructions.
    In Xcode, we'll find our newly-created C++  files. In our example, they will be named FloatingActor.cpp and FloatingActor.h and will be inside the QuickStart project.

    We're about to write code. All code used in this tutorial will be found at bottom of the page in its current state as of completing that page's instructions.
  1. In FloatingActor.h, we'll add the following code just before the closing brace and semicolon at the end of the file:
    float RunningTime;
    //fload runningJustintime=0.0;
    //here is the source
    	
  2. Switching to FloatingActor.cpp, we'll add the following code just before the closing brace at the bottom of AFloatingActor::Tick:
    FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 20.0f;       //Scale our height by a factor of 20
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);
    	
    The code we've just written will cause FloatingActors to bob up and down smoothly, using the RunningTime variable we created to keep track of our movement over time.
  • Now that we're done coding, we can compile by right-clicking our project in the Solution Browser and selecting the Build command, or by pressing the Compile button in the Unreal Editor. Once the compile succeeds, Unreal Editor will automatically load our changes.

    Compiling from Visual Studio.

    Compiling from the Unreal Editor.

When building in Xcode, you are only compiling the game project, not the Editor.

  • Now that we're done coding, we can compile our project using XCode's Debug Configuration by clicking the Product > Build option. We can also compile using XCode's Development Configuration by clicking the Product > Build For > Profiling option, or by pressing the Compile button in the Unreal Editor. Once the compile succeeds, Unreal Editor will automatically load our changes.

Location of the build command within XCode.

Compiling in XCode, using the currently-selected Configuration.

Compiling using XCode's Development Configuration.

Compiling from the Unreal Editor.

When running the binary Editor, it is important to add the -game flag if you rebuilt your project in any Uncooked configuration, and the -debug flag if you rebuilt your project in any Debug configuration.

We're now ready to create objects in Unreal Editor based on our code. For reference, all code used on this page is included below.

4. Test Your Code

  1. In the Unreal Editor, locate the Content Browser, and expand the folder called "C++ Classes". Within that folder, there is a "QuickStart" folder that contains our Actor class, FloatingActor.

  2. We can drag the FloatingActor class directly into the Level Editor window to create an instance of FloatingActor in our world. It will be selected in the Level Editor and the World Outliner, where it will be called "FloatingActor1". Its Components and other properties will be visible in the Details Panel.

  3. Our FloatingActor needs to be visible in the game. While it is selected, we can click Add Component in the Details Panel, and select Cone to give it a simple visual representation.

  4. Now that our customized Actor is ready, let's move it to somewhere prominent. We can select it and drag it around in the world with the left mouse button, or we can move it manually. To move it manually, we need select it in the Level Editor or World Outliner and then use the Details Panel to select "FloatingActor1 (Instance)". We can now edit the Location field of FloatingActor1's Transform directly. Let's set X to -200 and Z to 200. This will place"FloatingActor1" right over the table in our scene.

  5. Press the Play button and watch the cone float up and down!

5. On Your Own!

Using what you have learned, try to do the following:

  • Add a Particle System Component to your FloatingActor. Some pre-built Particle Systems are already included in your project.
  • Use Unreal Engine's UProperty macro to expose a variable for the magnitude of your FloatingActor's movement, instead of using a hardcoded value. You might want to check the Variables, Timers, and Events tutorial for help on this topic.
  • Add periodic motion on the X and/or Y axis, and multiply the DeltaTime value by a number between 0.6 and 1.4, so your FloatingActor appears to float around freely. This can look great for powerups!

As for the specifics covered in this tutorial: