What is the Super:: and why we use it.
I am new in Unreal Engine. I searched a few and I found that we use Super:: with override functions. In this way we call the base class function. Why we wanna do this ? Could someone explain properly please ?
What is the Super:: and why we use it.
I am new in Unreal Engine. I searched a few and I found that we use Super:: with override functions. In this way we call the base class function. Why we wanna do this ? Could someone explain properly please ?
Super
is a typedef
for the base class of your current class. It is part of the GENERATED_BODY()
and will be added by the UnrealHeaderTool
alongside ThisClass
.
In the following example, I define a class UYourClass
that inherits from UObject
, the base object for a lot of Unreals classes.
class UYourClass : public UObject
{
GENERATED_BODY()
};
The GENERATED_BODY()
will contain the following code:
typedef UYourClass ThisClass;
typedef UObject Super;
When you want to call an implementation for an overriden behavior of your base class, you do not need to write the base typename, but instead can just use Super
. And when accessing the own class, you can also use ThisClass
. It makes the code a little shorter and more portable, e.g. when you use macros to generate code.
In the following scenario, we inherit from AActor
and override its BeginPlay
method, so we can initialize our own object. But because AActor
also does stuff in its BeginPlay
implementation (e.g. calling the node "Begin Play" in blueprints), we have to call it as well, otherwise the behavior is faulty.
class ABuilding : public AActor
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
}
void ABuilding::BeginPlay()
{
Super::BeginPlay();
// Do your own stuff here
}
That's as far as I am willing to describe it. Everything beyond this point is just "C++ works like that" or "OOP works like that" and that's not suitable for this site.