Hello. I’m totally confused with ue4 replication. I can’t understand the logic behind it. Please, take a look at this simple code:
*.h
UPROPERTY(Replicated, ReplicatedUsing = TestFunction)
int32 Test;
virtual void PlayerTick(float DeltaTime) OVERRIDE;
UFUNCTION()
void TestFunction();
*.cpp
void AMyProjectPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
if (Role == ROLE_Authority)
{
UE_LOG(LogTemp, Warning, TEXT("On server %s: %d"), *this->GetName(), Test);
Test++;
}
UE_LOG(LogTemp, Warning, TEXT("On Client %s: %d"), *this->GetName(), Test);
}
void AMyProjectPlayerController::TestFunction()
{
UE_LOG(LogTemp, Warning, TEXT("Notification on client %s: %d"), *this->GetName(), Test);
}
I made my controller replicated, I add Test variable in DOREPLIFETIME() macro.
When I run a single client as listen server, I have 2 lines in log - “On server…” and “On Client…” with Test changed. Notification don’t called on server, I know it, it’s how it should work.
When I run a single client with dedicated server I have only log - “On Client…” and Test is unchanged. But why??? It’s changed in PlayerTick() function in ROLE_Authority block and from here:
So, my single client runs on both client and server at the same time. Test is changed on server. Test is Replicated. Why can’t I see it on client? Why notification is not fired?
Next. I added
UFUNCTION(Reliable, Server, WithValidation)
void ServerFunction();
and call it in PlayerTick(). And everything is wonderful - now I can see Test changed on client and even notification is fired.
So, I hope someone can explain why my variable doesn’t replicates. Why I need to call server function for this from client (it’s a lot of unnecessary traffic). What’s the difference of ROLE_Authority and UFUNCTION(Server)?