Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Physics door not work
#include "Town.h"
#include "Door.h"
#include "UnrealNetwork.h"
// Sets default values
ADoor::ADoor(const class FObjectInitializer& ObjectInitializer)
{
is_opened = false;
PrimaryActorTick.bCanEverTick = true;
SetReplicates(true);
bAlwaysRelevant = true;
NetPriority = 1.0f;
this->SetReplicateMovement(true);
//ReplicatedMovement.bRepPhysics = 1;
mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("door_mesh"));
ConstructorHelpers::FObjectFinder<USkeletalMesh> fmesh_finder(TEXT("SkeletalMesh'/Game/Interior/Doors/NP110_EnterDoor_Board_Skinned.NP110_EnterDoor_Board_Skinned'"));
USkeletalMesh* fmesh = nullptr;
if (fmesh_finder.Succeeded())
fmesh = fmesh_finder.Object;
else
fmesh = LoadObject<USkeletalMesh>(NULL, TEXT("SkeletalMesh'/Game/Interior/Doors/NP110_EnterDoor_Board_Skinned.NP110_EnterDoor_Board_Skinned'"), NULL, LOAD_None, NULL);
mesh->SetSkeletalMesh(fmesh);
mesh->SetRelativeLocation(FVector(0.0, 0.0, 0.0));
mesh->bEnablePhysicsOnDedicatedServer = true;
joint = mesh->FindConstraintInstance(FName("BoneBoard"));
if (mesh->bEnablePhysicsOnDedicatedServer && !IsRunningDedicatedServer())
{
mesh->BodyInstance.bSimulatePhysics = true;
mesh->bBlendPhysics = true;
for (int32 i = 0; i < mesh->Bodies.Num(); i++)
{
mesh->Bodies[i]->bSimulatePhysics = true;
mesh->Bodies[i]->UpdateInstanceSimulatePhysics();
}
}
RootComponent = mesh;
}
void ADoor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
}
void ADoor::BeginPlay()
{
AActor::BeginPlay();
}
void ADoor::Tick(float DeltaTime)
{
AActor::Tick(DeltaTime);
}
void ADoor::Use(AActor* user)
{
if (Role == ROLE_SimulatedProxy) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, "Use Simulated Proxy", false);
if (Role == ROLE_AutonomousProxy) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, "Use Autonomous Proxy", false);
if (Role == ROLE_Authority) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, "Use Authority", false);
Open(user);
}
FString ADoor::GetActionsMenuPath() const
{
return FString("WidgetBlueprint'/Game/UI/DoorTwoActionMenu.DoorTwoActionMenu_C'");
}
void ADoor::ServerUse_Implementation(AActor * user)
{
if (Role == ROLE_SimulatedProxy) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, "Server Use Simulated Proxy", false);
if (Role == ROLE_AutonomousProxy) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, "Server Use Autonomous Proxy", false);
if (Role == ROLE_Authority) GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::White, "Server Use Authority", false);
Use(user);
}
bool ADoor::ServerUse_Validate(AActor * user)
{
return true;
}
void ADoor::ExecuteAction(AActor* user, const FString& action)
{
GEngine->AddOnScreenDebugMessage(-1, 40.0f, FColor::Green, "Execute Action: " + action, false);
//if (Role < ROLE_Authority)
// ServerExecuteAction(user, action);
if (action == "open")
Open(user);
else
if (action == "close")
Close(user);
}
void ADoor::ServerExecuteAction_Implementation(AActor* user, const FString& action)
{
GEngine->AddOnScreenDebugMessage(-1, 40.0f, FColor::Green, "Server Execute Action: " + action, false);
if (action == "open")
Open(user);
else
if (action == "close")
Close(user);
}
bool ADoor::ServerExecuteAction_Validate(AActor* user, const FString& action)
{
return true;
}
void ADoor::Open(AActor* user)
{
GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::White, "OPEN", false);
if (joint == nullptr) return;
joint->SetAngularVelocityDriveTwistAndSwing(false, true);
joint->SetAngularVelocityTarget(FVector(10, 10, 10));
joint->SetAngularDriveParams(300,300,300);
}
void ADoor::Close(AActor* user)
{
GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::White, "CLOSE", false);
if (joint == nullptr) return;
joint->SetAngularVelocityDriveTwistAndSwing(false, true);
joint->SetAngularVelocityTarget(FVector(-10, -10, -10));
}
bool ADoor::IsOpen()
{
return is_opened;
}
#pragma once
#include <Runtime/Engine/Classes/PhysicsEngine/PhysicsConstraintComponent.h>
#include "GameFramework/Actor.h"
#include "Usable.h"
#include "Door.generated.h"
UCLASS()
class TOWN_API ADoor : public AActor, public IUsable
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ADoor(const class FObjectInitializer& ObjectInitializer);
void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
virtual void Use(AActor* user) override;
virtual FString GetActionsMenuPath() const override;
virtual void ExecuteAction(AActor* user, const FString& action) override;
virtual void Open(AActor* user);
virtual void Close(AActor* user);
bool IsOpen();
// Клиент вызывает функцию на сервере, сервер её выполняет.
UFUNCTION(Server, Reliable, WithValidation)
virtual void ServerExecuteAction(AActor* user, const FString& action);
virtual void ServerExecuteAction_Implementation(AActor* user, const FString& action);
virtual bool ServerExecuteAction_Validate(AActor* user, const FString& action);
// Клиент вызывает функцию на сервере, сервер её выполняет.
UFUNCTION(Server, Reliable, WithValidation)
virtual void ServerUse(AActor* user);
virtual void ServerUse_Implementation(AActor* user);
virtual bool ServerUse_Validate(AActor* user);
protected:
bool is_opened;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Door, meta = (AllowPrivateAccess = "true"))
USkeletalMeshComponent* mesh;
FConstraintInstance* joint;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.