Put it all together - Login with Steam - (Unreal Engine module)
Last updated on October 24, 2024
Connect the UI to let player log in with Steam
In this tutorial, you will learn how to connect the SinglePlatformAuthWidget_Starter
class with the AuthEssentialsSubsystem_Starter
subsystem to handle the Steam login process.
Open the
SinglePlatformAuthWidget_Starter
C++ class and ensure that your class has the following highlighted code in theNativeOnActivated()
function.void USinglePlatformAuthWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
if (AuthEssentialsModule->IsStarterModeActive())
{
UE_LOG_AUTH_ESSENTIALS(Warning, TEXT("AuthEssentials is in starter mode and SinglePlatformAuth is dependent upon it. Please also enable the starter mode for the SinglePlatformAuth module."))
Btn_LoginWithSinglePlatformAuth->SetVisibility(ESlateVisibility::Collapsed);
return;
}
const bool bIsNativePlatformValid = IOnlineSubsystem::GetByPlatform() != nullptr;
if (!bIsNativePlatformValid)
{
Btn_LoginWithSinglePlatformAuth->SetVisibility(ESlateVisibility::Collapsed);
return;
}
if (ShouldAutoLogin())
{
OnLoginWithSinglePlatformAuthButtonClicked();
return;
}
const FString LoginButtonText = TEXT_LOGIN_WITH.ToString().Replace(TEXT("%PLATFORM%"), *GetDefaultNativePlatform());
Btn_LoginWithSinglePlatformAuth->SetButtonText(FText::FromString(LoginButtonText));
Btn_LoginWithSinglePlatformAuth->OnClicked().AddUObject(this, &ThisClass::OnLoginWithSinglePlatformAuthButtonClicked);
LoginWidget->SetButtonLoginVisibility(ShouldDisplayDeviceIdLogin() ? ESlateVisibility::Visible : ESlateVisibility::Collapsed);
}In the same class, add the following highlighted code to the
OnLoginWithSinglePlatformAuthButtonClicked()
function.void USinglePlatformAuthWidget_Starter::OnLoginWithSinglePlatformAuthButtonClicked()
{
UAuthEssentialsSubsystem_Starter* AuthSubsystem = GetGameInstance()->GetSubsystem<UAuthEssentialsSubsystem_Starter>();
ensure(AuthSubsystem);
// Set the login widget to logging in state and bind the retry login delegate.
LoginWidget->SetLoginState(ELoginState::LoggingIn);
LoginWidget->OnRetryLoginDelegate.AddUObject(this, &ThisClass::OnLoginWithSinglePlatformAuthButtonClicked);
// Login with single platform auth is considered as login with default native platform.
// Thus, it doesn't need username, token, nor the login method.
AuthSubsystem->SetAuthCredentials(EAccelByteLoginType::None, TEXT(""), TEXT(""));
AuthSubsystem->Login(GetOwningPlayer(), FAuthOnLoginCompleteDelegate::CreateUObject(LoginWidget, &ULoginWidget::OnLoginComplete));
}
When you click the Login with Steam
button, it will perform a Steam login via UAuthEssentialsSubsystem_Starter
. The callback is already being handled with the OnLoginComplete()
function in ULoginWidget_Starter
, which will do the following:
- If the login is successful, navigate to the main menu.
- If the login fails, display an error message.
Resources
- The files used in this tutorial section are available in the Byte Wars Unreal GitHub repository.
- AccelByteWars/Source/AccelByteWars/TutorialModules/Access/SinglePlatformAuth/UI/SinglePlatformAuthWidget_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Access/SinglePlatformAuth/UI/SinglePlatformAuthWidget_Starter.cpp
- AccelByteWars/Source/AccelByteWars/TutorialModules/Access/AuthEssentials/AuthEssentialsSubsystem_Starter.h
- AccelByteWars/Source/AccelByteWars/TutorialModules/Access/AuthEssentials/AuthEssentialsSubsystem_Starter.cpp