Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

Help me with Unreal Engine 5. I override PawnClientRestart which called on client when…

  1. User

    Help me with Unreal Engine 5. I override PawnClientRestart which called on client when pawn possessed and for some reason in standalon it called properly means it's called one time on possess but with dedicated server it is called twice on client, one time for old pawn and one time for new pawn. I thought to use OnRep_Controller to call some clients functions on possess but will it be also called on unpossess? What is a better sollution to call function on client when server call possess or unpossess?

  2. Assistant

    When working with networked multiplayer in Unreal Engine 5 (UE5), handling possession and ensuring that client-side functions are called appropriately can be a bit tricky due to the way replication and remote procedure calls (RPCs) work. Let's address your questions step by step and provide a solution that suits your needs.

    Why is PawnClientRestart Called Twice on the Client in a Dedicated Server Setup?

    In a dedicated server environment, replication can sometimes cause functions to be called more than once due to property updates and state synchronization between the server and clients. Specifically:

    • First Call: When the server possesses a new pawn, it replicates this change to the client. The client receives the new controller-pawn association, triggering PawnClientRestart on the new pawn.
    • Second Call: The old pawn might still be present on the client for a brief moment until the server tells the client to destroy or unpossess it. During this transition, PawnClientRestart might be called on the old pawn as it processes the change.

    Using OnRep_Controller to Detect Possession Changes

    Yes, OnRep_Controller is a reliable way to detect changes in possession on the client because it's called whenever the Controller property of a pawn changes:

    • On Possess: When the pawn gains a new controller (i.e., Controller becomes valid/non-null).
    • On Unpossess: When the pawn loses its controller (i.e., Controller becomes null).

    Here's how you can use OnRep_Controller:

    void APawnYourPawn::OnRep_Controller()
    {
        Super::OnRep_Controller();
    
        if (Controller)
        {
            // This is called on possession.
            OnPossessed();
        }
        else
        {
            // This is called on unpossess.
            OnUnpossessed();
        }
    }
    

    Better Solution to Call Functions on Client When Server Calls Possess or Unpossess

    Using OnRep_Controller is an effective method, but let's explore a more robust solution that ensures your client-side functions are called exactly when you need them, without unintended side effects.

    Option 1: Override PossessedBy and UnPossessed with RPCs

    Since PossessedBy and UnPossessed are called on the server, you can use RPCs to notify the client:

    void APawnYourPawn::PossessedBy(AController* NewController)
    {
        Super::PossessedBy(NewController);
    
        if (HasAuthority())
        {
            // Notify the client
            ClientOnPossessed();
        }
    }
    
    void APawnYourPawn::UnPossessed()
    {
        Super::UnPossessed();
    
        if (HasAuthority())
        {
            // Notify the client
            ClientOnUnpossessed();
        }
    }
    
    UFUNCTION(Client, Reliable)
    void ClientOnPossessed()
    {
        // This function runs on the client
        OnPossessed();
    }
    
    UFUNCTION(Client, Reliable)
    void ClientOnUnpossessed()
    {
        // This function runs on the client
        OnUnpossessed();
    }
    

    This method ensures that your OnPossessed and OnUnpossessed functions are called on the client precisely when possession changes occur on the server.

    Option 2: Use Replicated Variables with OnRep Functions

    If you prefer to avoid RPCs, you can create a replicated variable that tracks the possession state:

    // In your pawn class
    private:
        UPROPERTY(ReplicatedUsing=OnRep_IsPossessed)
        bool bIsPossessed = false;
    
    public:
        virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override
        {
            Super::GetLifetimeReplicatedProps(OutLifetimeProps);
            DOREPLIFETIME(APawnYourPawn, bIsPossessed);
        }
    
        void OnRep_IsPossessed()
        {
            if (bIsPossessed)
            {
                // Called on possession
                OnPossessed();
            }
            else
            {
                // Called on unpossess
                OnUnpossessed();
            }
        }
    
        virtual void PossessedBy(AController* NewController) override
        {
            Super::PossessedBy(NewController);
            bIsPossessed = true;
        }
    
        virtual void UnPossessed() override
        {
            Super::UnPossessed();
            bIsPossessed = false;
        }
    

    This method leverages Unreal's replication system to synchronize the possession state, calling OnRep_IsPossessed on clients when bIsPossessed changes.

    Recommendation

    Using RPCs (Option 1) is generally more immediate and gives you precise control over when client-side functions are called. This is particularly useful if you need the client to respond promptly to possession changes without waiting for property replication cycles, which can introduce slight delays.

    However, if your game can tolerate minimal delays and you prefer to stick with property replication, Option 2 is a clean and efficient approach.

    Summary

    • OnRep_Controller is called on both possess and unpossess events on the client. You can use it to detect possession changes.
    • For better control and immediate execution, using client RPCs (ClientOnPossessed, ClientOnUnpossessed) triggered from PossessedBy and UnPossessed on the server is recommended.
    • Alternatively, replicating a custom variable with an OnRep function provides a tidy solution that stays within the replication framework.

    Final Thoughts

    Choose the method that best fits your game's needs:

    • If immediate response and precision are critical, go with RPCs.
    • If you prefer simplicity and can handle minor replication delays, use replicated variables with OnRep functions.
    • Remember to handle potential edge cases, such as ensuring that the client is ready to receive RPCs (the connection is established) before they are called.

    Implementing one of these solutions should resolve the issue of extra calls to PawnClientRestart and give you reliable client-side possession handling in a dedicated server environment.