Hello,
I created an actor with a mesh component, and added an enable and disable function, like this:
header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "Barrel.generated.h"
UCLASS()
class MYGAME_API ABarrel : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABarrel();
/** StaticMeshComponent to represent the pickup in the level */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Replicated, Category = "Barrel")
UStaticMeshComponent* BarrelMesh;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
/** Override function to replicate this class properties */
void GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const override;
void EnableBarrel();
void DisableBarrel();
};
cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGame.h"
#include "Barrel.h"
// Sets default values
ABarrel::ABarrel()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
//Create the static mesh component
BarrelMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BarrelMesh"));
// Set the SphereComponent as the root component
RootComponent = BarrelMesh;
SetReplicates(true);
}
// Called when the game starts or when spawned
void ABarrel::BeginPlay()
{
Super::BeginPlay();
EnableBarrel();
}
void ABarrel::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// Replicate to everyone
DOREPLIFETIME(ABarrel, BarrelMesh);
}
void ABarrel::EnableBarrel()
{
BarrelMesh->SetHiddenInGame(false);
BarrelMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
}
void ABarrel::DisableBarrel()
{
BarrelMesh->SetHiddenInGame(true);
BarrelMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
In the GameMode, there’s a check in DefaultTimer() that runs it every 5 seconds (testing purposes), effectivelly toggling the barrel visibility and collision.
My problem is: It works on the listen server (in the editor), but it does not work on the client.
Collision is disabled, but the actor do not disappear.
Any idea how can I replicate the SetHiddenInGame() call?
Thank you in advance!