Ue4C++Replay详细编写案例
时间: 2024-03-12 21:50:04 浏览: 91
UE4 UI简单例子 C++
好的,下面是一个简单的Ue4 C++ Replay编写案例:
1. 首先,在UE4中创建一个新的C++项目,并创建一个名为MyGameInstance的GameInstance子类。
2. 在MyGameInstance.h文件中添加以下代码:
```cpp
#pragma once
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"
UCLASS()
class MYPROJECT_API UMyGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable)
void StartRecording();
UFUNCTION(BlueprintCallable)
void StopRecording();
UFUNCTION(BlueprintCallable)
void StartReplay();
private:
FString ReplayFilename;
};
```
3. 在MyGameInstance.cpp文件中实现StartRecording、StopRecording和StartReplay函数。下面是一个简单的实现示例:
```cpp
#include "MyGameInstance.h"
#include "Engine/Engine.h"
#include "EngineUtils.h"
#include "GameFramework/PlayerController.h"
#include "Misc/Paths.h"
#include "Misc/FileHelper.h"
#include "Engine/LocalPlayer.h"
#include "GameFramework/PlayerState.h"
#include "GameFramework/GameStateBase.h"
#include "GameFramework/WorldSettings.h"
void UMyGameInstance::StartRecording()
{
FString CurrentDateTime = FDateTime::Now().ToString(TEXT("%Y-%m-%d-%H-%M-%S"));
ReplayFilename = FPaths::ProjectSavedDir() / FString(TEXT("Replays/")) + CurrentDateTime + TEXT(".replay");
for (TActorIterator<APlayerController> It(GetWorld()); It; ++It)
{
APlayerController* PlayerController = *It;
if (PlayerController->IsLocalPlayerController())
{
ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(PlayerController->Player);
if (LocalPlayer)
{
FString AdditionalData = FString::Printf(TEXT("PlayerName=%s"), *LocalPlayer->GetPlayerName());
GetWorld()->DemoNetDriver->StartRecording(ReplayFilename, false, AdditionalData);
break;
}
}
}
}
void UMyGameInstance::StopRecording()
{
GetWorld()->DemoNetDriver->StopRecording();
}
void UMyGameInstance::StartReplay()
{
if (FFileHelper::FileExists(*ReplayFilename))
{
for (TActorIterator<APlayerController> It(GetWorld()); It; ++It)
{
APlayerController* PlayerController = *It;
if (PlayerController->IsLocalPlayerController())
{
ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(PlayerController->Player);
if (LocalPlayer)
{
FString AdditionalData = FString::Printf(TEXT("PlayerName=%s"), *LocalPlayer->GetPlayerName());
GetWorld()->DemoNetDriver->PlayDemoFromFile(ReplayFilename, AdditionalData);
break;
}
}
}
}
}
```
4. 在UE4中创建一个新的Blueprint类,并将其命名为ReplayActor。在ReplayActor中添加以下代码:
```cpp
UCLASS()
class MYPROJECT_API AReplayActor : public AActor
{
GENERATED_BODY()
public:
AReplayActor();
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* StaticMeshComponent;
UPROPERTY(EditAnywhere)
UMaterialInterface* Material;
void OnReplayFinished();
};
```
5. 在ReplayActor.cpp文件中实现OnReplayFinished和BeginPlay函数。下面是一个简单的实现示例:
```cpp
#include "ReplayActor.h"
#include "Components/StaticMeshComponent.h"
#include "Materials/MaterialInterface.h"
#include "Kismet/GameplayStatics.h"
#include "MyGameInstance.h"
AReplayActor::AReplayActor()
{
PrimaryActorTick.bCanEverTick = false;
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
StaticMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
RootComponent = StaticMeshComponent;
}
void AReplayActor::BeginPlay()
{
Super::BeginPlay();
if (UMyGameInstance* GameInstance = Cast<UMyGameInstance>(GetGameInstance()))
{
FDelegateHandle ReplayFinishedHandle = GameInstance->GetWorld()->AddOnWorldCleanup(FWorldCleanup::FOnWorldCleanupDelegate::CreateUObject(this, &AReplayActor::OnReplayFinished));
}
if (Material)
{
StaticMeshComponent->SetMaterial(0, Material);
}
}
void AReplayActor::OnReplayFinished()
{
Destroy();
}
```
6. 在UE4中创建一个新的关卡,并将其命名为ReplayLevel。在ReplayLevel中添加一个ReplayActor,并将其Material属性设置为一个你喜欢的材质。
7. 在UE4中创建一个新的蓝图,并将其命名为ReplayController。在ReplayController中添加以下事件:
- Event Begin Play:调用MyGameInstance的StartRecording函数来开始记录Replay。
- Input Action Replay:调用MyGameInstance的StartReplay函数来开始回放Replay。
8. 在ReplayController中添加一个Input Action,并将其命名为Replay。在Replay Action的Details面板中将Key设置为R。
9. 在UE4中运行ReplayLevel,并在ReplayController蓝图中按下R键来开始记录Replay。完成后,按下R键再次开始回放Replay。
上面是一个简单的UE4 C++ Replay编写案例。您可以根据您的需求和项目的要求进行修改和扩展。
阅读全文