Course: The EOS Online Subsystem (OSS) Plugin

Here is achievements. We’ll update the GitHub repo when we publish the 3rd part of this course.

// Additional code to query achievements -  example not in course
void AEOSPlayerState::QueryAchievements()
{
	// This function will query EOS achievements for a given player
	// To query achievements we need to cache the achievement descriptions AND achievement data
	
	IOnlineSubsystem* Subsystem = Online::GetSubsystem(GetWorld());
	IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
	IOnlineAchievementsPtr Achievements = Subsystem->GetAchievementsInterface(); 

	// Check if player is online before trying to update stat -- this code is repeated and could go in it's own function
	FUniqueNetIdPtr NetId = Identity->GetUniquePlayerId(0);

	if (!NetId || Identity->GetLoginStatus(*NetId) != ELoginStatus::LoggedIn)
	{
		return;
	}

	// Cache description
	Achievements->QueryAchievementDescriptions(*NetId, FOnQueryAchievementsCompleteDelegate::CreateLambda([this](
		const FUniqueNetId& NetId,
		const bool bQueryResultSuccessful
		)
		{
			if (bQueryResultSuccessful)
			{
				QueryAchievementsData();
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Failed to query achievement description."));
			}
		}));
}

void AEOSPlayerState::QueryAchievementsData()
{
	IOnlineSubsystem* Subsystem = Online::GetSubsystem(GetWorld());
	IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
	IOnlineAchievementsPtr Achievements = Subsystem->GetAchievementsInterface();

	// Check if player is online before trying to update stat -- this code is repeated and could go in it's own function
	FUniqueNetIdPtr NetId = Identity->GetUniquePlayerId(0);

	if (!NetId || Identity->GetLoginStatus(*NetId) != ELoginStatus::LoggedIn)
	{
		return;
	}

	// Cache achievement data 
	Achievements->QueryAchievements(*NetId, FOnQueryAchievementsCompleteDelegate::CreateLambda([this](
		const FUniqueNetId& NetId,
		const bool bQueryResultSuccessful
		)
		{
			if (bQueryResultSuccessful)
			{
				GetAchievements(); 
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Failed to query achievement data.")); 
			}
		}));
}

void AEOSPlayerState::GetAchievements()
{
	IOnlineSubsystem* Subsystem = Online::GetSubsystem(GetWorld());
	IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
	IOnlineAchievementsPtr Achievements = Subsystem->GetAchievementsInterface();

	// Check if player is online before trying to update stat -- this code is repeated and could go in it's own function
	FUniqueNetIdPtr NetId = Identity->GetUniquePlayerId(0);

	if (!NetId || Identity->GetLoginStatus(*NetId) != ELoginStatus::LoggedIn)
	{
		return;
	}

	// Read cache achievement description and data
	TArray<FOnlineAchievement> AchievementsData;
	if (Achievements->GetCachedAchievements(*NetId, AchievementsData) == EOnlineCachedResult::Success)
	{
		for (auto AchievementData : AchievementsData)
		{
			FOnlineAchievementDesc AchievementDescription;

			if (Achievements->GetCachedAchievementDescription(AchievementData.Id, AchievementDescription)
				== EOnlineCachedResult::Success)
			{
				FString AchievementId = AchievementData.Id;
				double AchievementProgress = AchievementData.Progress;
				FText AchievementTitle = AchievementDescription.Title;
				FText LockedDescription = AchievementDescription.LockedDesc;
				FText UnlockedDescription = AchievementDescription.UnlockedDesc;
			}
			else
			{
				UE_LOG(LogTemp, Error, TEXT("Failed to get cached achievement description achievement with id: %s."),
					*AchievementData.Id);
			}
		}
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("Failed to get cached achievement data."))
	}
}
3 Likes