Why we need to override the BeginPlay and why we need to call Super::BeginPlay in overridden BeginPlay?

I made some games in unity but I am new to UE4 but i know about c++. why we can’t write the functionality in Tick and BeginPlay functions like we do in unity’s start and update without overriding? these functions calling behaviour is controlled by ue4’s behaviour tree so why we need to call base class function (Super::Tick) in overridden tick function?

does’t UE4’s behaviour tree handle that when which function sould be called. so why we even need to call super tick function. i am still confused about overridden function why we can’t just write the code in super::tick after all its an abstract function?

can you explain it in more simple way …

The base class functions do specific things, in AActor you can see it does this

if (GetWorldSettings() != nullptr && (bAllowReceiveTickEventOnDedicatedServer || !IsRunningDedicatedServer()))
	{
		ReceiveTick(DeltaSeconds);
	}


	// Update any latent actions we have for this actor

	// If this tick is skipped on a frame because we've got a TickInterval, our latent actions will be ticked
	// anyway by UWorld::Tick(). Given that, our latent actions don't need to be passed a larger
	// DeltaSeconds to make up the frames that they missed (because they wouldn't have missed any).
	// So pass in the world's DeltaSeconds value rather than our specific DeltaSeconds value.
	UWorld* MyWorld = GetWorld();
	MyWorld->GetLatentActionManager().ProcessLatentActions(this, MyWorld->GetDeltaSeconds());

	if (bAutoDestroyWhenFinished)
	{
		bool bOKToDestroy = true;

		for (UActorComponent* const Comp : GetComponents())
		{
			if (Comp && !Comp->IsReadyForOwnerToAutoDestroy())
			{
				bOKToDestroy = false;
				break;
			}
		}

		// die!
		if (bOKToDestroy)
		{
			Destroy();
		}
	}

You can still write your own code after the Super::Tick() function of course. Apart from that 1 line it’s really no different to Unity’s Update() function.

Overriding these functions means when ue4 calls all the actors tick functions it’ll call the tick function you created since your class is derived from AActor

Not really sure why you want to go into the base classes tick to do stuff. It’s 1 very short line of code you need to write. And you are writing the functionality in your classes BeginPlay and Tick functions anyway, they just have virtual and override in the .h file

i am getting your point, and I don’t have any issue to write the simple line (super::Tick). but I want to know the purpose of that single line to clear concepts.

Well… That single line simply executes parent’s implementation of given function.

When you have a hierarchy like AActorAItemAWeapon and engine calls Tick function on AActor then it actually executes AWeapon’s implementation of that function. It’s your job as a programmer to specify whether you want to entirely override Tick behavior (and skip Super call) or if you want to execute it as well. Thanks to this, you can also decide in what exact part of your Tick function the Super implementation will be executed. In most basic cases it doesn’t matter and calling Super implementation on top of your function is fine. But sometimes you actually do want to control when that Super implementation is executed.

Well, with access to UE4 code you can check it out on your own. See Actor.cpp (AActor::Tick function).

As of 4.18 it calls Tick event in BP, processes latent actions (delay nodes for example), and takes care of destroying itself if it’s this kind of actor and all its components allow it to do so.

thanks for explaining it. nd what kind of functionality is implemented by Super Tick.
and can completely skip that super tick call or its necessary to call it to execute the overridden functions?

As for your original question about BeginPlay. Skipping execution of Super BeginPlay would cause your actor to stop ticking (same goes for all of its components) for example. It wouldn’t route BeginPlay event to blueprints, (same goes for all of its components). Actors that are would be supposed to destroy themselves over time wouldn’t do that anymore.

BeginPlay and Tick are inherited methods. Override is a C++ directive that asks the compiler to make sure your method signature matches the inherited signature. Super just refers to the inherited class. When you call Super::Tick, you’re saying you want your override method to do the same thing that the inherited method does, and afterwards, you want to do whatever additional functionality you added.

You can call these functions without invoking the Super class, but then you won’t have any of the base functionality.

1 Like