Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions Game/Source/GDKShooter/Private/Game/GDKSessionGameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ void AGDKSessionGameState::RemovePlayerState(APlayerState* PlayerState)

void AGDKSessionGameState::OnRep_SessionProgress()
{
USpatialNetDriver* SpatialNetDriver = Cast<USpatialNetDriver>(GetWorld()->GetNetDriver());
bool bAuthoritativeOverSessionEntity = SpatialNetDriver->StaticComponentView->HasAuthority(SessionEntityId, SessionComponentId);

if (bAuthoritativeOverSessionEntity)
{
SendStateUpdate(SessionProgress);
}
SendStateUpdate(SessionProgress);
TimerEvent.Broadcast(SessionProgress, SessionTimer);
}

Expand All @@ -78,44 +72,41 @@ void AGDKSessionGameState::TickGameTimer()
{
SessionTimer--;

USpatialNetDriver* SpatialNetDriver = Cast<USpatialNetDriver>(GetWorld()->GetNetDriver());
bool bAuthoritativeOverSessionEntity = SpatialNetDriver->StaticComponentView->HasAuthority(SessionEntityId, SessionComponentId);

if (SessionProgress == EGDKSessionProgress::Lobby && SessionTimer <= 0)
{
UE_LOG(LogGDK, Log, TEXT("Advance GameState to Running"));
SessionProgress = EGDKSessionProgress::Running;
if (bAuthoritativeOverSessionEntity)
{
SendStateUpdate(SessionProgress);
}
SendStateUpdate(SessionProgress);
SessionTimer = GameSessionLength;
}
if (SessionProgress == EGDKSessionProgress::Running && SessionTimer <= 0)
{
UE_LOG(LogGDK, Log, TEXT("Advance GameState to Results"));
SessionProgress = EGDKSessionProgress::Results;
if (bAuthoritativeOverSessionEntity)
{
SendStateUpdate(SessionProgress);
}
SendStateUpdate(SessionProgress);
SessionTimer = ResultsSessionLength;
}
if (SessionProgress == EGDKSessionProgress::Results && SessionTimer <= 0)
{
UE_LOG(LogGDK, Log, TEXT("Advance GameState to Finished"));
SessionProgress = EGDKSessionProgress::Finished;
if (bAuthoritativeOverSessionEntity)
{
SendStateUpdate(SessionProgress);
}
SendStateUpdate(SessionProgress);
}
}
}

void AGDKSessionGameState::SendStateUpdate(EGDKSessionProgress SessionProgressState)
{
if (!GetWorld()->GetNetDriver() || !GetWorld()->GetNetDriver()->IsA<USpatialNetDriver>())
// Only send the state update if we're using Spatial networking and if we have authority over the session entity.
UNetDriver* NetDriver = GetWorld()->GetNetDriver();
if (NetDriver == nullptr || !NetDriver->IsA<USpatialNetDriver>())
{
return;
}

USpatialNetDriver* SpatialNetDriver = Cast<USpatialNetDriver>(NetDriver);
bool bAuthoritativeOverSessionEntity = SpatialNetDriver->StaticComponentView->HasAuthority(SessionEntityId, SessionComponentId);
if (!bAuthoritativeOverSessionEntity)
{
return;
}
Expand All @@ -129,7 +120,7 @@ void AGDKSessionGameState::SendStateUpdate(EGDKSessionProgress SessionProgressSt
component_update.schema_type = Schema_CreateComponentUpdate(SessionComponentId);
Schema_Object* fields_object = Schema_GetComponentUpdateFields(component_update.schema_type);
Schema_AddInt32(fields_object, 1, SessionState);
Cast<USpatialNetDriver>(GetWorld()->GetNetDriver())->Connection->SendComponentUpdate(target_entity_id, &component_update);
SpatialNetDriver->Connection->SendComponentUpdate(target_entity_id, &component_update);
}