3. Preparation

Creating a C++ script in UE4 Before we can start with the fun stuff, we need to create a script where we can work on. We need to create one script for this workshop, where we will put all of our code in. 


In order to create a script, we need to access the “C++ Classes” folder in the content browser. Click on the folder icon left of “Content” in the content browser. A window will open where you can at least choose between “Content” and “C++ Classes”. It’s possible that you see extra folders that are related to the engine. You can hide and unhide these folders in your settings.

Click on “C++ Classes” to access the C++ Classes folder. In this folder you can find all of your C++ scripts in the project.

Img 2, Accessing the “C++ Classes” folder


Right click in the content browser. A window will appear like in image 3. Click on “New C++ Class”.

Img 3, Clicking on “New C++ Class” to create a new script


A window will ask you to choose a parent class. UE4 has different parent classes for different purposes. We are choosing for the “Actor” parent class, because that will give us the right access to the right pieces of code that we need for creating our dynamic material.

Img4, Choosing the “Actor” parent class


Then we’ll get the option to give our class a name. I am calling my script “DMObject”. Click on “Create Class” to finalize the creation of the script.

Img 5, Creating our “DMObject” actor script


When UE4 is done compiling our new script, your header file should look like this:

Img 6, Default header setup of an actor script


Preparing a header for programming with dynamic materials

We need to include “Materials/MaterialInstanceDynamic.h” in our header. This will give us access to the necessary code that we need for programming with dynamic materials. 

Make sure to never include anything after the “.generated.h” inclusion.

Img 7, Including “Materials/MaterialInstanceDynamic.h”


In our header we need to create a pointer. The pointer will store the memory address of the dynamic material we are going to create. With that we can access our dynamic material from different times and locations, within our script.

Img 8, Creating a pointer for our dynamic material


It is important to use “TWeakObjectPtr<>” in order for the system of UE4 to recognize a pointer. The default pointers of C++ are not recognized by UE4. Because of that the program may delete a pointer that is still in use, which will cause crashes.


The class “UMaterialInstanceDynamic” is a standard class of UE4, which represents our dynamic material. In opposition of a static material, which has the class “UMaterial”. A static material is the default material of UE4, which cannot be accessed at runtime.


Your .cpp file should look like this:

Img 9, Default .cpp setup of an actor script


The header is now prepared for programming with dynamic materials.


Back to the previous lesson | Continue to the next lesson


Popular posts from this blog

6. Creating and editing the dynamic material

5. Constructing a blueprint

8. Reading parameters of the dynamic material