Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void UEosHelper::SubmitLeaderboardScore(APlayerController* PlayerController, FName StatName, int32 StatValue)
- {
- UE_LOG(LogTemp, Warning, TEXT("Submitting score for %s. Value: %d"), *StatName.ToString(), StatValue);
- FOnlineLeaderboardWrite WriteObject;
- WriteObject.LeaderboardNames.Add(StatName);
- WriteObject.RatedStat = StatName;
- WriteObject.DisplayFormat = ELeaderboardFormat::Milliseconds;
- WriteObject.SortMethod = ELeaderboardSort::Ascending;
- WriteObject.UpdateMethod = ELeaderboardUpdateMethod::KeepBest;
- WriteObject.SetIntStat(StatName, StatValue);
- bool Successful = DoWriteLeaderboardObject(PlayerController, WriteObject);
- }
- void UEosHelper::QueryTopPlayersLeaderboard(FName LeaderboardName, int32 Range)
- {
- if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
- {
- IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
- if (Leaderboards.IsValid())
- {
- FOnlineLeaderboardReadRef ReadRef = MakeShared<FOnlineLeaderboardRead, ESPMode::ThreadSafe>();
- ReadRef->LeaderboardName = FName(LeaderboardName);
- ReadRef->SortedColumn = LeaderboardName;
- new (ReadRef->ColumnMetadata) FColumnMetaData(LeaderboardName, EOnlineKeyValuePairDataType::Int32);
- Leaderboards->AddOnLeaderboardReadCompleteDelegate_Handle(
- FOnLeaderboardReadComplete::FDelegate::CreateUObject(
- this,
- &UEosHelper::TopLeaderboardReadComplete,
- ReadRef));
- Leaderboards->ReadLeaderboardsAroundRank(1, Range, ReadRef);
- }
- }
- }
- void UEosHelper::QueryLeaderboardAroundPlayer(FName LeaderboardName, const APlayerController* PlayerController, int32 Range)
- {
- if (APlayerState* PlayerState = (PlayerController != NULL) ? PlayerController->PlayerState : NULL)
- {
- FUniqueNetIdPtr LocalUserNetId = PlayerState->GetUniqueId().GetUniqueNetId();
- if (LocalUserNetId.IsValid())
- {
- if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
- {
- IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
- if (Leaderboards.IsValid())
- {
- FOnlineLeaderboardReadRef ReadRef = MakeShared<FOnlineLeaderboardRead, ESPMode::ThreadSafe>();
- ReadRef->LeaderboardName = FName(LeaderboardName);
- Leaderboards->AddOnLeaderboardReadCompleteDelegate_Handle(
- FOnLeaderboardReadComplete::FDelegate::CreateUObject(
- this,
- &UEosHelper::TopLeaderboardReadComplete,
- ReadRef));
- Leaderboards->ReadLeaderboardsAroundUser(LocalUserNetId.ToSharedRef(), Range, ReadRef);
- }
- }
- }
- }
- }
- void UEosHelper::TopLeaderboardReadComplete(bool bWasSuccessful, FOnlineLeaderboardReadRef ReadRef)
- {
- TArray<FLeaderboardUiData> LeaderboardUiData;
- ClearTopLeaderboardReadDelegates();
- if (bWasSuccessful)
- {
- for (const FOnlineStatsRow& Row: ReadRef->Rows)
- {
- const FVariantData* Variant = Row.Columns.Find(ReadRef->LeaderboardName);
- int32 Value = 0;
- if (Variant != nullptr)
- {
- // Get player rank
- Variant->GetValue(Value);
- // Create list item
- FLeaderboardUiData DataToWrite;
- DataToWrite.Rank = Row.Rank;
- FUniqueNetIdPtr PlayerId = Row.PlayerId;
- if (PlayerId != nullptr)
- {
- DataToWrite.PlayerId = PlayerId->ToString();
- }
- DataToWrite.PlayerName = Row.NickName;
- DataToWrite.PlayerScore = Value;
- // Add to list
- LeaderboardUiData.Add(DataToWrite);
- } else
- {
- UE_LOG(LogTemp, Warning, TEXT("Leaderboard value is not found for Leaderboard %s"), *ReadRef->LeaderboardName.ToString());
- }
- }
- }
- LeaderboardLoadComplete.Broadcast(LeaderboardUiData);
- }
- void UEosHelper::ClearTopLeaderboardReadDelegates()
- {
- if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
- {
- IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
- if (Leaderboards.IsValid())
- {
- Leaderboards->ClearOnLeaderboardReadCompleteDelegates(this);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement