I’m trying to figure out how to setup the GameState. I know that every client have the game state, so I stored the team scores et the timer for the game there. And I’m trying to get the variable replicated to all the clients. That’s where my problem is. With the “Replicated” UPROPERTY tag, I’ve managed to replicate the score, but for some reason, it doesn’t work for the timer.
KOTHGameState.h
UCLASS()
class SNIPERBATTLE_API AKOTHGameState : public AGameState
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, Replicated, Category="Score")
int ScoreTeam1;
UPROPERTY(BlueprintReadOnly, Replicated, Category="Score")
int ScoreTeam2;
UPROPERTY(BlueprintReadOnly, Replicated, Category = "Timer")
FTimerHandle GameTimer;
};
KOTHGameState.cpp
#include "KOTHGameState.h"
#include "Net/UnrealNetwork.h"
void AKOTHGameState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AKOTHGameState, ScoreTeam1);
DOREPLIFETIME(AKOTHGameState, ScoreTeam2);
DOREPLIFETIME(AKOTHGameState, GameTimer);
}
PlayerHUD.cpp
#include “PlayerHUD.h”
#include “HealthWidget.h”
#include “ScoreWidget.h”
#include “TimerManager.h”
#include "KOTHGameState.h"
#include "GameTimerWidget.h"
bool UPlayerHUD::Initialize()
{
Super::Initialize();
GameState = GetWorld()->GetGameState<AKOTHGameState>();
if (GameState != nullptr)
{
SetScoreTeam1(FString::FromInt(GameState->ScoreTeam1));
SetScoreTeam2(FString::FromInt(GameState->ScoreTeam2));
GameTimer->SetGameTimer(GetFormatedGameTimer());
}
return true;
}
void UPlayerHUD::NativeTick(const FGeometry & MyGeometry, float DeltaTime)
{
Super::NativeTick(MyGeometry, DeltaTime);
if (GameState != nullptr)
{
GameTimer->SetGameTimer(GetFormatedGameTimer());
}
}
FString UPlayerHUD::GetFormatedGameTimer()
{
float RemainingSeconds = GetWorld()->GetTimerManager().GetTimerRemaining(GameState->GameTimer);
int Minutes = RemainingSeconds / 60;
int Seconds = fmod(RemainingSeconds, 60);
return FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds);
}
I’m obviously not understanding something properly. Any clues?