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