I run into problems with the order in which replicated variables are updated in the client. Basically in the server variables A and B are changed, but in some client B arrives first, then A, causing problems, because those two things affect a related Actor, each other, or thing.
How do people solve these issues in UE4?
Unfortunately you can’t rely on the order of basic replication. At the lowest level in a function called ServerReplicateActors the connection order and the actor order can be jumbled up based on net priorities and possibly starvation of some clients.
So, how to handle this.
First, if you aren’t using OnRep functions to announce the change, then you really have to keep track of the multiple changes yourself. I’ve not typically tried to do anything that was important without an OnRep function.
With the OnRep put on each of the important values, the typical pattern is to have a common function that you call from both when you detect that both variables are ready.
OnRep_AChanged()
{
if (B has been changed)
DoABChangeThing()
}
OnRep_BChanged()
{
if (A has been changed)
DoABChangeThing()
}
It doesn’t seem that elegant, but it is somewhat common. You can always go the more heavy handed approach with an RPC. RPC order is guaranteed, but in this case you can find the right place on the server to call
ClientHereIsABTogether(valueA, valueB)
And then you are guaranteed to get what you need at the same time.
Can two variables be replicated at once, together? Not using RPC. Perhaps if they’re part of the same structure?
struct DTypeToReplicate
{
int value;
float fval;
}
...
UPROPERTY(Replicated)
DtypeToReplicate Var;
I believe that is also true, good point.
It is my understanding that a UStruct as a UProperty of a UObject would have all of its variables replicated atomically and with the one OnRep call have all the data you need.
You can run into bandwidth issues if you make your UStructs too big. It will need to come across in multiple packets and possibly slow packet processing. But for something infrequent/small, that would probably be a great way to do it.