UE5学习笔记25-游戏中时间同步

一、原因

1.由于网络问题会导致服务器上的时间和客户端上获得的时间不一致

二、解决方法

在程序启动时向服务器请求服务器的时间返回给客户端并获得客户端发送消息的往返的时间,在获得客户端上的时间,用服务器上获得的时间加上往返时间减去客户端上的时间获得差值,在通过获得客户端上的时间加上差值获得当前服务器上的时间

三、实现

1.在界面添加显示时间的文本框用来显示时间(MatchCountDownText)

2.添加设置界面的函数

2.1界面类中添加

cpp 复制代码
	UPROPERTY(meta = (Bindwidget))
	UTextBlock* MatchCountDownText;

2.2.在PlayerController类中添加设置界面函数

cpp 复制代码
void SetHUDMatchCountDowntime(float CountdownTime);

void ABlasterPlayerController::SetHUDMatchCountDowntime(float CountdownTime)
{
	BlasterHUD = BlasterHUD == nullptr ? Cast<AABasterHUD>(GetHUD()) : BlasterHUD;
	bool bHUDValid = BlasterHUD &&
		BlasterHUD->CharacterOverlay &&
		BlasterHUD->CharacterOverlay->MatchCountDownText;
	if (bHUDValid)
	{
		int32 Minutes = FMath::FloorToInt(CountdownTime / 60.f);
		int32 Seconds = CountdownTime - Minutes * 60;
		FString CarriedDownText = FString::Printf(TEXT("%02d:%02d"), Minutes, Seconds); 
		BlasterHUD->CharacterOverlay->MatchCountDownText->SetText(FText::FromString(CarriedDownText));
	}
}

四、 同步

4.1代码流程

ReceivedPlayer()函数会在客户端启动时最开始调用,通过if判断当前是否是客户端通过GetWorld()->GetTimeSeconds();获得当前的客户端上的时间TClientTime1,调用服务器的RPC请求服务器的时间,在服务器上通过GetWorld()->GetTimeSeconds()获得当前服务器上的时间TServerTime1,在将TClientTime1,TServerTime1作为参数调用客户端的RPC将当前服务器时间TServerTime1和客户端请求同步的时间TClientTime1在给会客户端,在通过GetWorld()->GetTimeSeconds()获得当前客户端时间TClientTime2,通过TClientTime2-TClientTime1获得从请求到收到回复一共用了多长时间,在通过TServerTime1 + (TClientTime2-TClientTime1)/2 在客户端上获得当前服务器上的时间,再通过GetWorld()->GetTimeSeconds();获得客户端现在的时间,用当前服务器上的时间将去当前客户端的时间获得时间的差值。在游戏进行时通过Tick函数进行相同过程的判断。

4.2代码实现,添加服务端RPC函数,客户端RPC函数等

cpp 复制代码
	/**
	*	同步客户端和服务器上的时间
	*/

    virtual void Tick(float DeltaTime) override;

	//请求当前的服务器上的时间,在发送请求时传入 Client 端时间,参数是客户端请求需要的时间
	UFUNCTION(Server , Reliable)
	void ServerRequestServerTime(float TimeOfClientRequest);

	// 给客户端报告当前的服务器的时间响应服务器请求服务器的时间
	UFUNCTION(Client, Reliable)
	void ClientRequestServerTime(float TimeOfClientRequest,float TimeServerReceivedClientRequest);

	float ClientServerDelta = 0.f; // 客户端和服务器端的时间的差值

	UPROPERTY(EditAnywhere , Category = Time)
	float TimeSyncFrequency = 5.f;//同步频率

	float TimeSyncRunningTime = 0.f; // 距离上一次同步过去的时间

	void CheckTimeSync(float DeltaTime);

	virtual float GetServerTime(); // 获得服务器上的时间

	virtual void ReceivedPlayer() override; // 尽快同步服务器时间

    void SetHUDTime();

	float MatchTime = 120.f; // 匹配时间
	uint32 CountdownInt;
cpp 复制代码
void ABlasterPlayerController::ServerRequestServerTime_Implementation(float TimeOfClientRequest)
{
	float ServerTimeOfReceipt = GetWorld()->GetTimeSeconds(); // 服务器上的时间
	//UE_LOG(LogTemp, Warning, TEXT("My variable: %f"), ServerTimeOfReceipt);
	ClientRequestServerTime(TimeOfClientRequest, ServerTimeOfReceipt);
}

void ABlasterPlayerController::ClientRequestServerTime_Implementation(float TimeOfClientRequest, float TimeServerReceivedClientRequest)
{
	float RoundTripTime = GetWorld()->GetTimeSeconds() - TimeOfClientRequest; // 获得往返时间
	float CurrentServerTime = TimeServerReceivedClientRequest + (0.5f * RoundTripTime); // 当前服务器时间
	ClientServerDelta = CurrentServerTime - GetWorld()->GetTimeSeconds();
	//UE_LOG(LogTemp, Warning, TEXT("My variable: %f"), ClientServerDelta);
}

float ABlasterPlayerController::GetServerTime()
{
	float time = GetWorld()->GetTimeSeconds() + ClientServerDelta;
	//UE_LOG(LogTemp, Warning, TEXT("My variable: %f"), time);
	return time;
}

void ABlasterPlayerController::ReceivedPlayer()
{
	Super::ReceivedPlayer();

	if (IsLocalController())
	{
		float Time = GetWorld()->GetTimeSeconds();
		//UE_LOG(LogTemp, Warning, TEXT("My variable: %f"), Time);
		ServerRequestServerTime(Time);
	}
}

void ABlasterPlayerController::SetHUDTime()
{
	uint32 SecondsLeft = FMath::CeilToInt(MatchTime - GetServerTime());

	if (CountdownInt != SecondsLeft)
	{
		//UE_LOG(LogTemp, Warning, TEXT("My variable: %f"), MatchTime - GetServerTime());
		SetHUDMatchCountDowntime(MatchTime - GetServerTime());
	}

	CountdownInt = SecondsLeft;
}

void ABlasterPlayerController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	SetHUDTime();

	CheckTimeSync(DeltaTime);
}

void ABlasterPlayerController::CheckTimeSync(float DeltaTime)
{
	TimeSyncRunningTime += DeltaTime;
	
	if (IsLocalController() && TimeSyncRunningTime > TimeSyncFrequency)
	{
		ServerRequestServerTime(GetWorld()->GetTimeSeconds());
		TimeSyncRunningTime = 0.f;
	}
}
相关推荐
red_redemption24 分钟前
自由学习记录(172)
学习·cache line 64b·重用距离
阿荻在肝了1 小时前
Agent学习六:LangGraph学习-持久化与记忆一
python·学习·agent
妙为1 小时前
UE5中武器未跟随角色
ue5·gas·gameplay
寒秋花开曾相惜3 小时前
(学习笔记)4.1 Y86-64指令集体系结构(4.1.4 Y86-64异常&4.1.5 Y86-64程序)
开发语言·jvm·数据结构·笔记·学习
莹宝思密达3 小时前
【AI学习】 playwright-cli + SKILL 替换 chrom-devTools-MCP
学习
cyr___3 小时前
Unity教程(二十七)技能系统 黑洞技能(下)黑洞状态
学习·游戏·unity·游戏引擎
Theodore_10224 小时前
深度学习(15):倾斜数据集 & 精确率-召回率权衡
人工智能·笔记·深度学习·机器学习·知识图谱
不会聊天真君6474 小时前
JavaScript基础语法(Web前端开发笔记第三期)
前端·javascript·笔记
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.4 小时前
Redis主从复制配置全攻略
数据库·redis·笔记
豆瓣鸡5 小时前
Gradle学习
学习