Basic networking question

I’ve been trying to create a very simple multiplayer environment which basically sends some information between a host and client. Now I’ve got two computers to join a game with each other and some objects replicated between them, however I can’t seem to get my head around replication so I’ve gone right down the the basics.

I want to send some variables, or one variable for now, across the network but I’m not entirely sure whats going on under the hood to make this work. Now I know there are a couple of ways things are send across the network according to this, but I can’t seem to get it working as intended.

For now I have in the header



int32 nSomeLocalVar;

UFUNCTION(reliable, server, WithValidation)
		void SetValueServer(int32 nVar);

and in the .cpp



// In some key pressed fucntion
if (Role < ROLE_Authority)
	{
		// Client calls server
		SetValueServer(3);
	}
	else
	{
		// We are not the client
	}
////////////////////////
bool AOnlineCharacter::SetValueServer_Validate(int32 nVar)
{
	return true;
}

void AOnlineCharacter::SetValueServer_Implementation(int32 nVar)
{
	if (GEngine)
	{
		FString OutputVar = "I've receieved: ";
		OutputVar .AppendInt(nVar);
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *(OutputVar ));
	}
        // Now here, I want to make a local sever variable == nVar
        // nSomeLocalVar = nVar;
}

Now the server function is called on my machine if the client machine presses a the test key, but I can’t seem to make a local variable == to the one passed in from the client? I’ve also tried replicating the variables with GetLifetimeReplicatedProps(…) {…} but I can’t seem to get that working either. Anyone got any insights?

Still struggling on getting my head around networking. It seems after following some tutorials also, I am unable to get variables to replicate with clients/hosts using the


UPROPERTY(Replicated)
		int32 someVariable;
....
DOREPLIFETIME(AOnlineCharacter, someVariable);

Is there any more word on official networking documentation?

First of all - try to avoid using RPC calls - do it only where there is no other way for implement Your functionalities - cost of use RPC calls is much more bigger than casual replication.
Anyway it seems that RPC calls works properly, but Your value wasn’t replicated. There might be few reasons:

  • role of an Actor that owns this variable has no RemoteRole ( RemoteRole = ROLE_None ). Actor with no role won’t be considered for replication. You want to change it probaby for RemoreRole = ROLE_SimulatedProxy - it means that original Actor will be on Server, and Clients will get Copy from Server
  • AActor funcion IsNetRelevantFor returns false. Check conditions of this function, if You aren’t satisfied with its results, You can override this function or set bAlwaysRelevant on True

Is bReplicates set to true in the AOnlineCharacter?

Did you see the networking tutorials we just posted? Those should have some useful information for C++ and Blueprint replication work:

What type of behavior are you seeing with your variable replication example? The value doesn’t change on the clients?

As GreenEyed suggested, you should make sure your actor itself has the bReplicates value set to true. Past that, it looks like you have the variable set up correctly. One thing to note is that you don’t want to directly set replicated variables on the client machines. If the value of that replicated variable changes on the server, then it will propagate that change out to the clients. If you want the client to initiate a change to that value, then you should use the server function to tell the server to change the value, which will in turn then replicate to all the clients.

I think I got my head around the idea after a few days.

If I create a class (say a gamestate) and set some of the UPROPERTY to replicate, then when I make these changes on the server side, the client can look these up and use them as almost a way for the server to communicate with the clients?

And then the client will use the functions to call/set values on the server side of things?

Is there no simpler way of communication between two players however, like a simple SendMessage(something, ControllerToSendTo) functionality which will send information from a given controller to another?