1.Actor Replication
2.Property Replication
3.Function Call Replication
4.Actor Component Replication
5.Generic Subobject Replication
1.Detailed Actor Replication
1.Actor Replication
2.Property Replication
3.Function Call Replication
1 void AShooterCharacter::FlyDown() 2 { 3 if (this->Role < ROLE_Authority) 4 { 5 ServerFlyDown(); 6 } 7 if (!GetCharacterMovement()->IsMovingOnGround() && !GetCharacterMovement()->IsFalling()) 8 { 9 GetCharacterMovement()->SetMovementMode(MOVE_Falling); 10 } 11 }
UENUM() enum ENetRole { /** No role at all. */ ROLE_None, /** Locally simulated proxy of this actor. */ ROLE_SimulatedProxy, /** Locally autonomous proxy of this actor. */ ROLE_AutonomousProxy, /** Authoritative control over the actor. */ ROLE_Authority, ROLE_MAX, };
1 void UActorChannel::ProcessBunch( FInBunch & Bunch ) 2 { 3 //...... 4 // Owned by connection's player? 5 UNetConnection* ActorConnection = Actor->GetNetConnection(); 6 if (ActorConnection == Connection || (ActorConnection != NULL && ActorConnection->IsA(UChildConnection::StaticClass()) && ((UChildConnection*)ActorConnection)->Parent == Connection)) 7 { 8 RepFlags.bNetOwner = true; 9 } 10 //...... 11 }
1 UFUNCTION( Client ); 2 void ClientRPCFunction();
Server
1 UFUNCTION(reliable, server, WithValidation) 2 void ServerFlyUp();
NetMulticast
1 UFUNCTION( NetMulticast,unreliable ); 2 void MulticastRPCFunction();
1 void AShooterCharacter::ServerFlyUp_Implementation() 2 { 3 FlyUp(); 4 }
1 void AShooterCharacter::FlyUp() 2 { 3 if (this->Role < ROLE_Authority) 4 { 5 ServerFlyUp(); 6 } 7 //implement character fly up 8 //....... 9 }
1 bool AShooterCharacter::ServerFlyUp_Validate() 2 { 3 return true; 4 }
4.Actor Component Replication
AActorComponent::SetIsReplicated(true)
1 ACharacter::ACharacter() 2 { 3 // Etc... 4 5 CharacterMovement = CreateDefaultSubobject<UMovementComp_Character>(TEXT("CharMoveComp")); 6 if (CharacterMovement) 7 { 8 CharacterMovement->UpdatedComponent = CapsuleComponent; 9 CharacterMovement->GetNavAgentProperties()->bCanJump = true; 10 CharacterMovement->GetNavAgentProperties()->bCanWalk = true; 11 CharacterMovement->SetJumpAllowed(true); 12 CharacterMovement->SetNetAddressable(); // Make DSO components net addressable 13 CharacterMovement->SetIsReplicated(true); // Enable replication by default 14 15 } 16 }
Replicates
SetIsReplicated
5.Generic Subobject Replication
1 //ReplicatedSubobject.h 2 UCLASS() 3 class UReplicatedSubobject : public UObject 4 { 5 GENERATED_UCLASS_BODY() 6 7 public: 8 9 UPROPERTY(Replicated) 10 uint32 bReplicatedFlag:1; 11 12 virtual bool IsSupportedForNetworking() const override 13 { 14 return true; 15 } 16 };
1 //ReplicatedSubobject.cpp 2 UReplicatedSubobject::UReplicatedSubobject(const class FPostConstructInitializeProperties& PCIP) 3 : Super(PCIP) 4 { 5 } 6 7 void UReplicatedSubobject::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const 8 { 9 DOREPLIFETIME(UReplicatedSubobject, bReplicatedFlag); 10 }
1 UCLASS() 2 class AReplicatedActor : public AActor 3 { 4 GENERATED_UCLASS_BODY() 5 6 public: 7 8 virtual void PostInitializeComponents() override; 9 virtual bool ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) override; 10 11 /** A Replicated Subobject */ 12 UPROPERTY(Replicated) 13 UReplicatedSubobject* Subobject; 14 15 private: 16 };
1 #include "ReplicatedActor.h" 2 #include "UnrealNetwork.h" 3 #include "Engine/ActorChannel.h" 4 AReplicatedActor::AReplicatedActor(const class FPostConstructInitializeProperties& PCIP) 5 : Super(PCIP) 6 { 7 bReplicates = true; 8 } 9 10 void AReplicatedActor::PostInitializeComponents() 11 { 12 Super::PostInitializeComponents() 13 14 if (HasAuthority()) 15 { 16 Subobject = NewObject<UReplicatedObject>(this); 17 } 18 } 19 20 bool AReplicatedActor::ReplicateSubobjects(class UActorChannel *Channel, class FOutBunch *Bunch, FReplicationFlags *RepFlags) 21 { 22 bool WroteSomething = Super::ReplicateSubobjects(Channel, Bunch, RepFlags); 23 24 if (Subobject != nullptr) 25 { 26 WroteSomething |= Channel->ReplicateSubobject(Subobject, *Bunch, *RepFlags); 27 } 28 29 return WroteSomething; 30 } 31 32 void AReplicatedActor::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const 33 { 34 DOREPLIFETIME(AReplicatedActor, Subobject); 35 }
1.Detailed Actor Replication
AActor::NetUpdateFrequency
AActor::PreReplication
AActor::bOnlyRelevantToOwner
AActor::IsRelevancyOwnerFor
AActor::IsNetRelevantFor
UChannel::ReplicateActor
文章来源: 深入浅出UE4网络