The use of virtual functions in ToonTanks

I do not understand the purpose of declaring BeginPlay as a virtual function in the ProjectileBase.h file. Furthermore, why do we need to use the Super:: accessor in the respective cpp file when it seems like we’re not doing anything with it?

I understand virtual functions are used when you want to override a base class with a child class, but what is the base class we’re wanting to override here? I’m on lecture 150 and so far the corresponding code for what I’m asking about is as follows:

Declaration:

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

Implementation:

// Called when the game starts or when spawned
void AProjectileBase::BeginPlay()
{
	Super::BeginPlay();
}

AProjectileBase inherits from another class, and this also declares BeginPlay as a virtual - this means it can be overridden. So, in AProjectileBase, you are specifying virtual so you can create classes defining different types of projectiles based on AProjectileBase and perhaps add additional code by overriding the BeginPlay method.

e.g. you could create ACannonProjectile and AMachineGunProjectile each based on AProjectileBase and each overriding the BeginPlay to change behaviours.

1 Like

Thank you Brian

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.