Implementing the Actor functionality by inheritance

Inheritance is the second way to implement a custom Actor. This is commonly done to make a new subclass, which adds member variables, functions, or a Component to an existing Actor class. In this recipe, we are going to add a variable to a custom GameState subclass.

How to do it...

  1. In the Unreal Editor, click on Add New in the Content Browser, and then on New C++ Class... then select GameState as the base class, then give your new class a name.
  2. Add the following code to the new class header:
    AMyGameState(); 
    
    UFUNCTION()
    void SetScore(int32 NewScore);
    
    UFUNCTION()
    int32 GetScore();
    private:
    UPROPERTY()
    int32 CurrentScore;
  3. Add the following code to the cpp file:
    AMyGameState::AMyGameState() { CurrentScore ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.