Hi guys,
I have to code the multiplayer part of our project, for that I have read several posts and documentations, and now I would like to test what I have read.
First step : Replication of a simple variable. I would like to sync a simple int32 from the server to the client.
I have created a new test project based on the third person template. I have tested the generated code and it’s works fine. I see both players, sync on the server and on the client.
To test variable replication I have added this in my TestNetworkCharacter.h
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Replicated, Category = Testing)
int32 Test;
And this to my TestNetworkCharacter.cpp
void ATestNetworkCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ATestNetworkCharacter, Test);
}
I have added #include “Net/UnrealNetwork.h” in the TestNetwork.h
To test if the client receive the new value when the server change it, I have added this 2 actions in the .cpp.
This to change the value
void ATestNetworkCharacter::Action2()
{
if (Role == ROLE_Authority)
Test = 2;
}
And this to log the current value
void ATestNetworkCharacter::Action1()
{
FString MyString = FString::FromInt(Test);
if (Role == ROLE_Authority)
{
UE_LOG(LogSomething, Log, TEXT("Server %s"), *MyString);
}
else
{
UE_LOG(LogSomething, Log, TEXT("Client %s"), *MyString);
}
}
Test procedure :
- I launch the server and next the client.
- I hit the key to execute Action1() on server and next on the client, this is to log the current value of my variable.
- On the server I hit the key to execute Action2(), this normaly change the value to 2, and replicated it to the client.
- I repeat the step 2 to log the new value.
But when I read the server’s log I see
[2014.04.21-14.47.18:015][315]LogSomething: Serveur 1
[2014.04.21-14.47.24:864][579]LogSomething: Serveur 2
And I see this in the client’s log
[2014.04.21-14.47.08:804][233]LogSomething: Client 1
[2014.04.21-14.47.28:621][981]LogSomething: Client 1
The value is still on 1 on the client. Replication dont have to change the value of my variable to 2 on the client ?
Where is the error ?
Thanks you for your reply.