First Class

This is not a post about X-Men: First Class, even though I love the franchise... today I'm going to explore the creation of C++ classes directly inside the Unreal Editor.

Creating a C++ class in Unreal Engine is really simple: just click on the "Create new C++ class" button, select on what existing class you want it to be derived from (an Actor in my case), and give it a name:





 This will create all the needed files and open Visual Studio editor.


Here we can see that both the .cpp and .h files have been created for us, along with some useful methods:

  • AMainMap(): This is obviously a constructor for the class. Here we can initialize all the variables, and do all the stuff we need to perform when the instance of the Actor is created. The variable bCanEverTick tells the compler if this object will need to do something every frame (like check its own psition, change animations, etc.). If not needed, it's better to set it to false to save performance. Note that every class derived from Actor has the prefix A- before the name we choose.
  • BeginPlay(): This method is called once, when the Level starts. Here we can reset variables if we enter a new level or re-start the same one.
  • Tick(float DeltaTime): this method will be called every frame if bCanEverTick was set to true in the constructor. Unreal team suggest to keep it light, because too many instructions could kill the performance of your game, especially if many instances of the class are placed in the scene at the same time. DeltaTime is the time in milliseconds from the last time this method was called. We can use this information to create animations indipendent from fps (frames per second).

After creating the class, I will extend it to a Blueprint class, right-clicking on it in the editor and selecting "extend class to Blueprint". The resulting class, that I called  "MainMapBP", will have all the variables and methods of my original C++ class.


After doing this, I can drag the MainMapBP icon inside the level so that it's instanced at the beginning of the level.

If I want to be able to set a variable for my C++ class directly in Unreal, I can use the UPROPERTY macro in my header file:


 In this screen I created the variable SMhex, that is a pointer to a StaticMesh, and I tell Unreal that I want it to be EditDefaultsOnly (I can set it but not change it at runtime), BlueprintReadOnly (I can only read it from the Blueprint, where I'm setting it) and I want it to show in my custom Category "StaticMeshComponents". After compiling, let's see the effect of this property:


Now, when I open the Blueprint editor for my MainMapBP I have a new tab called Static Mesh Components, and inside of it a variable called SMhex, that I can choose among all the Static Meshes I alreay imported in the editor.
This allows me to assign Static Meshes by simply selecting them from the drop-down list instead of hard-coding them in C++.

Next time we'll continue building our map class, trying to create an array of hexagons that will be teh base of our gameplay.

Game you next time!



Commenti