Unreal Engine
In this section, we will walk you through how to access the user profile in Unreal Engine using ChafKit.
Before you can access the user profile, you need to initialize ChafKit. Make sure you have added the necessary import statement at the top of your code file:
#include "ChafKit/Public/ChafKitAuth.h"
To access the user profile fields, you can use the following code:
auto Profile = ChafKitAuth::GetProfile().Value;
FString Username = Profile->GetUsername().Value;
FString UserID = Profile->GetUserId().Value;
FString Photo = Profile->GetPhoto().Value;
FString DisplayName = Profile->GetDisplayName().Value;
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, UserID);
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, Username);
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, Photo);
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, DisplayName);
This code retrieves the user profile information using the GetProfile
function from the ChafKitAuth
class. It then extracts the values of various profile fields, such as username, user ID, photo, and display name. The obtained profile information can be used as needed within your Unreal Engine game.
Here is the full code example within the Init
function of your custom GameInstance
class:
MyGameInstance.cpp
#include "MyGameInstance.h"
#include "Engine/Engine.h"
#include "ChafKit/Public/ChafKitSDK.h"
#include "ChafKit/Public/ChafKitAuth.h"
void UMyGameInstance::Init()
{
Super::Init();
auto CallbackHandle = ChafKitSDK::Initialize(TEXT("YOUR-GAME-ID"));
CallbackHandle->BindLambda(
[](ChafKitCore::ChafKitInitialized result)
{
if (result.error == nullptr) {
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, TEXT("Successfully Initialized!"));
auto Profile = ChafKitAuth::GetProfile().Value;
FString Username = Profile->GetUsername().Value;
FString UserID = Profile->GetUserId().Value;
FString Photo = Profile->GetPhoto().Value;
FString DisplayName = Profile->GetDisplayName().Value;
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, UserID);
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, Username);
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, Photo);
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, DisplayName);
}
else {
GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, result.error);
}
}
);
}