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.
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.
Add the following code to the new class header:
AMyGameState(); UFUNCTION() void SetScore(int32 NewScore); UFUNCTION() int32 GetScore(); private: UPROPERTY() int32 CurrentScore;
Add the following code to the cpp file:
AMyGameState::AMyGameState() { CurrentScore = 0; } int32 AMyGameState::GetScore() { return CurrentScore; } void AMyGameState::SetScore(int32 NewScore) { CurrentScore = NewScore; }
Confirm that your code looks like the following listing, and compile using the Compile button in the...