# Movement replication works on client, but not on host

**URL:** <https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [October 29, 2022, 7:01pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717 "2022-10-29T19:01:42Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 29, 2022, 7:01pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/1 "2022-10-29T19:01:42Z")

</div>

I set up a basic C++ unreal project to iron out some troubles I have with multiplayer.

> **[GitHub - rubenmoor/TutorialMPBasics: Beginner-friendly multiplayer setup for...](https://github.com/rubenmoor/TutorialMPBasics)**
>
> Beginner-friendly multiplayer setup for Unreal Engine/C++ - GitHub - rubenmoor/TutorialMPBasics: Beginner-friendly multiplayer setup for Unreal Engine/C++

Now in this most basic of all multiplayer projects, I got everything to work, BUT

Regardless how I launch (two independent games, or PIE, or PIE with run-under-on-process), the hosting window doesn’t show the movement of the pawn of the connected client. The client window does show the replicated movement of the host pawn.

What am I doing wrong? Is this a bug? I feel, at this basic stage (movement replication, activated by ticking the box) I shouldn’t actually have to bother, whether or not I am executing server or client code. But I might be wrong.

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 3:39pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/2 "2022-10-31T15:39:48Z")

</div>

bumping this.

Any opinions on this one? What can cause different behavior in the host and client window? I am only using the `replicate` check box, no other logic.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 4:26pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/3 "2022-10-31T16:26:29Z")

</div>

if fails on join session in the file mygameinstance.cpp on line 45

GetSubsystem()-\>JoinSession(LPC, [this, LPC] (FName SessionName, EOnJoinSessionCompleteResult::Type Result)  
{};

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 4:38pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/4 "2022-10-31T16:38:53Z")

</div>

is that your result from running the example project?

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/1/4/3149dc84f147adcb5132df51f341a557440b8f1c.jpeg)

This is what I get, when I launch this twice (hitting the `launch.bat` twice).

PIE works fine for me, too.

I then click on one of the windows and can move the respective Pawn with `A` and `D` key.

Moving the host pawn replicates to the client. (see left window below)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/4/0/840d3aab484c57ad49269607c384dbedf599e65e.jpeg)

But moving the client pawn doesn’t replicate to the host (in the right window, the right pawn has moved, but in the left windows, the right pawn is still at its starting position)

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 4:40pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/5 "2022-10-31T16:40:10Z")

</div>

let me push my code to make sure we are actually running the same code … EDIT: done [commit: c9e3caf0](https://github.com/rubenmoor/TutorialMPBasics/commit/c9e3caf07f803333e7c3beb5fc47b7c770bb3aa6)

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 4:44pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/6 "2022-10-31T16:44:53Z")

</div>

No, I tried a packed version first, then did a version from within the editor. The bat file is very specific to your pc drive config.

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 4:46pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/7 "2022-10-31T16:46:59Z")

</div>

of course … it has all my paths. That is a bit annoying for quick testing

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 4:52pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/8 "2022-10-31T16:52:03Z")

</div>

Running Standalone with “Number of Players” 2, works for me, too.

Be ware that one first clicks inside the window to capture the mouse and then on the host or join button. Clicking twice on the join button already causes problems.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 4:55pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/9 "2022-10-31T16:55:53Z")

</div>

Ok seems that it did connect after a couple of tries. I’ll have to look into how your possession logic works. Are you using agamemode PostLogin to spawn and posses your clients?

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 4:56pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/10 "2022-10-31T16:56:46Z")

</div>

No. I just put Player Start Actors and I have a default pawn set. That’s it. Trying to be minimal.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 5:01pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/11 "2022-10-31T17:01:45Z")

</div>

Won’t work. You need to make a custom game mode based on AGameModeBase and override OnPostLogin.  
Once OnPostLogin is called it will give you the new players controller.

You need to get a pointer to UWorld probably best through UGameplayStatics and spawn your player character at the start position (getActorofClass → startpoisiton → getTransform pass in the loc & rot).

Pass in the transform (with scale (1,1,1)) and once it spawns get the passed in controller to posses the newly spawned class. Then you will get control of it in MP

And set default pawn to none in your gamemode

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 5:38pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/12 "2022-10-31T17:38:51Z")

</div>

I overwrote `PostLogin` like this:

```auto
UCLASS()
class TUTORIALMPBASICS_API AMyGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

protected:
	// event handlers
	virtual void OnPostLogin(AController* NewPlayer) override;
	virtual void BeginPlay() override;

	// implementing my own default pawn class, to avoid auto spawning
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	TSubclassOf<class AMyPawn> MyDefaultPawnClass;
};

```

```auto
void AMyGameModeBase::OnPostLogin(AController* NewPlayer)
{
	Super::OnPostLogin(NewPlayer);

	if(!IsValid(MyDefaultPawnClass))
	{
		return;
	}
	
	UWorld* World = GetWorld();

	TArray<AActor*> Starts;
	UGameplayStatics::GetAllActorsOfClass(World, APlayerStart::StaticClass(), Starts);

	UE_LOG(LogNet, Warning, TEXT("%s: OnPostLogin: %d/%d"), *GetFullName(), GetNumPlayers(), Starts.Num())
	
	APlayerStart* Start = Cast<APlayerStart>(Starts[GetNumPlayers() - 1]);
	AMyPawn* Pawn = World->SpawnActor<AMyPawn>(MyDefaultPawnClass, Start->GetActorTransform());
	NewPlayer->Possess(Pawn);
}

void AMyGameModeBase::BeginPlay()
{
	Super::BeginPlay();
	
	if(!IsValid(MyDefaultPawnClass))
	{
		UE_LOG(LogGameMode, Error, TEXT("%s: DefaultPawnClass null"))
	}
}

```

Producing the exact same result. In the host window, the client pawn won’t move. In the client window, both host and client pawn move, when I hit A or D in their respective window.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/5/0/4503c093cd1ff7d0e2a7eb6b82f815fdc241825a.jpeg)

Note that possessing of my pawns seemingly worked fine, even with the old code (relying on `DefaultPawnClass` and auto-spawn).

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 5:50pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/13 "2022-10-31T17:50:23Z")

</div>

Yes the part i mentioned works as intended you forgot to add any form of replication into your pawn

You need to implement

AActor::GetLifetimeReplicatedProps for your pawn and pass in what variables you want to replicate.

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 5:55pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/14 "2022-10-31T17:55:36Z")

</div>

Setting “replicate” and “replicate movement” in the BluePrint of the Pawn isn’t enough?

Also note that some sort of replication is going on. At least, I believe so. In the client window, I can see the movement of the host pawn AND the client pawn.

Once I deactivate “replicate movement”, I can see both balls in both windows, but the movement only of the locally controlled ball - as expected.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 6:18pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/15 "2022-10-31T18:18:29Z")

</div>

You are only changing the variable on client or server for velocity. If client you need to call a ufunction with the server macro and reliable. You need to check the logic if it’s authoritive (server) or not (client)  
if client call server update function if server just update the velocity

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 6:19pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/16 "2022-10-31T18:19:34Z")

</div>

I slowly come to the conclusion that the behavior I see is intended.

“Replicate movement” is specifically meant to work together with the Movement Component, i.e. using `ACharacter` rather than `APawn` to efficiently manage movement based on user input.

If I set my pawn to move (on every tick) given some velocity, independent of user input, both pawns move in both windows, i.e. they are replicated (by the power of checking “replicate” in the blueprint) including their location. This is with “replicate movement” unchecked.

* * *

Just adding custom movement to my pawn w/o the movement component has two consequences:

- my custom movement doesn’t get replicated, and
- with “replicate movement” checked, the host movement _does_ get replicated but the client movement doesn’t and it shouldn’t: On the host, I have to evaluate the user input of the client to move the client-controlled pawn and then replicate that.

Thanks @3dRaven for your help!

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [October 31, 2022, 6:26pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/17 "2022-10-31T18:26:59Z")

</div>

pawn .h

```auto

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"

UCLASS()
class TUTORIALMPBASICS_API AMyPawn : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	AMyPawn();

protected:
	// VisibleAnywhere on properties that are pointers to UObjects (like this one) does allow editing of its properties;
	// If you put "EditAnywhere" instead, you could change the type of Body (e.g. from UStaticMeshComponent to
	// USplineComponent), which isn't what you usually want
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	TObjectPtr<UStaticMeshComponent> Body;
	
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Replicated)
	FVector Velocity = FVector::Zero();

public:	
	void AccelerateLeft();
	void AccelerateRight();

	UFUNCTION(Server, reliable)
	void ServerAccelerateLeft();

	UFUNCTION(Server,reliable)
	void ServerAccelerateRight();

	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	//virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

};

```

cpp

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyPawn/MyPawn.h"
#include "Net/UnrealNetwork.h"

// Sets default values
AMyPawn::AMyPawn()
{
 	// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	SetReplicates(true);
	SetReplicateMovement(true);
	Body = CreateDefaultSubobject<UStaticMeshComponent>(FName(TEXT("Body")));
	SetRootComponent(Body);
}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
}

void AMyPawn::AccelerateLeft()
{
	if (GetLocalRole() == ROLE_Authority) {
		Velocity += FVector(0, -10, 0);
	}
	else {
		ServerAccelerateLeft();
	}
}

void AMyPawn::AccelerateRight()
{
	if (GetLocalRole() == ROLE_Authority) {
		Velocity += FVector(0, 10, 0);
	} else {
		ServerAccelerateRight();
	}
}

void AMyPawn::ServerAccelerateRight_Implementation() {
	AccelerateRight();
}

void AMyPawn::ServerAccelerateLeft_Implementation() {
	AccelerateLeft();
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{

	Super::Tick(DeltaTime);
	//if (GetLocalRole() == ROLE_Authority) {
		SetActorLocation(GetActorLocation() + Velocity * DeltaTime);
		const float Dampening = 10.;
		Velocity = Velocity.Length() < Dampening * DeltaTime ? FVector::Zero() : Velocity + Velocity.GetUnsafeNormal() * -Dampening * DeltaTime;
	//}
}

void AMyPawn::GetLifetimeReplicatedProps(TArray< FLifetimeProperty >& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
	DOREPLIFETIME(AMyPawn, Velocity);
}

```

Not a fan of updating parameters on tick. It can cause desync between client and server

---

<div class="post-metadata">

**Author:** ![rubm123](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rubm123/32/476292_2.png) [@rubm123](https://forums.unrealengine.com/u/rubm123)\
**Post date:** [October 31, 2022, 9:13pm UTC](https://forums.unrealengine.com/t/movement-replication-works-on-client-but-not-on-host/679717/18 "2022-10-31T21:13:49Z")

</div>

Thanks a lot!

This is my code that finally works:

[AMyPawn](https://github.com/rubenmoor/TutorialMPBasics/blob/main/Source/TutorialMPBasics/Private/MyPawn/MyPawn.cpp), not much going on there. Only

- Replicate Movement: false, no need to replicate location or anything
- Replicated property: velocity, this together with the tick function takes care of correct movement

> Not a fan of updating parameters on tick. It can cause desync between client and server

This is valuable advice indeed. As this is just a multiplayer learning experience, I simply removed the dampening from the tick function such that velocity only ever changes when I hit a button. If I were to implement dampening and I just had to change the velocity in the tick function, I would chose _not_ to replicate `velocity` and instead, I would use a client rpc to make sure `AccelerateLeft` and `AccelerateRight` get executed on the client.

And [MyPlayerController.h](https://github.com/rubenmoor/TutorialMPBasics/blob/main/Source/TutorialMPBasics/Public/Modes/MyPlayerController.h) and [MyPlayerController.cpp](https://github.com/rubenmoor/TutorialMPBasics/blob/main/Source/TutorialMPBasics/Private/Modes/MyPlayerController.cpp)

Two design choices here:

- I created an enum for all the actions. This is unfortunately redundant with unreals action binding system, but I use the `EAction` enum in `SetupInputComponent` to define only once what will happen at both client _and_ server. This way, I can’t accidently move left on the client and right on the server due to some copy-paste mishap
- `BindAction` then takes care of registering both cases, server and client. This implies use of closures, otherwise I can’t use the `EAction` parameter. `HandleAction` is where stuff actually happens and `ServerRPC_HandleAction` only wraps that one.
