Solve the code error problem after the UE4 playback system is upgraded to UE5

Keywords: UE4 playback system upgrade UE5 error reporting DemoNetDriver GetDemoCurrentTime GetDemoTotalTime

background

According to the UE4 playback system taught online, it is also called the replay system, English Replay. Finished, the test runs normally, but it can be upgraded to UE5 but a bunch of WorldSetting and Pauser errors are reported.
This article will show you how to fix these errors, not only in UE5, but also in your UE4.

Error reporting

Sorry forgot to take a screenshot anyway it's a bug about WorldSetting and Pauser.

find problem

The Pauser in the WorldSetting file traces back to the source code, as shown in the figure below, "This property is deprecated after 4.23, please use GetPauserPlayerState() or SetPauserPlayerState()" So guess, the version of UE4 has been insisting on maintenance. When it comes to UE5, it simply gave up
insert image description here
.

Then, let's update the new code.

Solution

Please focus on confirming the places marked in red in the figure below. Because the names of these functions have changed. In the old version, it was a variable, but in the new version it becomes a function with a return value.

Except for the part in the red box in the picture below, other places can just follow other playback tutorials on the Internet, no need to change.

The picture below shows the PC_ReplayerSpectator.cpp file, which is the spectator controller I created and inherited from PlayerController. Its header file is the same as other tutorials on the Internet.

insert image description hereinsert image description here

full code

// Fill out your copyright notice in the Description page of Project Settings.


#include "PC_ReplayerSpectator.h"
#include "Engine/World.h"
#include "Engine/DemoNetDriver.h"

APC_ReplayerSpectator::APC_ReplayerSpectator(const FObjectInitializer& ObjectInitializer) :  Super(ObjectInitializer)
{
    
    
	bShowMouseCursor = true;//显示鼠标
	PrimaryActorTick.bTickEvenWhenPaused = true;
	bShouldPerformFullTickWhenPaused = true;
}
bool APC_ReplayerSpectator::SetCurrentReplayPausedState(bool bDoPause)
{
    
    
	//设置暂停状态

	AWorldSettings* WorldSettings = GetWorldSettings();

	//设置抗锯齿为FXAA 和 关闭运动模糊,否则在回放暂停中画面会有问题。
	//命令行 抗锯齿
	static const auto CVarAA = IConsoleManager::Get().FindConsoleVariable(TEXT("r.DefaultFeature.AntiAliasing"));
	//命令行 运动模糊
	static const auto CVarMB = IConsoleManager::Get().FindConsoleVariable(TEXT("r.DefaultFeature.MotionBlur"));

	if (bDoPause)
	{
    
    
		PreviousAASetting = CVarAA->GetInt();
		PreviousMBSetting = CVarMB->GetInt();

		//关闭运动模糊,关闭抗锯齿TXAA切换成FXAA,因为变速播放,跳放会对帧速率产生变化
		CVarAA->Set(1);
		CVarMB->Set(0);

		//设置 暂停状态 ,当SetPauserPlayerState不为NULL时,则暂停
		WorldSettings->SetPauserPlayerState(PlayerState);
		//整个函数返回true,输出暂停。
		return true;
	}
	CVarAA->Set(PreviousAASetting);
	CVarMB->Set(PreviousMBSetting);

	//当SetPauserPlayerState 为NULL时,则不暂停
	WorldSettings->SetPauserPlayerState(NULL);
	//整个函数返回false,输出继续播放。
	return false;
}
//获取当前影片的总时间
int32 APC_ReplayerSpectator::GetCurrentReplayTotalTimeInSeconds() const
	{
    
    
		if (GetWorld())
		{
    
    
			if (GetWorld()->GetDemoNetDriver())
			{
    
    
				return GetWorld()->GetDemoNetDriver()->GetDemoTotalTime();
			}
		}
		return 0.f;
	}
//获取当前时刻的时间
int32 APC_ReplayerSpectator::GetCurrentReplayCurrentTimeInSeconds() const
	{
    
    
		if (GetWorld())
		{
    
    
			if (GetWorld()->GetDemoNetDriver())
			{
    
    
				return GetWorld()->GetDemoNetDriver()->GetDemoCurrentTime();
			}
		}
		return 0.f;
	}

//设置跳转到的时刻
void APC_ReplayerSpectator::SetCurrentReplayTimeToSeconds(int32 Seconds) 
{
    
    
	if (GetWorld())
	{
    
    
		if (GetWorld()->GetDemoNetDriver())
		{
    
    
			GetWorld()->GetDemoNetDriver()->GotoTimeInSeconds(Seconds);
		}
	}
}
//设置播放速率
void APC_ReplayerSpectator::SetCurrentReplayPlayRate(float PlayRate)
{
    
    
	if(GetWorld())
	{
    
    
		if (GetWorld()->GetDemoNetDriver()) 
		{
    
    
			GetWorld()->GetWorldSettings()->DemoPlayTimeDilation = PlayRate;
		}
	}
}

appendix:

You may find the PC_ReplayerSpectator.h header file useful

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "PC_ReplayerSpectator.generated.h"

/**
 * 
 */
UCLASS()
class AUCDISPLAY_API APC_ReplayerSpectator : public APlayerController
{
    
    
	GENERATED_BODY()
	
public:
	APC_ReplayerSpectator(const FObjectInitializer& ObjectInitializer);

protected:

	int32 PreviousAASetting;
	int32 PreviousMBSetting;

	UFUNCTION(BlueprintCallable, Category = "CurrentReplay")
		bool SetCurrentReplayPausedState(bool bDoPause);

	UFUNCTION(BlueprintCallable, Category = "CurrentReplay")
		int32 GetCurrentReplayTotalTimeInSeconds() const;
			
	UFUNCTION(BlueprintCallable, Category = "CurrentReplay")
		int32 GetCurrentReplayCurrentTimeInSeconds() const;

	UFUNCTION(BlueprintCallable, Category = "CurrentReplay")
		void SetCurrentReplayTimeToSeconds(int32 Seconds);

	UFUNCTION(BlueprintCallable, Category = "CurrentReplay")
		void SetCurrentReplayPlayRate(float PlayRate = 1.0f);

};

I wish you success

Guess you like

Origin blog.csdn.net/leelizc/article/details/130208830