I was genuinely suprised when I didn’t see the “Server: OnRep_ReplicatedVar” message at 7:50. Despite Kaan saying multiple times that the server does NOT call OnRep, at least within c++. We have to call it manually. That’s my biggest take away. I’m glad he harped on it. As an aside, I tested it in Blueprint. If OnRep() is created in BP then the server does indeed call OnRep automatically.
I want to write down what I see, in pseudo code, to paint a clear picture.
Setup:
- MyBox: SetReplicates(true), SetReplicateMovement(true), Tick{SetVarText("ReplicatedVar: " + ReplicatedVar)} in BP.
- ReplicatedVar: UPROPERTY(
ReplicatedReplicatedUsing=OnRep_ReplicatedVar) // callback, GetLifetimeReplicatedProps( DOREPLIFETIME() ). - MyBox.Shape_Cube > ComponentReplicates=true // to increase accuracy?
Logic:
- BP event: press the Space bar, event started
- BP: if (hasAuthority) {proceed} else {nothing} // are you the server?
- BP: set ReplicatedVar to random int on server
- Replication: Replicate the number change to clients // it seems this happens first and regardless of the callback
- BP: MyBox.Tick{SetVarText("ReplicatedVar: " + ReplicatedVar)} // updates the textBox
- CPP: Trigger the callback handler function, OnRep_ReplicatedVar() for the clients
- Replication: Clients and Clients only run OnRep().
- OnRep() prints “Client %d OnRep_ReplicatedVar”, // a replicated message to even the server
- BP: We have the Server Call OnRep_ReplicatedVar(). Prints “Server: OnRep_ReplicatedVar” and sets the location of MyBox.
- Replication: the message and location are replicated to the clients
UE’s Replication (proper noun)
UEDocs: This is the name of the customizable and abstract system UE uses to sync clients and servers.
ComponentReplicates=true
it made the location replication more accurate. It is generally used for ‘important things’ like player movement, inventory, health, abilities, and stats. I think it’s for customizing the replication of a component separate from the parent.
DOREPLIFETIME and GetLifetimeReplicatedProps()
UEDocs “Returns the properties used for network replication, this needs to be overridden by all actor classes with native replicated properties”. There’s other versions like DOREPLIFETIME_CONDITION().
#define DOREPLIFETIME(c,v) DOREPLIFETIME_WITH_PARAMS(c,v,FDoRepLifetimeParams())
/** This macro is used by nativized code (DynamicClasses), so the Property may be recreated. */
#define DOREPLIFETIME_DIFFNAMES(c,v, n) \
{ \
static TWeakFieldPtr<FProperty> __swp##v{}; \
const FProperty* sp##v = __swp##v.Get(); \
if (nullptr == sp##v) \
{ \
sp##v = GetReplicatedProperty(StaticClass(), c::StaticClass(), n); \
__swp##v = sp##v; \
} \
for ( int32 i = 0; i < sp##v->ArrayDim; i++ ) \
{ \
OutLifetimeProps.AddUnique( FLifetimeProperty( sp##v->RepIndex + i ) ); \
} \
}
I don’t understand other than we have to do it. I think that’s intentional. It’s abstracted away, but still customizable. Network Tips and Tricks for general tips and tricks.
UEDocs " The GetLifetimeReplicatedProps
function is responsible for replicating any properties you designate with the Replicated
specifier, and enables you to configure how a property will replicate. If at any time you add more properties that need to be replicated, you must add them to this function as well."