Add replicated physics actor

This commit is contained in:
Steve Streeting 2023-11-13 10:59:41 +00:00
parent c1fef4d10a
commit be89a2840e
3 changed files with 75 additions and 0 deletions

View File

@ -12,6 +12,7 @@ which makes a bunch of things better:
* [Debug visualisation](https://www.stevestreeting.com/2021/09/14/ue4-editor-visualisation-helper/)
* [Better DataTable Row References](https://www.stevestreeting.com/2023/10/06/a-better-unreal-datatable-row-picker/)
* [Light Flicker](doc/LightFlicker.md)
* [Replicated Physics Actor](Source/StevesUEHelpers/Public/StevesReplicatedPhysicsActor.h)
* Halton Sequence based random stream
## Examples

View File

@ -0,0 +1,51 @@
// Copyright Steve Streeting
// Licensed under the MIT License (see License.txt)
#include "StevesReplicatedPhysicsActor.h"
AStevesReplicatedPhysicsActor::AStevesReplicatedPhysicsActor()
{
PrimaryActorTick.bCanEverTick = false;
bReplicates = true;
bStaticMeshReplicateMovement = true;
const auto MeshComp = GetStaticMeshComponent();
MeshComp->SetMobility(EComponentMobility::Movable);
MeshComp->SetCollisionObjectType(ECC_WorldDynamic);
MeshComp->bReplicatePhysicsToAutonomousProxy = true;
// We do NOT replicate MeshComp itself! That's expensive and unnecessary
}
void AStevesReplicatedPhysicsActor::BeginPlay()
{
SetReplicateMovement(true);
const auto MeshComp = GetStaticMeshComponent();
// Server test, includes dedicated and listen servers
if (IsServer())
{
// Server collision. Block all but allow pawns through
// Subclasses can change this if they like you can change t
MeshComp->SetCollisionResponseToAllChannels(ECR_Block);
MeshComp->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
MeshComp->SetSimulatePhysics(true);
}
else
{
// Ignore all collisions on client
MeshComp->SetCollisionResponseToAllChannels(ECR_Ignore);
}
Super::BeginPlay();
}
bool AStevesReplicatedPhysicsActor::IsServer() const
{
// Server test, includes dedicated and listen servers
// I prefer this to Authority test because you can't replicate objects client->server or client->client anyway
return GetWorld()->GetNetMode() < NM_Client;
}

View File

@ -0,0 +1,23 @@
// Copyright Steve Streeting
// Licensed under the MIT License (see License.txt)
#pragma once
#include "CoreMinimal.h"
#include "Engine/StaticMeshActor.h"
#include "StevesReplicatedPhysicsActor.generated.h"
/// A static mesh actor which is simulated on the server and replicated to clients.
/// The results will be pretty smooth on the clients due to a very specific set of settings.
/// You MUST spawn this actor ONLY on the server!
UCLASS(Blueprintable)
class STEVESUEHELPERS_API AStevesReplicatedPhysicsActor : public AStaticMeshActor
{
GENERATED_BODY()
public:
AStevesReplicatedPhysicsActor();
protected:
virtual void BeginPlay() override;
bool IsServer() const;
};