I’ve been reading forum posts, answer posts and several wiki pages on replication (especially thorough: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums), 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.
- 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)
bReplicates = true;
bReplicateInstigator = true;
bReplicateMovement = true;
- Marked as Replicated so only server (e.g. ROLE_Authority) can modify it, and will be replicated to all clients (within NetCullDistanceSquare).
UPROPERTY(Transient, ReplicatedUsing=OnRep_Exploded)
bool bExploded;
- bExploded put in FRepLayout object as field that must be replicated
void AShooterProjectile::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const
{
Super::GetLifetimeReplicatedProps( OutLifetimeProps );
DOREPLIFETIME( AShooterProjectile, bExploded );
}
- Calls OnImpact when projectile stops on server and all clients.
void AShooterProjectile::PostInitializeComponents()
{
Super::PostInitializeComponents();
MovementComp->OnProjectileStop.AddDynamic(this, &AShooterProjectile::OnImpact);
// other stuff
}
- Called on server and clients, but Role == ROLE_Authority ensures only executes on server when bExploded
void AShooterProjectile::OnImpact(const FHitResult& HitResult)
{
if (Role == ROLE_Authority && !bExploded)
{
Explode(HitResult);
DisableAndDestroy();
}
}
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?
void AShooterProjectile::OnRep_Exploded()
{
// trace impact trajectory (or use failsafe)
Explode(Impact);
}
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?
void AShooterProjectile::Explode(const FHitResult& Impact)
{
// apply damage and spawn explosion effect
bExploded = true;
}