Functions, Arrays & Classes

Functions, Arrays & Classes - Page 1

A look at functions, arrays and classes in UE4 & C++

Otter dancing with a fish

For this exercise we will be just using blueprints so all we need is to run Unreal. It is easiest to manage the various versions of Unreal using the Epic Launcher. This allows you to install and run multiple versions of the engine. Go to and install the Epic Game Launcher. From here we will manage our game assets and resources. You will also need to install either Visual Studio Community 19 on the PC with C++ drivers or XCode on the mac.

1. Simple Function in CPP

Lets look at how functions work in C++ and in Unreal Blueprints. Then lets create a function in C++ that works within blueprints.

There are user defined functions as well as supplied functions by the language we are using or a library we are including. It is a block of code that performs a task. We can pass it parameters (various variables)and the function can return a single type or none at all (void). Let's write a simple function:


Simple Function in CPP

pen up Visual Studio 19 Community or xcode. You should see a screen like this. Click on the Create a New Project button:


Select Console

Select a Console App as we will only be working withing the console.


Name and Save

Name the solution FunctionTemplatesClasses and select a location on your hard drive to save it in.

Three Components

A function has three components. We start with ReturnType then we have the name of the function FunctionName and then in parenthesis we can pass it parameters (or not) separated by commas like (Param1, Param2). So for example we can have a function that just prints a message that returns no value nor does it take any parameters that can read void Sorry(). This is a function that returns nothing and takes no parameters. Let's give this a shot.


Create Function

Lets now create a function. Now we have used functions before main() is a function that visual studio runs first by default. We can also define a new function. In this case we defined a new function that returns no value (it is void). Next run the game to see what happens.


Run Program

Run the program and Main() runs and calls the function. The function just prints Sorry! to the console.


Try Placing Function after main()

Now what happens if the Sorry() function is placed AFTER the Main() function? Try it and press the Run button:


Doesn't Run

Now the build won't compile or run. If you look at the bottom you should see an Output tab that has 0 succeeded builds and 1 file that failed to compile. You get an error message asking if you want to run the previous build. Press the No button.


Declare Function

Now you can see in the bottom tab an error list where it can't find the identifier on line 11 in the FunctionTemplatesClasses.cpp file. The compiler does not kow that the function exists when it gets to it and we need to declare it first (even before it is defined).


Declare Function

What we can do is tell the compiler before the function definition that there is a declaration (stating the the definition is somewhere in the file). As long as the declaration is before the definition, it will compile and run as before. In the above case it would look like:


Build Before Running

It is good practice to build before you run, if you can. You can do this by selecting Build | Build NAMEOFPROJECT or by pressing control b on the keyboard. You will notice that it now builds and runs as it did previously. Check this on your end.


Declarations & Definitions

Notice that our program is located in a .cpp file called FunctionTemplatesClasses.cpp. This is usually where we keep our definitions. Our declarations are usually held in a .h file. Lets move this to a .h file. Right click on the Solution Explorer | HeaderFiles folder and select Add | New Item:


Create Header

Select the file type Header File (.h) and it is customary to name the file that holds the declarations with the same name as the one with the definitions. So I called it FunctionTemplatesClasses.h then pressed the Add button:


Move Declaration

Now remove the declaration from the .cpp file and add it under the provide line #pragma once. What does #pragma once do? It is a non-standard but widely supported preprocessor directive that tells the compiler to only include this file in the final program once. Now a game can be made up of hundreds if not thousands of files, and a header file could be used in many places. This ensures that no many how many times it is used it is just included one time in the final program.


Now if we try and run it, this will not work. The .cpp file does not know to look for this declaration. So go back to the cpp file and add an #include with the FunctionTemplatesClasses.h file like so:


Run Project

Next, run the project again it should find the definition by including the .h file. Looks no different than before.