I've been reading forum posts, answer posts and several wiki pages on replication (especially thorough: https://wiki.unrealengine.com/Replication), now I'm trying to make sure I use it correctly so I'm dissecting the Shooter example (as a starting point).
I would appreciate some feedback on if my understanding of how this works is correct - as outlined in my seven numbered comments for the example of the bExploded replicated bool in AShooterProjectile? I've put the ones I'm particularily shaky about in bold.
1. relevant part of AShooterProjectile constructor
2. Marked as Replicated so only server (e.g. ROLE_Authority) can modify it, and will be replicated to all clients (within NetCullDistanceSquare).
3. bExploded put in FRepLayout object as field that must be replicated
4. Calls OnImpact when projectile stops on server and all clients.
5. Called on server and clients, but Role == ROLE_Authority ensures only executes on server when bExploded
6. ReplicatedUsing means this is called when bExploded is set to true.
Would it also be called if set to false again - i.e. for any change of the replicated variable?
7. Called by OnImpact from server and OnRepExploded from clients to simulate damage and effect.
Sets bExploded to trye - client will also set it, but will be overwritten by server version (only when updated on server or periodically?)
Also, why is the explosion effect spawned on the server - surely needs only be spawned for clients?
I would appreciate some feedback on if my understanding of how this works is correct - as outlined in my seven numbered comments for the example of the bExploded replicated bool in AShooterProjectile? I've put the ones I'm particularily shaky about in bold.
1. relevant part of AShooterProjectile constructor
- bReplicates: enables replication
- bReplicateInstigator: replicates AActor that created AShooterProjectile? Correct? Why?
- bReplicateMovement: enables replication of AActor movement (i.e. position at any given time)
Code:
bReplicates = true; bReplicateInstigator = true; bReplicateMovement = true;
Code:
UPROPERTY(Transient, ReplicatedUsing=OnRep_Exploded) bool bExploded;
Code:
void AShooterProjectile::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const { Super::GetLifetimeReplicatedProps( OutLifetimeProps ); DOREPLIFETIME( AShooterProjectile, bExploded ); }
Code:
void AShooterProjectile::PostInitializeComponents() { Super::PostInitializeComponents(); MovementComp->OnProjectileStop.AddDynamic(this, &AShooterProjectile::OnImpact); // other stuff }
Code:
void AShooterProjectile::OnImpact(const FHitResult& HitResult) { if (Role == ROLE_Authority && !bExploded) { Explode(HitResult); DisableAndDestroy(); } }
Would it also be called if set to false again - i.e. for any change of the replicated variable?
Code:
void AShooterProjectile::OnRep_Exploded() { // trace impact trajectory (or use failsafe) Explode(Impact); }
Sets bExploded to trye - client will also set it, but will be overwritten by server version (only when updated on server or periodically?)
Also, why is the explosion effect spawned on the server - surely needs only be spawned for clients?
Code:
void AShooterProjectile::Explode(const FHitResult& Impact) { // apply damage and spawn explosion effect bExploded = true; }