Creating a custom Actor in C++

While there are a number of different types of Actors that ship with Unreal as part of the default installation, you will find yourself needing to create custom Actors at some point during your project's development. This might happen when you need to add functionality to an existing class, combine Components in a combination not present in the default subclasses, or add additional member variables to a class. The next two recipes demonstrate how to use either composition or inheritance to customize Actors.

Getting ready

Make sure you have installed Visual Studio and Unreal 4 as per the recipe in Chapter 1, UE4 Development Tools. You'll also need to have either an existing project, or create a new one using the Unreal-provided wizard.

How to do it...

  1. Open up your project within the Unreal Editor, then click on the Add New button in Content Browser:
    How to do it...
  2. Select New C++ Class...
    How to do it...
  3. In the dialog that opens, select Actor from the list:
    How to do it...
  4. Give your Actor a name, such as MyFirstActor, then click on OK to launch Visual Studio.

    Tip

    By convention, class names for Actor subclasses begin with an A. When using this class creation wizard, make sure you don't prefix your class with A, as the engine automatically adds the prefix for you.

    How to do it...
  5. When Visual Studio loads, you should see something very similar to the following listing:
    MyFirstActor.h
    #pragma once
    
    #include "GameFramework/Actor.h"
    #include "MyFirstActor.generated.h"
    
    UCLASS()
    class UE4COOKBOOK_API AMyFirstActor : public AActor
    {
      GENERATED_BODY()
      public:
      AMyFirstActor(); 
    };
    MyFirstActor.cpp
    #include "UE4Cookbook.h"
    #include "MyFirstActor.h"
    AMyFirstActor::AMyFirstActor()
    {
      PrimaryActorTick.bCanEverTick = true;
    }

How it works...

In time, you'll become familiar enough with the standard code, so you will be able to just create new classes from Visual Studio without using the Unreal wizard.

  • #pragma once: This preprocessor statement, or pragma, is Unreal's expected method of implementing include guards—pieces of code that prevent an include file from causing errors by being referenced multiple times.
  • #include "GameFramework/Actor.h": We're going to create an Actor subclass, so naturally, we need to include the header file for the class we are inheriting from.
  • #include "MyFirstActor.generated.h": All actor classes need to include their generated.h file. This file is automatically created by Unreal Header Tool (UHT) based on the macros that it detects in your files.
  • UCLASS(): UCLASS is one such macro, which allows us to indicate that a class will be exposed to Unreal's reflection system. Reflection allows us to inspect and iterate object properties during runtime as well as manage references to our objects for garbage collection.
  • class UE4COOKBOOK_API AMyFirstActor : public AActor: This is the actual declaration of our class. The UE4COOKBOOK_API macro is created by UHT, and is necessary to help our project compile properly on Windows by ensuring that our project module's classes are exported correctly in the DLL. You will also notice that both MyFirstActor and Actor have the prefix A—this is the naming convention that Unreal requires for native classes that are inherited from Actor.
  • GENERATED_BODY(): GENERATED_BODY is another UHT macro that has been expanded to include the automatically generated functions that the underlying UE type system requires.
  • PrimaryActorTick.bCanEverTick = true;: Inside the constructor implementation, this line enables ticking for this Actor. All Actors have a function called Tick, and this Boolean variable means that the Actor will have that function called once per frame enabling the actor to perform actions in every frame as necessary. As a performance optimization, this is disabled by default.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.142.146