Download

RepNotify for arrays not triggered when modifying a contained value by reference

If you create an array of structs and set it to RepNotify, then you get an array element by reference and modify it using “Set members in struct”, the array element is correctly modified. However, it does not trigger the associated RepNotify function.

This makes sense because I doubt it’s efficient for UE4 to compare the values of a variable to see if they’ve changed. And since I’m modifying a value contained within the array by reference, I’m bypassing whatever trigger is used to see if the array itself has changed.

Is there any way for me to manually tell the array “You have updated” without touching the contents? I have many values in this array and I do not wish to actually change anything other than the one element I am updating (which was my main purpose for getting the element by reference). I can’t manually call my “OnRep” function, so can I manually modify some element of that array to say “You were updated, do what you gotta do”?

If not, I suppose I just need to write my own OnRep function and call it manually any time I update the array values. But I’d rather not have to do that.

For anybody who faces the same issue: the actual reason for this is that the actual replication IS DONE but the RepNotify is called only for the Uproperty changes (in this case it’s an array itself without any content - and it doesn’t change). It is the default behavior if you just declare UPROPERTY( ReplicatedUsing = OnRep …) and specify DOREPLIFETIME(Class, Array) in GetLifetimeReplicatedProps().

To make RepNotify be called each time the array’s content is changed you have to replace DOREPLIFETIME(Class, Array) with DOREPLIFETIME_CONDITION_NOTIFY(Class, Array, COND_…, REPNOTIFY_Always). Pay attention to the last parameter - REPNOTIFY_Always - it tells the network system that you want to receive RepNotifies every time the property or it’s content is changed (in other words the replication package for this property is received).