GameState uninitialised in networking test

Hello,

I am currently trying to pick apart the networking aspects of the ShooterGame example.
All I’m trying is to have an integer in the GameState that is periodically incremented with a timer that will replicate and be displayed on any clients.

My very simple GameState:
NetworkTestGameState.h


UCLASS()
class ANetworkTestGameState : public AGameState
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(Transient, Replicated)
	int32 Counter;
};

NetworkTestGameState.cpp


ANetworkTestGameState::ANetworkTestGameState(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	Counter = 5;
}

void ANetworkTestGameState::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(ANetworkTestGameState, Counter);
}

NetworkTestGameMode.h


UCLASS()
class ANetworkTestGameMode : public AGameMode
{
	GENERATED_UCLASS_BODY()

	virtual void BeginPlay() OVERRIDE;
	virtual void HandleMatchHasStarted() OVERRIDE;

	void IncrementValue();
};

NetworkTestGameMode.cpp


ANetworkTestGameMode::ANetworkTestGameMode(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	PlayerControllerClass = ANetworkTestPlayerController::StaticClass();
	GameStateClass = ANetworkTestGameMode::StaticClass();
}

void ANetworkTestGameMode::BeginPlay()
{
	Super::BeginPlay();

	if (!GEngine)
		return;
	GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Yellow, TEXT("Hello World!"));

	//Set timer for update function
	if (Role == ROLE_Authority)
		GetWorldTimerManager().SetTimer(this, &ANetworkTestGameMode::IncrementValue, 10.0f, true);

	// start match if necessary.
	if (GetMatchState() == MatchState::WaitingToStart)
	{
		StartMatch();
	}
}

void ANetworkTestGameMode::IncrementValue()
{
	FName state = GameState->GetMatchState();
	
	ANetworkTestGameState* const MyGameState = Cast<ANetworkTestGameState>(GameState);
	if (MyGameState && MyGameState->Role == ROLE_Authority)
		MyGameState->Counter++;
}

void ANetworkTestGameMode::HandleMatchHasStarted()
{
	Super::HandleMatchHasStarted();

	GameState->SetMatchState(MatchState::InProgress);

	ANetworkTestGameState* const MyGameState = Cast<ANetworkTestGameState>(GameState);
	if (MyGameState && MyGameState->Role == ROLE_Authority)
		MyGameState->Counter++;
}

As you can see I am able to refer to GameState however while these lines run without exception viewing the GameState object isn’t possible and the return value from GetMatchState seems garbled. The casting to my custom game state is never successful.

I’m missing a trick from ShooterGame that initialises GameState it seems but I seriously cannot see the forest for the trees in that example right now!
Any ideas?

Thanks,
Berno

You know how sometimes when you explain your problem to someone else and suddenly figure out the problem because you’ve been forced to go over the details? :slight_smile:

While reviewing my post I noticed this line in my game mode constructor:


GameStateClass = ANetworkTestGameMode::StaticClass();

Changing it to the state type just sorted out my problems!


GameStateClass = ANetworkTestGameState::StaticClass();

I guess this is what happens when you dont really understand a line, I pulled this from the shooter example does anybody know whats actually being set here? Is it the default Game State? Same as in the editor?

Yeah, happens sometimes :slight_smile:

Regarding setting game state in C++, yeah it’s exactly the same thing as in editor, the only difference here is that you don’t use configs/blueprints but hard-code it. Not much of difference, just different ways to do the same thing.