Creating a new C++ class

We're now going to create a new C++ class with the following steps:

  1. To do this, from the Unreal editor, click on File | New C++ Class. We'll be creating an Actor class, so select Actor as the base class. Actors are the objects that are placed in the scene (anything from meshes, to lights, to sounds, and more).
  2. Next, enter a name for your new class, such as MyNewActor. Hit Create Class. After it adds the files to the project, open MyNewActor.h in Visual Studio. When you create a new class using this interface, it will generate both a header file and a source file for your class.
  3. Let's just make our actor print a message to the output log when we start our game. To do this, we'll use the BeginPlay event. BeginPlay is called once the game has started (in a multiplayer game, this might be called after an initial countdown, but in our case, it will be called immediately).
  4. The MyNewActor.h file (which should already be open at this point) should contain the following code after the GENERATED_BODY() line:
    public:   virtual void BeginPlay();
  5. Then, in MyNewActor.cpp, add a log that prints Hello, world! in the void AnyNewActor::BeginPlay() function, which runs as soon as the game starts:
    void AnyNewActor::BeginPlay()
    {
        Super::BeginPlay();
        
        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Hello World!"));
    }
  6. Then, switch back to the editor and click on the Compile button in the main toolbar.
  7. Now that your actor class has compiled, we need to add it to the scene. To do this, navigate to the Content Browser tab located at the bottom of the screen. Search for MyNewActor (there's a search bar to help you find it) and drag it into the scene view, which is the level viewport. It's invisible, so you won't see it or be able to click on it. However, if you scroll the Scene/World Outliner pane (on the right-hand side) to the bottom, you should see the MyNewActor1 actor has been added to the scene:
    Creating a new C++ class
  8. To test your new actor class, click on the Play button. You should see a yellow Hello, world! message printed to the console, as shown in the following screenshot. This can be seen in the Output Log tab on the right-hand side of the Content Browser tab at the bottom of the screen:
    Creating a new C++ class

Congratulations, you have created your first actor class in Unreal.

..................Content has been hidden....................

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