I’ve recently beem working on a networked game and ran into some troubles with replication on physics objects. The image below was with an object with replicate movement off:
Even with replicate movement on I get some desync (its hard to tell in the image but its subtle) and other issues (like characters glitching when they touch the object or disappearing).
Am I doing replication for physics objects incorrectly? Ideally for these types of objects i would like physics to only be generated from the server.
// Fill out your copyright notice in the Description page of Project Settings.
**//@CoinPickup.h**
#pragma once
#include "CoreMinimal.h"
#include "Pickup.h"
#include "CoinPickup.generated.h"
/**
*
*/
UCLASS()
class CPPTEST_API ACoinPickup : public APickup
{
GENERATED_BODY()
public:
//set default values for constructor
ACoinPickup();
};
// Fill out your copyright notice in the Description page of Project Settings.
**//@CoinPickup.cpp**
#include "CoinPickup.h"
ACoinPickup::ACoinPickup()
{
//keep physics synced from client to server
bReplicateMovement = true;
//this coponent is physics enabled and should move
GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable);
GetStaticMeshComponent()->SetSimulatePhysics(true);
}
// Fill out your copyright notice in the Description page of Project Settings.
**//@Pickup.h**
#pragma once
#include "CoreMinimal.h"
#include "Engine/StaticMeshActor.h"
#include "Pickup.generated.h"
/**
*
*/
UCLASS()
class CPPTEST_API APickup : public AStaticMeshActor
{
GENERATED_BODY()
public:
//Set default values
APickup();
//Code for networking
void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
//Returns wether or not the pickup is activated
UFUNCTION(BlueprintPure, Category = "Pickup")
bool IsActive();
//Allows other classes to safely change the pickup's activation state
UFUNCTION(BlueprintCallable, Category = "Pickup")
void SetActive(bool newPickupState);
protected:
//True when pickup can be activated
UPROPERTY(ReplicatedUsing = OnRep_IsActive)
bool bIsActive;
//This is called whenever bIsActive is updated
UFUNCTION()
virtual void OnRep_IsActive();
};
// Fill out your copyright notice in the Description page of Project Settings.
**//@Pickup.cpp**
#include "Pickup.h"
#include "Net/UnrealNetwork.h"
APickup::APickup()
{
//Tells UE4 to replicate actor
bReplicates = true;
//Pickups will never tick
PrimaryActorTick.bCanEverTick = false;
if (Role = ROLE_Authority)
{
bIsActive = true;
}
}
void APickup::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(APickup, bIsActive);
}
void APickup::OnRep_IsActive()
{
}
void APickup::SetActive(bool newPickupState)
{
if (Role == ROLE_Authority)
{
bIsActive = newPickupState;
}
}
bool APickup::IsActive()
{
return bIsActive;
}