Need help to understand replication better

Hello guys!

You see, I know that OnRep are called automatically in the clients, when a replicated property value is changed, and you shouldn’t call an OnRep manually, unless you want execute it in the server.

BUT I have this class, and I have changed the value of the replicated variable (I verified the Role of the machine and change its value in the server), and no OnRep was called.

I had to execute the OnRep manually to make it work. Shouldn’t the OnRep be called automatically?

I didn’t understand very well why it didn’t trigger the callback, can someone explain to me the reason, and what I should change to make it work without a manual call?

Here are the snippets:

.h

.cpp

Thanks!

OnRep is called on the client when a replication update is received. You call OnRep on the server manually. If it is also not called on the client, the reason is probably that the actor doesn’t exist yet on the client when BeginPlay is called on the server (and/or ViewTarget already has the same value as MainCamera).

Thanks for your reply!

I did watch more carefully the execution of my code and I can say that, if I comment the OnRep_ call inside the if, the replication happens only for the client who joined, not for the client who hosted the session. Looking for an answer in the internet, I found this RepNotify from c++ confusion. - #10 by Irascible, and I learned that OnRep_ isn’t called automatically for the local cilent that is running in the same process as the server (the case of a listen server), its only called in the connected clients. So its expected to call manually the OnRep_ in the server, after verifying its authority and its nature as a listen server.

Clients do not host sessions. The server hosts, clients join, and replication only happens from server to client. You can call your OnRep manually anywhere you want, it is just a function, but it only gets called automatically on a client when a replication update is received from the server.

I don’t really understand what your problem is specifically, but judging from your previous statements this might clear up some confusion for you: The server can host a game and clients can connect to that server. If it is a listen server, there can also be a player on the server, but that player is then authoritative, there is never a “client that hosts the session”. Clients receive replicated values from the server (in your case the value of ViewTarget). So it makes sense that the OnRep function is not called for the player on the server, because the value on the server is what is being sent to the clients in the first place (although this might be different in Blueprint, it is the case in C++). Consequently, you have to call SetViewTarget(ViewTarget); on the server manually, which you do by calling the OnRep function manually.

1 Like

Understood. Thanks for the explanation! :slight_smile: