Put it all together - Private Chat - (Unreal Engine module)
Connect the UI with private chat
In this tutorial, you will connect your implementation in PrivateChatSubsystem_Starter
to the private chat widget.
Open the
PrivateChatSubsystem_Starter
CPP file and start with connecting the UI to send private chat messages. Replace theSendPrivateChatMessage()
code with the following code:void UPrivateChatWidget_Starter::SendPrivateChatMessage(const FText& MessageText)
{
if (!PrivateChatSubsystem)
{
UE_LOG_PRIVATECHAT(Warning, TEXT("Cannot send private chat message. Private Chat subsystem is not valid."));
return;
}
if (!ensure(GetOwningPlayer()))
{
UE_LOG_PRIVATECHAT(Warning, TEXT("Cannot send private chat message. PlayerController is not valid."));
return;
}
const ULocalPlayer* LocalPlayer = GetOwningPlayer()->GetLocalPlayer();
if (!ensure(LocalPlayer))
{
UE_LOG_PRIVATECHAT(Warning, TEXT("Cannot send private chat message. LocalPlayer is not valid."));
return;
}
// Send private private chat message.
PrivateChatSubsystem->SendPrivateChatMessage(
LocalPlayer->GetPreferredUniqueNetId().GetUniqueNetId(),
PrivateChatRecipientUserId,
MessageText.ToString());
}Next, modify the
GetLastPrivateChatMessages()
to get and display the chat message history. To do this, replace its code with the following code:void UPrivateChatWidget_Starter::GetLastPrivateChatMessages() const
{
if (!PrivateChatSubsystem)
{
UE_LOG_PRIVATECHAT(Warning, TEXT("Cannot get last private chat messages. Private Chat subsystem is not valid."));
return;
}
if (!ensure(GetOwningPlayer()))
{
UE_LOG_PRIVATECHAT(Warning, TEXT("Cannot get last private chat messages. PlayerController is not valid."));
return;
}
const ULocalPlayer* LocalPlayer = GetOwningPlayer()->GetLocalPlayer();
if (!ensure(LocalPlayer))
{
UE_LOG_PRIVATECHAT(Warning, TEXT("Cannot get last private chat messages. LocalPlayer is not valid."));
return;
}
// Get chat room id.
const FString ChatRoomId = PrivateChatSubsystem->GetPrivateChatRoomId(
LocalPlayer->GetPreferredUniqueNetId().GetUniqueNetId(),
PrivateChatRecipientUserId);
// Abort if room id was not found.
if (ChatRoomId.IsEmpty())
{
W_Chat->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
return;
}
// Get last private chat messages.
TArray<TSharedRef<FChatMessage>> OutMessages;
if (PrivateChatSubsystem->GetLastPrivateChatMessages(
LocalPlayer->GetPreferredUniqueNetId().GetUniqueNetId(),
ChatRoomId,
W_Chat->GetMaxChatHistory(),
OutMessages))
{
// Abort if last messages is empty.
if (OutMessages.IsEmpty())
{
W_Chat->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Empty);
return;
}
// Display last chat messages.
Algo::Reverse(OutMessages);
for (const TSharedRef<FChatMessage>& Message : OutMessages)
{
AppendChatMessage(Message.Get());
}
}
// Show error message if failed.
else
{
W_Chat->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Error);
}
}Now, complete the code by binding the chat events with the widget. To do this, modify the
NativeOnActivated()
by adding the following code. You will also add a call to theGetLastPrivateChatMessages
function so that the messages will be loaded when this menu opens.void UPrivateChatWidget_Starter::NativeOnActivated()
{
// ...
W_Chat->OnSubmitDelegates.AddUObject(this, &ThisClass::SendPrivateChatMessage);
// ...
if (PrivateChatSubsystem)
{
PrivateChatSubsystem->GetOnSendPrivateChatCompleteDelegates()->AddUObject(this, &ThisClass::OnSendPrivateChatComplete);
PrivateChatSubsystem->GetOnPrivateChatMessageReceivedDelegates()->AddUObject(this, &ThisClass::OnPrivateChatMessageReceived);
}
// Display default state.
GetLastPrivateChatMessages();
}Finally, unbind those events when the widget is closed. To this, modify the
NativeOnDeactivated()
by adding the following code:void UPrivateChatWidget_Starter::NativeOnDeactivated()
{
// ...
W_Chat->OnSubmitDelegates.RemoveAll(this);
// ...
// Unbind chat events.
if (PrivateChatSubsystem)
{
PrivateChatSubsystem->GetOnSendPrivateChatCompleteDelegates()->RemoveAll(this);
PrivateChatSubsystem->GetOnPrivateChatMessageReceivedDelegates()->RemoveAll(this);
}
// ...
}
Congratulations! You have connected the widget with the private chat implementation.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.