mirror of
https://github.com/sinbad/StevesUEHelpers.git
synced 2025-02-23 09:35:25 +00:00
Add replicated physics actor
This commit is contained in:
parent
c1fef4d10a
commit
be89a2840e
@ -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
|
||||
|
@ -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;
|
||||
}
|
23
Source/StevesUEHelpers/Public/StevesReplicatedPhysicsActor.h
Normal file
23
Source/StevesUEHelpers/Public/StevesReplicatedPhysicsActor.h
Normal 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;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user