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:
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:
#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!