Help me with Unreal Engine 5. I override PawnClientRestart which called on client when…
- 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?
- 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
PawnClientRestartCalled 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
PawnClientRestarton 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,
PawnClientRestartmight be called on the old pawn as it processes the change.
Using
OnRep_Controllerto Detect Possession ChangesYes,
OnRep_Controlleris a reliable way to detect changes in possession on the client because it's called whenever theControllerproperty of a pawn changes:- On Possess: When the pawn gains a new controller (i.e.,
Controllerbecomes valid/non-null). - On Unpossess: When the pawn loses its controller (i.e.,
Controllerbecomes 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_Controlleris 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
PossessedByandUnPossessedwith RPCsSince
PossessedByandUnPossessedare 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
OnPossessedandOnUnpossessedfunctions are called on the client precisely when possession changes occur on the server.Option 2: Use Replicated Variables with
OnRepFunctionsIf 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_IsPossessedon clients whenbIsPossessedchanges.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_Controlleris 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 fromPossessedByandUnPossessedon the server is recommended. - Alternatively, replicating a custom variable with an
OnRepfunction 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
OnRepfunctions. - 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
PawnClientRestartand give you reliable client-side possession handling in a dedicated server environment. - 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