Download

Does BeginPlay() get called on clients?

I’m trying to figure out if there is any BeginPlay() activity on the client during loading, as I feel like there should be.

I have a Training Target class that extends AActor (all in C++). During game start BeginPlay() activates and determines if the mesh is enabled/disabled based on a simple bool bDisabled. This will either enable/disable the mesh’s visibility, the collision, and etc. This Target is then placed inside the level VIA editor, and the bool is set randomly. However, whenever the Client joins, no debug messages are called, and their targets aren’t being disabled.


AROTTrainingTarget::AROTTrainingTarget(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{
	bDisabled = false;
	bInactive = true;
	ActivationType = EActivationType::ENUM_Active;

	// === Collision ===

	
	// === Components ===
	SceneComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComp"));
	RootComponent = SceneComponent;

	Mesh = ObjectInitializer.CreateOptionalDefaultSubobject<UStaticMeshComponent>(this, TEXT("MeshComp"));
	Mesh->AttachParent = RootComponent;	
	Mesh->bGenerateOverlapEvents = true;
	
	Mesh->SetCollisionProfileName("Pawn");
	
	PreviewMesh = ObjectInitializer.CreateOptionalDefaultSubobject<UStaticMeshComponent>(this, TEXT("PreviewMeshComp"));
	PreviewMesh->AttachParent = RootComponent;
	PreviewMesh->bHiddenInGame = true;
	PreviewMesh->SetCollisionProfileName("NoCollision");
	PreviewMesh->bGenerateOverlapEvents = false;

	//Replication
	bReplicates = true;
	
}
	
void AROTTrainingTarget::BeginPlay()
{

	if (GetNetMode() == ENetMode::NM_Client)
		GEngine->AddOnScreenDebugMessage(-1, 15, FColor::Yellow, TEXT("CLIENT!!!!"));

	if (HasAuthority())
		GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Blue, TEXT("Server"));
	else
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, TEXT("Client"));
	
	Super::BeginPlay();

	//Begin activation
	if (bDisabled)
	{
		DisableTarget(); 
		return;
	}

	EnableTarget();

}

void AROTTrainingTarget::ToggleEnabled()
{
	GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, TEXT("ToggleEnabled!"));

	if (!bDisabled)
		EnableTarget();
	else
		DisableTarget();
}

void AROTTrainingTarget::EnableTarget()
{
	UE_LOG(Log, All, TEXT("%s - EnableTarget()"), *GetName());
	
	if (HasAuthority())
	{
		switch (ActivationType)
		{
		case EActivationType::ENUM_Blueprint:
			PopDown();
			break;
		case  EActivationType::ENUM_Radial:
			PopDown();
			break;
		case EActivationType::ENUM_Active:
			Popup();
			break;
		}
	}

	Mesh->SetHiddenInGame(false);
	bDisabled = false;
	Mesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
}


void AROTTrainingTarget::DisableTarget()
{
	Mesh->SetHiddenInGame(true);
	bDisabled = true;	
	Mesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}


Yep, it does get called. Providing it’s loaded and/or spawned on them of course.

Try calling Super before you’re debug messages, just in case any setup is done that allows you to do that.

I’ve tried using Super first. For some reason BeginPlay() is not being called on any of my classes on the client at all, I tried various classes including Pawn. But if I copy the code over to any of my other projects, it works fine and the debugs messages show up. There seems to be something wrong with this project, but I’m not sure what.

Sorry for the necro. Did you ever figure this out? I’m having the same problem where BeginPlay just isn’t called on clients.

Just make sure that you’re not missing any Super call for any method on any class. Maybe you’re missing a Super call in another class and other method.
For example, maybe you’re overriding another method like PostInitializeComponents in other class and you forgot the Super call.
Another cause could be that you override two methods and call the same super for both methods.
For example:



YourClass::PreInitializeComponents()
{
    Super::PreInitializeComponents();
}

YourClass::PostInitializeComponents()
{
    Super::PreInitializeComponents();
}


In all those cases you should expect an unpredictable behavior like BeginPlay not being called on clients.