I’m still not really sure what super:: does. I understand it’s a class and that it’s somehow related to inheritance, but anything more than that and I’m lost…
So what does super do?
Here’s the code that I’m especially confused about:
void UPositionReporter::BeginPlay()
{
Super::BeginPlay(); // this line right here!
UE_LOG(LogTemp, Warning, TEXT("Position report reporting for duty on Chair!")); // this is a macro
}
@DanM, Ok. So all Super is is a shorter way of saying that BeginPlay() belongs to the UActorComponent class? If this was the case why wouldn’t unreal just say: UActorComponent::BeginPlay()?
Also, how does this relate to inheritance? Ben said something about how Super runs something first in the chain of inheritance? What does this mean?
It’s to call the parent class’ function. I’ll give a code example at the end
So you don’t need to concern yourself with what the parent class is when writing these.
Code example
#include <iostream>
class Base
{
public:
virtual void Foo()
{
std::cout << "Base\n";
}
};
class Derived : public Base
{
public:
using Super = Base;
virtual void Foo() override
{
//uncomment the next line
//Super::Foo();
std::cout << "Derived\n";
}
};
int main()
{
Derived D;
D.Foo();
}
If you uncomment the Super::Foo() line you will see the output has changed.
Think of it this way, with the call to the super class version you are extending the functionality whereas without you are completely replacing the functionality.
@DanM, sorry for the long response time … was applying to uni. Your response was very clear and thanks for the example. Some questions:
Firstly, what do you mean by “functionality?”
Secondly, BeginPlay shows up in two places:
void UGrabber::BeginPlay() // what is the difference between this BeginPlay()
{
Super::BeginPlay(); // and this BeginPlay()?
}
I know they are both methods of classes (and that one is the method of UGrabber while the other is the method of UActorComponent) but is there any difference in what they do/how they work/etc.? Might I be overthinking this?
Thirdly, Super is used in more than one place. How does that work? Is Super a big class with other classes inside it?
void UGrabber::BeginPlay()
{
Super::BeginPlay(); // here and...
}
// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction); // ...here
}
Finally, to make sure I understand your response, here’s my summary. Please tell me what’s right/wrong. Super is just a class. BeginPlay() is one of the functions of that class that already has some functionality included because of Unreal Engine code.