Client to server?
This works for us:
MyPlayerState.h:
FString MyString;
UFUNCTION()
void SetMyString(FString NewMyString);
UFUNCTION(Reliable, Server, WithValidation)
virtual void ServerSetMyString();
bool ServerSetMyString_Validate();
void ServerSetMyString_Implementation();
UFUNCTION(Reliable, Client)
virtual void ClientRequestMyStringChunk(int32 ServerMyStringLen);
void ClientRequestMyStringChunk_Implementation(int32 ServerMyStringLen);
UFUNCTION()
void RequestMyStringChunk();
FTimerHandle TimerHandle_RequestMyStringChunk;
UFUNCTION(Reliable, Server, WithValidation)
virtual void ServerSendMyStringChunk(const FString& MyStringChunk);
bool ServerSendMyStringChunk_Validate(const FString& MyStringChunk);
void ServerSendMyStringChunk_Implementation(const FString& MyStringChunk);
MyPlayerState.cpp:
void AMyPlayerState::SetMyString(FString NewMyString)
{
// Set my string.
MyString = NewMyString;
// If I'm client & MyString isn't empty, tell server I have it.
if ((Role < ROLE_Authority) && !MyString.IsEmpty())
{
ServerSetMyString();
}
}
bool AMyPlayerState::ServerSetMyString_Validate()
{
return true;
}
void AMyPlayerState::ServerSetMyString_Implementation()
{
// Client has told me it has set MyString.
// Clear server version and ask client to start sending first chunk.
MyString.Empty();
ClientRequestStringChunk(0);
}
void AMyPlayerState::ClientRequestMyStringChunk_Implementation(int32 ServerMyStringLen)
{
static int32 MaxChunkSize = 512; // NOTE: 512 could be anything < 1024
FString MyStringChunk;
// server has a MyString that as long as ours - tell server its done.
if (ServerMyStringLen >= MyString.Len())
{
MyStringChunk = TEXT("END");
}
// Not enough string to send an entire chunk - send what we can.
else if (ServerMyStringLen + MaxChunkSize >= MyString.Len())
{
MyStringChunk = MyString.Mid(ServerMyStringLen, (MyString.Len() - ServerMyStringLen));
}
// Send an entire chunk.
else
{
MyStringChunk = MyString.Mid(ServerMyStringLen, MaxChunkSize);
}
ServerSendMyStringChunk(MyStringChunk);
}
void AMyPlayerState::RequestMyStringChunk()
{
// Ask client to start sending another chunk
ClientRequestStringChunk(MyString.Len());
}
bool AMyPlayerState::ServerSendMyStringChunk_Validate(const FString& MyStringChunk)
{
return true;
}
void AMyPlayerState::ServerSendMyStringChunk_Implementation(const FString& MyStringChunk)
{
// client has told us we have whole string.
// Time to do something with it.
if (MyStringChunk == TEXT("END"))
{
}
else
{
// We have another chunk of string.
// Add it to our version.
MyString += MyStringChunk;
// Delay request for next string chunk to prevent flooding.
SETTIMERH(TimerHandle_RequestMyStringChunk, AMyPlayerState::RequestMyStringChunk, 0.05f, false);
}
}
We send condensed JSON formatted strings from client to server using this method.
Easy to send it back, since arrays replicate
You don’t have to use player state - any owner actor will do.
e.g PlayerController
Hope that helps.