Download

Sync variable client-server

Hello. I’ve read all I can found on this topic. I’ve implemented replication in blueprints. Now I want to do the same with C++ but I can’t, something wrong.
I have a controller with


UPROPERTY(Replicated)
FVector Position;

UFUNCTION(Reliable, Server, WithValidation)
void MovePawn();

I’m running two clients with PIE. As I understand, when I’ll call MovePawn() on client it will run on server, right?
Inside MovePawn_Implementation() I change Position. Again, as I understand, since Position is Replicated it should be automatically updated on client. But when I’m trying to get it in PlayerTick() it’s always the same and never changes.

Did you set the replication condition in your Cpp class file?

It’s in the function : void Namespace::GetLifetimeReplicatedProps(TArray<class FLifetimeProperty>& OutLifetimeProps) const

You can look in ShooterGame template, you can see some example.

Check there or there

Yes, I set it.

Have you tried to set a notification on the replication process for this variable.
This will allow you to check is the replication has worked on the client side.

Can you share the rep function ?

Did you check the value on your server side and validate that it is really change and not override by another function after the change?

as Elvince said you can try something like that

H)


UPROPERTY(ReplicatedUsing=OnRep_Position)
FVector Position;

CPP)


void YOURNAMESPACE::OnRep_Position()
{
	UE_LOG(LogGameMode, Warning, TEXT("%s] OnRep_Position: %i"), *this->GetName(), *Position.ToString());
}


void YOURNAMESPACE::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	// Replicate to everyone
	DOREPLIFETIME(YOURNAMESPACE, Position);
}

you might have to put


DEFINE_LOG_CATEGORY_STATIC(LogGameMode, Log, All);

after your #include

then you should get something like [0_PlayerController] OnRep_Position: x:000,y:000,z:000 in your output log

Thank you for help. My problem was that in my calculations (that was on server) was used variable that I change on client. I made this variable as Replicated, but again it didn’t work. So, new question: property replicates only from server to client and not vice versa?

Btw, how UE_LOG should work? I defined log, I even tried LogTemp (build-in name), but I can’t see any messages - not on the screen and console (~).

Ok, I’m almost done. I found UE3 info about replication - it says:

And I believe same thing goes to UE4. If you want to get property from server on client simply make it [Replicated]. If you want to get variable from client on server - make a function with [Server] declaration and pass necessary parameters.
But I still have an issue. I have Position property, it updates on server, in PlayerTick() I set Pawn->SetActorLocation(Position). But it works only for my client, all others don’t see pawn change. Why? Player controller should call PlayerTick() and this Position property should be visible on all machines, isn’t it?

If you use “warning” as log type you should be seeing a yellow warning message in your output log ( window-> output log in your editor) and your logfile under …/saved/logs/

make sure to set bReplicateMovement = true in your pawn. that way the position of your pawn should be replicated automatically.