UE4 Networking Development Tips

UE4 Networking Development Tips


For more detail, you can visit Networking and Multiplayer. In this blog, I just share brief introduction and some tips in development.

1 Actor Role


For UE4 network project, Dedicated Server and Client are running same copy of codes. Programmer can use below roles to distinguish actors:

  • ROLE_SimulatedProxy:Locally simulated proxy of this actor.

  • ROLE_AutonomousProxy:Locally autonomous proxy of this actor.

  • ROLE_Authority:Authoritative control over the actor.

  • Tips: Notice that actors with ROLE_Authority are running at the dedicated server for network project.

2 RPC


  • Called on server and execute in client:

      UFUNCTION( Client );
      void ClientRPCFunction();
    
  • Called on client and execute in server:

      UFUNCTION( Server );
      void ServerRPCFunction();
    
  • Called on server and execute in all clients:

      UFUNCTION( NetMulticast );
      void MulticastRPCFunction();
    
  • Implementation of RPC function

      void SomeRPCFunction_Implementation()
      {
      }
    
  • Validation and Reliability

    Using keyword Reliable or UnReliable to determine the rpc is called on a reliable or unreliable channel. Using keyword WithValidation to assign validation function:

      UFUNCTION( Server, Reliable, WithValidation );
      void SomeRPCFunction( int32 AddHealth );
    	
      bool SomeRPCFunction_Validate( int32 AddHealth )
      {
         return true;                             
      }
    
  • Tips: Notice that if the validation function return false, the caller will be disconnected.

  • Tips: No all actor can call RPC function, only those actors directly or indirectly controlled (owned) by PlayerController can call RPC function effectively.

  • Tips: RPC function drived by UNetDriver::InternalProcessRemoteFunction.

3 Replication


  • Actor or ActorComponent : Controlled by property bReplicates or function SetIsReplicated

  • Properties: Using keyword Replicated in C++ and replicated properties should be add to GetLifetimeReplicatedProps;

      class ENGINE_API AActor : public UObject
      {
          UPROPERTY( replicated )
          AActor * Owner;
      };
    	
      void AActor::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const
      {
          DOREPLIFETIME( AActor, Owner );
      }
    
  • Tips: Order to call RepNotify function and RPC function cannot be promised.

4 Reconnection


UE4 build-in networking is not supporting reconnection mechanism. Developers should achieve two type of reconnections.

  • Reconnection for killing client process: relogin dedicated server again for all game logic.

  • Reconnection for heartbeat timeout: refresh all game logic when reconnected.

References

[1] Networking and Multiplayer

Tags: UE4 Network Vistied:
Share: Twitter Facebook LinkedIn