# How To REPLICATE the Door code On MULTICAST and SERVER?

**URL:** <https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012>\
**Category:** Multiplayer & Networking\
**Tags:** client, unreal-engine, Multiplayer, CPP, question\
**Created:** [April 25, 2024, 1:52pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012 "2024-04-25T13:52:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlashAWme](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FlashAWme](https://forums.unrealengine.com/u/FlashAWme)\
**Post date:** [April 25, 2024, 1:52pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012/1 "2024-04-25T13:52:16Z")

</div>

#pragma once

#include “CoreMinimal.h”  
#include “GameFramework/Actor.h”  
#include “Components/StaticMeshComponent.h”  
#include “Components/TimelineComponent.h”  
#include “MyInterface.h”  
#include “MyDoor.generated.h”

class AMyCharacter;

UCLASS()  
class INTHEFRONT\_API AMyDoor : public AActor, public IMyInterface  
{  
GENERATED\_BODY()

public:   
// Sets default values for this actor’s properties  
AMyDoor();

protected:  
// Called when the game starts or when spawned  
virtual void BeginPlay() override;

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

```
virtual void OnInteractDoor();

void ToggleDoor();

UPROPERTY(VisibleDefaultsOnly, Category = "Mesh")
UStaticMeshComponent* DoorFrame;

UPROPERTY(VisibleDefaultsOnly, Category = "Mesh")
UStaticMeshComponent* Door;

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Animation")
UCurveFloat* CurveFloat;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Door")
bool IsDoorOpen = false;

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Door")
float DoorRotateAngle = 90.f;

UPROPERTY()
AMyCharacter* MyCharacter;	

```

protected:  
FTimeline Timeline;

```
bool DoorSameSide;

void OpenDoor(float Value);

void SetDoorOnSameSide();

```

};

#include “MyDoor.h”  
#include “MyCharacter.h”

// Sets default values  
AMyDoor::AMyDoor()  
{  
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.  
PrimaryActorTick.bCanEverTick = true;

```
DoorFrame = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DoorFrame"));
DoorFrame->SetupAttachment(RootComponent);

Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
Door->SetupAttachment(DoorFrame);

```

}

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

```
if (CurveFloat)
{
	FOnTimelineFloat TimelineProgress;
	TimelineProgress.BindUFunction(this, FName("OpenDoor"));
	Timeline.AddInterpFloat(CurveFloat, TimelineProgress);
}

if (IsDoorOpen)
{
	Timeline.Play();
}

```

}

// Called every frame  
void AMyDoor::Tick(float DeltaTime)  
{  
Super::Tick(DeltaTime);

```
Timeline.TickTimeline(DeltaTime);

```

}

void AMyDoor::OnInteractDoor()  
{

```
ToggleDoor();
IsDoorOpen = !IsDoorOpen;

```

}

void AMyDoor::ToggleDoor()  
{  
if (IsDoorOpen)  
{  
Timeline.Reverse();  
}  
else  
{  
if (!Timeline.IsPlaying())  
{  
SetDoorOnSameSide();  
Timeline.Play();  
}  
}  
}

void AMyDoor::OpenDoor(float Value)  
{  
float Angle = DoorSameSide ? -DoorRotateAngle : DoorRotateAngle;  
FRotator Rot = FRotator(0.f, Angle \* Value, 0.f);  
Door-\>SetRelativeRotation(Rot);  
}

void AMyDoor::SetDoorOnSameSide()  
{  
if (MyCharacter)  
{  
FVector CharacterFV = MyCharacter-\>GetActorForwardVector();  
FVector DoorFV = GetActorForwardVector();  
DoorSameSide = (FVector::DotProduct(CharacterFV, DoorFV) \>= 0);  
}  
}

How To REPLICATE the Door code On MULTICAST and SERVER???

---

<div class="post-metadata">

**Author:** ![RockyDias](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RockyDias](https://forums.unrealengine.com/u/RockyDias)\
**Post date:** [April 25, 2024, 1:56pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012/2 "2024-04-25T13:56:28Z")

</div>

Try set `SetReplicateMovement(true);` on door actor and replicate the Door static mesh. Also, make sure to create a RPC on server to play the timeline. Make sure to read: [Multiplayer Network Compendium | An Unreal Engine Blog by Cedric Neukirchen](https://cedric-neukirchen.net/docs/category/multiplayer-network-compendium/)

---

<div class="post-metadata">

**Author:** ![Rev0verDrive](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rev0verdrive/32/907214_2.png) [@Rev0verDrive](https://forums.unrealengine.com/u/Rev0verDrive)\
**Post date:** [April 27, 2024, 7:50pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012/3 "2024-04-27T19:50:26Z")

</div>

You only want to replicate the door state. Open or closed. Let each client handle the timeline process.

Rep\_notify is your friend here.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/1/d/f1d08c1c51d255deaae0afcccaf194220da772a9.jpeg "UE5 - Multiplayer Interaction System Pt 2 - Doors") ](https://www.youtube.com/watch?v=1PNMh0pYNMI)

---

<div class="post-metadata">

**Author:** ![FlashAWme](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FlashAWme](https://forums.unrealengine.com/u/FlashAWme)\
**Post date:** [April 29, 2024, 4:05pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012/4 "2024-04-29T16:05:05Z")

</div>

Hey, the video is about blueprint code but i programming on c++ code

---

<div class="post-metadata">

**Author:** ![adlab](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@adlab](https://forums.unrealengine.com/u/adlab)\
**Post date:** [April 29, 2024, 4:55pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012/5 "2024-04-29T16:55:09Z")

</div>

To replicate the door code on multicast and server, you can use the `OnRep_IsDoorOpen` function. This function is called on the server and all clients when the `IsDoorOpen` property is changed.

Here’s an example of how you can use `OnRep_IsDoorOpen` to replicate the door code:

```auto
void AMyDoor::OnRep_IsDoorOpen(bool NewValue)
{
    if (NewValue)
    {
        // Open the door on the server
        OpenDoor(1.0f);

        // Notify all clients that the door is open
        GetLifetimeReplicatedProps().BroadcastRep_IsDoorOpen(NewValue);
    }
    else
    {
        // Close the door on the server
        CloseDoor();

        // Notify all clients that the door is closed
        GetLifetimeReplicatedProps().BroadcastRep_IsDoorOpen(NewValue);
    }
}

```

In this example, the `OnRep_IsDoorOpen` function checks the new value of `IsDoorOpen`. If it’s true, it opens the door on the server and notifies all clients that the door is open. If it’s false, it closes the door on the server and notifies all clients that the door is closed.

Note that you’ll need to add the `IsDoorOpen` property to the list of replicated properties in the `AMYDOOR_API` macro in the `MyDoor.h` file:

```auto
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/TimelineComponent.h"
#include "MyInterface.h"
#include "MyDoor.generated.h"

class AMyCharacter;

UCLASS()
class INTHEFRONT_API AMyDoor : public AActor, public IMyInterface
{
    GENERATED_BODY()

public:
    // Sets default values for this actor’s properties
    AMyDoor();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

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

    virtual void OnInteractDoor();

    void ToggleDoor();

    UPROPERTY(VisibleDefaultsOnly, Category = "Mesh")
    UStaticMeshComponent* DoorFrame;

    UPROPERTY(VisibleDefaultsOnly, Category = "Mesh")
    UStaticMeshComponent* Door;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Animation")
    UCurveFloat* CurveFloat;

    UPROPERTY()
    AMyCharacter* MyCharacter;      
    protected:
        FTimeline Timeline;

    bool DoorSameSide;

    void OpenDoor(float Value);

    void SetDoorOnSameSide();
};

#include "MyDoor.h"
#include "MyCharacter.h"

// Sets default values
AMYDOOR_API AMyDoor::AMYDOOR_API()
{
    // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
    PrimaryActorTick.bCanEverTick = true;

    DoorFrame = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DoorFrame"));
    DoorFrame->SetupAttachment(RootComponent);

    Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
    Door->SetupAttachment(DoorFrame);
}

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

    if (CurveFloat)
    {
        FOnTimelineFloat TimelineProgress;
        TimelineProgress.BindUFunction(this, FName("OpenDoor"));
        Timeline.AddInterpFloat(CurveFloat, TimelineProgress);
    }

    if (IsDoorOpen)
    {
        Timeline.Play();
    }
}

// Called every frame
AMYDOOR_API void AMyDoor::Tick(float DeltaTime)
{
hope it works!
```

---

<div class="post-metadata">

**Author:** ![Rev0verDrive](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rev0verdrive/32/907214_2.png) [@Rev0verDrive](https://forums.unrealengine.com/u/Rev0verDrive)\
**Post date:** [April 29, 2024, 6:19pm UTC](https://forums.unrealengine.com/t/how-to-replicate-the-door-code-on-multicast-and-server/1821012/6 "2024-04-29T18:19:20Z")

</div>

Yup it certainly is. Translate to C++

DoorState (rep\_notify)  
OnRep DoorState function uses the doorstate’s value to determine whether to open or close.

* * *

Do not replicate movement. It uses too much bandwidth and looks like hot garbage on the client.
