I noticed that BeginPlay function on my game state object is called twice. My game state wasn’t designed to handle this, so it ended up spawning two copies of various actors. Is that a bug? If not, are all actors require to handle multiple calls of BeginPlay?
I can reproduce this with Unreal 4.7.1 as follows:
-
Create BasicCode C++ project.
-
Add custom game state class to project and override BeginPlay as follows:
#pragma once
#include “GameFramework/GameState.h”
#include “MyGameState.generated.h”/**
*
*/
UCLASS()
class BASICCODE_API AMyGameState : public AGameState
{
GENERATED_BODY()virtual void BeginPlay() override { UE_LOG(LogTemp, Warning, TEXT("BeginPlay of %s (0x%016x)"), *this->GetName(), this); }
};
-
Create custom game mode blueprint that uses custom game state class, configure project to use custom game mode.
-
Pulling down play dropdown, Advanced Settings, set:
Number of Players: 2
Use Single Process: Unchecked
Editor Multiplayer Mode: Play as Client -
Click Play button to run.
RESULT: Debug output contains two BeginPlay lines.
[2015.03.27-21.30.09:030][341]LogTemp:Warning: BeginPlay of MyGameState_8 (0x00000000de80f800)
[2015.03.27-21.30.16:394][341]LogTemp:Warning: BeginPlay of MyGameState_8 (0x00000000de80f800)
EXPECTED: Debug output only shows a single occurrence of BeginPlay.
Setting breakpoints, I see that first call comes from:
> UE4Editor-BasicCode.dll!AMyGameState::BeginPlay() Line 18 C++
UE4Editor-Engine.dll!AWorldSettings::NotifyBeginPlay() Line 119 C++
UE4Editor-Engine.dll!AGameState::OnRep_MatchState() Line 228 C++
UE4Editor-CoreUObject.dll!UFunction::Invoke(UObject * Obj, FFrame & Stack, void * const Result) Line 3684 C++
UE4Editor-CoreUObject.dll!UObject::ProcessEvent(UFunction * Function, void * Parms) Line 920 C++
UE4Editor-Engine.dll!AActor::ProcessEvent(UFunction * Function, void * Parameters) Line 529 C++
UE4Editor-Engine.dll!FRepLayout::CallRepNotifies(FRepState * RepState, UObject * Object) Line 1529 C++
UE4Editor-Engine.dll!FObjectReplicator::PostReceivedBunch() Line 749 C++
UE4Editor-Engine.dll!UActorChannel::ProcessBunch(FInBunch & Bunch) Line 1901 C++
...
second call comes from:
> UE4Editor-BasicCode.dll!AMyGameState::BeginPlay() Line 18 C++
UE4Editor-Engine.dll!AActor::PostNetInit() Line 2434 C++
UE4Editor-Engine.dll!UActorChannel::ProcessBunch(FInBunch & Bunch) Line 1916 C++
...