I’m not entirely sure, but I think ReplicatedUsing doesn’t work with all variable types. It works fine with basic types like bool or int32, but in some cases I couldn’t make them work with more complex types, like pointers to class instances etc.
In such cases I just created workarounds with NetMulticast functions that would update the variable for all the clients, and it works fine, although I’m not sure which way is more efficient.
I thought the OnRep method was called as a side effect of a replicated variable getting updated but after some debugging, I can see that’s not the case. Is it necessary to manually call the OnRep method in C++? If so, what’s the purpose of coupling this method with the variable via ReplicatedUsing?
ReplicatedUsing is the equivalent however in c++ the callback function is only called on the Clients so if you want it to be called on the Server too then you need to call it explicitly.
Also keep in mind that the callback function is not called if the variable is, assumed by the Server, to already be set correctly on the Client.
Let’s say a bool defaults to false and the Server Set it to false then the default behavior is not to replicate as the Server already assume the variable is set to false on the Client.
Replicating pointers only work if the referenced object itself is replicated otherwise the reference is meaningless to the Client as the Client and Server pointer to the object has not been synchronized.
Is there anything that could cause this to not work when setting the replicated c++ variable via Blueprints? For example after moving the declaration of the variable to c++, but setting it in Blueprint, the OnRep method is not called